jupyter中ipython的基本使用方法,帮助你更快速高效的学习

简介: jupyter中ipython的基本使用方法,帮助你更快速高效的学习

一、启动程序


命令:jupyter notebook


这个命令可以启动jupyter的交互服务器,并且把当前目录作为映射打开一个web界面,加载映射的目录结构


【注意】如果这个命令提示错误,检测环境变量还有anaconda是否安装完全(如果不完全:手动安装pip install jupyter)


二、IPython的帮助文档


1、使用help()


help(list)


Help on class list in module builtins:
class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __iadd__(self, value, /)
 |      Implement self+=value.
 |  
 |  __imul__(self, value, /)
 |      Implement self*=value.
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __mul__(self, value, /)
 |      Return self*value.n
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __reversed__(...)
 |      L.__reversed__() -- return a reverse iterator over the list
 |  
 |  __rmul__(self, value, /)
 |      Return self*value.
 |  
 |  __setitem__(self, key, value, /)
 |      Set self[key] to value.
 |  
 |  __sizeof__(...)
 |      L.__sizeof__() -- size of L in memory, in bytes
 |  
 |  append(...)
 |      L.append(object) -> None -- append object to end
 |  
 |  clear(...)
 |      L.clear() -> None -- remove all items from L
 |  
 |  copy(...)
 |      L.copy() -> list -- a shallow copy of L
 |  
 |  count(...)
 |      L.count(value) -> integer -- return number of occurrences of value
 |  
 |  extend(...)
 |      L.extend(iterable) -> None -- extend list by appending elements from the iterable
 |  
 |  index(...)
 |      L.index(value, [start, [stop]]) -> integer -- return first index of value.
 |      Raises ValueError if the value is not present.
 |  
 |  insert(...)
 |      L.insert(index, object) -- insert object before index
 |  
 |  pop(...)
 |      L.pop([index]) -> item -- remove and return item at index (default last).
 |      Raises IndexError if list is empty or index is out of range.
 |  
 |  remove(...)
 |      L.remove(value) -> None -- remove first occurrence of value.
 |      Raises ValueError if the value is not present.
 |  
 |  reverse(...)
 |      L.reverse() -- reverse *IN PLACE*
 |  
 |  sort(...)
 |      L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None


help(len)


Help on built-in function len in module builtins:
len(obj, /)
    Return the number of items in a container.


len("123")


3


2、使用"?"


len?
• 1


list?
• 1


“?”调出一个函数的帮助文档,“??”调出一个函数的帮助文档以及源码


len??
• 1


def add_sum(a,b):
    "求两个数的和"
    c = a+b
    return c
• 1
• 2
• 3
• 4


add_sum??
• 1


3、使用"tab"键自动补全


aaaaa=10
• 1


import numpy
aaaaa
• 1
• 2
• 3


10


三、IPython魔法命令


1、运行在外部Python文件


%run xx.py


下面例子中的外部test.py文件的内容如下:


def hello(a):
    c = a**2
    m = 10000
    return c
w = 10000


以下为在Jupyter notebook中输入的代码:


%run test.py
• 1


hello(10)
• 1


100
• 1


w
• 1


10000
• 1


m # m是局部变量


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-24-69b64623f86d> in <module>()
----> 1 m
NameError: name 'm' is not defined


2、查看运行计时


%time print("hello")
• 1


hello
Wall time: 0 ns


def func1():
    res = 0
    for i in range(1000):
        res += 1
        for j in range(1000):
            res -= 1


%time func1()


Wall time: 65.5 ms


%timeit func1()


76.8 ms ± 4.24 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)


%%timeit
print("afda")
func1()
list("123")


afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
afda
136 ms ± 19.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)


%time 一般耗时比较多的代码用这个
%timeit 一般是耗时比较少的代码


3、查看当前会话中所有的变量与函数


%who


a  aaaaa   add_sum   func1   hello   numpy   w   


%whos


Variable   Type        Data/Info
--------------------------------
a          int         10
aaaaa      int         10
add_sum    function    <function add_sum at 0x000001957CA21840>
func1      function    <function func1 at 0x000001957CEEB840>
hello      function    <function hello at 0x000001957CEEB8C8>
numpy      module      <module 'numpy' from 'C:\<...>ges\\numpy\\__init__.py'>
w          int         10000


%who_ls


['a', 'aaaaa', 'add_sum', 'func1', 'hello', 'numpy', 'w']


%who、%whos、%who_ls查看当前cell运行的时候,ipython服务器中有那些函数和变量以及框架库(modele)等存在


4、执行系统终端指令


写法:!指令名(在windows系统下应该执行Windows的系统命令,linux要执行对应的Linux版本的系统zhil)


!ipconfig


Windows IP 配置
以太网适配器 以太网:
   连接特定的 DNS 后缀 . . . . . . . : 
   本地链接 IPv6 地址. . . . . . . . : fe80::b5c8:db48:5d06:3657%5
   IPv4 地址 . . . . . . . . . . . . : 10.31.153.97
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . : 10.31.161.1


!cd ..


!mkdir ppp


5、更多魔法指令或者cmd


列出所有的魔法指令


%lsmagic


%lsmagic



Available line magics:
%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cd  %clear  %cls  %colors  %config  %connect_info  %copy  %ddir  %debug  %dhist  %dirs  %doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %macro  %magic  %matplotlib  %mkdir  %more  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %popd  %pprint  %precision  %profile  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode
Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.


%cd?


print("sdafads")


sdafads


四、快捷键


1、命令模式


enter: 转入编辑模式


shift+enter:运行本行,并且选中下行


ctrl+enter: 运行本行,并且选中本行


alt+enter:运行本行,并且插入一个新的cell


Y:cell转入代码状态


M:cell转入Markdown状态


A: 在上方插入一个新的cell


B:在下方插入一个新的cell


双击D:删除当前cell


2、编辑模式


tab(或shift+tab)键:提示


ctrl+a:全选当前cell


ctrl+z:撤销


相关文章
|
2天前
|
数据采集 数据可视化 数据挖掘
交互式数据分析:使用Jupyter Notebooks和IPython提高生产力
【4月更文挑战第12天】Jupyter Notebooks和IPython是交互式数据分析的强大工具,提供了一个集成环境,支持多种编程语言,提升效率并减少错误。它们具有交互式编程、丰富库支持、可扩展性和协作功能。基本流程包括数据导入(如使用Pandas从CSV加载)、预处理、分析(利用Pandas、NumPy、Matplotlib等)、模型选择与训练(如Scikit-learn的RandomForestClassifier)以及模型评估和优化。
|
5月前
|
Linux 数据安全/隐私保护 Windows
jupyter环境下从零模拟开发简易操作系统: 搭建一个学习ucore_OS学习的环境(第二节)
上一节已经一万字了,只好再加一节 如果想要直接 体验 实验效果 可以直接执行本章的命令 生成的img文件可以在qemu中正常运行的话,那么在 真实硬件上基本 也是可以 正常安装操作系统
32 1
|
11月前
|
大数据 Linux Shell
【大数据学习篇13】在linux上安装jupyter
【大数据学习篇13】在linux上安装jupyter
169 0
|
12月前
|
人工智能 数据可视化 Scala
在PyCharm中使用Jupyter进行人工智能学习开发经验介绍
在PyCharm中使用Jupyter进行人工智能学习开发经验介绍
382 0
|
12月前
|
机器学习/深度学习 数据可视化 数据安全/隐私保护
python学习之旅(jupyter)
python学习之旅(jupyter)
86 0
|
机器学习/深度学习 Ubuntu 前端开发
数据挖掘基础学习一:VMware虚拟机Ubuntu上安装Python和IPython Notebook(Jupyter Notebook)完整步骤及需要注意的问题(以ubuntu-18.04.3为例)
数据挖掘基础学习一:VMware虚拟机Ubuntu上安装Python和IPython Notebook(Jupyter Notebook)完整步骤及需要注意的问题(以ubuntu-18.04.3为例)
643 0
数据挖掘基础学习一:VMware虚拟机Ubuntu上安装Python和IPython Notebook(Jupyter Notebook)完整步骤及需要注意的问题(以ubuntu-18.04.3为例)
|
IDE 数据可视化 前端开发
在tinycolinux上编译jupyter和rootcling组建混合cpp,python学习环境
本文关键字:升级/枚举tinycorelinux上的gcc,在tinycorelinux上安装python jupyter
243 0
在tinycolinux上编译jupyter和rootcling组建混合cpp,python学习环境
|
新零售 监控 数据可视化
【资料下载】Python第四讲——使用IPython/Jupyter Notebook与日志服务玩转超大规模数据分析与可视化
IPython/Jupyter Notebook非常流行,但随着数据量越来越大(例如几百亿条电商平台访问日志),如何继续保持灵活的交互式分析,是一个挑战。阿里云日志服务作为阿里商业操作系统的智能运维平台,无需开发就能快捷完成海量日志数据的采集、消费、投递以及查询分析等功能。
|
Web App开发 JSON 监控
日志服务IPython/Jupyter扩展实战:下载数据为Excel文件
想要将日志服务的日志下载并保存为Excel或者CSV格式,并且自动处理字段不一致的情况的话,该怎么办?通过使用日志服务IPython/Jupyter扩展,轻松做到这点。
4363 0
|
分布式计算 监控 Shell
新功能:日志服务IPython/Jupyter Notebook扩展发布
日志服务发布IPython/Jupyter Notebook扩展,可以轻松地使用Python对海量数据进行深度加工(ETL)、交互式分析(通过SQL、DataFrame)、机器学习与可视化等。
4010 0