Python3玩转儿 机器学习(4),python3,jupyternot


jupyternotebook 的使用方法¶

最基本的使用¶

In[1]:
1+2
Out[1]:
3

菜单树¶

File¶
  |------> New Notebook ----> Python3          新建一个 Notebook      |----> Open                                  打开一个 Notebook  |----> Make a Copy...                        拷贝一个 Notebook  |----> Rename                                重命名  |----> Save and Checkpoint                   保存  |----> Revert to Checkpoint->时间            恢复  |----> Print Preview                         打印预览  |----> Download as ----> Notebook(.ipynb)    把当前文件以.ipynb后缀的文件下载                      |--> Python(.py)         把当前文件以.py   后缀的文件下载                      |--> HTML(.html)         把当前文件以.html 后缀的文件下载                      |--> Markdown(.md)       把当前文件以.md   后缀的文件下载                      |--> reST(.rst)          把当前文件以.rst  后缀的文件下载                      |--> LaTex(.tex)         把当前文件以.tex  后缀的文件下载                      |--> PDF via LaTeX(.pdf) 把当前文件以.pdf  后缀的文件下载
Edit¶
  |-----> Cut Cells                            剪切一个单元  |---> Copy Cells                             拷贝一个单元  |---> Paste Cells Above                      在前面粘贴一个单元  |---> Paste Cells Below                      在后面粘贴一个单元  |---> Paste Cells & Replace                  粘贴和替换一个单元  |---> Delete Cells                           删除一个单元格  |---> Undo Delete Cells                      撤销删除一个单元  |---> Split Cell                             分开单元  |---> Merge Cell Above                       融合前面的单元  |---> Merge Cell Below                       融合后面的单元  |---> Move Cell Up                           将单元向上移动  |---> Move Cell Down                         将单元向下移动  |---> Edit Notebook Metadata                 编辑 Notebook的元数据  |---> Find and Replace                       发现并替换  |---> Cut Cell Attachments                   剪切单元连接  |---> Paste Cell Attachments                 粘贴单元连接  |---> Insert Image                           插入一张图片
View¶
  |-----> Toggle Header                        切换头  |---> Toggle Toolbar                         切换工具条  |---> Toggle Line Numbers                    切换行编号  |---> Cell Toolbar ----> None                单元工具条                      |--> Edit Metadata       编辑元数据                      |--> Raw Cell Format     原始单元格式                      |--> Slideshow           下滑显示                            |--> Attachments         附着                      |--> Tags                标记
Insert¶
    |-----> Insert Cell Above                 在前面插入单元    |---> Insert Cell Below                   在后面插入单元
Cell¶
  |-----> Run Cells                           运行单元  |---> Run Cells and Select Below            运行后面一个单元  |---> Run Cells and Insert Below            运行前面一个单元  |---> Run All                               运行所有单元  |---> Run All Above                         运行前面所有单元  |---> Cell Type ----> Code                  切换单元为Code类型  |               |---> Markdown              切换单元为Markdown类型  |               |---> Raw NBConvert         切换单元为Raw NBConvert类型  |                  |---> Current Outputs -----> Toggle           切换  |                      |---> Toggle Scrolling 切换上下换行  |                      |---> Clear            清除  |                        |--->All Output -----> ----> Toggle           切换                         |---> Toggle Scrolling 切换上下换行                         |---> Clear            清除
Kernel¶
    |-----> Interrupt                         中断    |---> Restart                             重启    |---> Restart & Clear Output              重启并清除输出    |---> Restart & Run All                   重启并运行所有    |---> Reconnect                           重连    |---> Shutdown                            关闭    |---> Change kernel -----> Python3        清除核心
Widgets¶
     |-----> Save Notebook Widget State       保存Notebook窗口状态     |---> Clear Notebook Widget State        清除Notebook窗口状态     |---> Download Widget State              下载Notebook窗口状态     |---> Embed Widgets                      嵌入窗口
Help¶
  |-----> User Interface Tour                 使用游客接口  |---> Keyboard Shortcuts                    键盘快捷方式  |---> Edit Keyboard Shortcuts               编辑键盘快捷方式  |---> Notebook Help                         Notebook帮助  |---> Markdown                              Markdown帮助  |---> Python Reference                      Python参考  |---> IPython Reference                     IPython参考  |---> NumPy Reference                       NumPy参考  |---> SciPy Reference                       SciPy参考  |---> Matplotlib Teference                  Matplotlib参考  |---> SymPy Reference                       SymPy参考  |---> pandas Reference                      pandas参考  |---> About                                 关于

2、Jupyter Notebook 高级 - 魔法命令¶

2.1 调用python模块¶

%run+模块路径 ,例如:¶

In[1]:
%run testmodule.py
hello tale
In[2]:
hello("ANANANA")
hello ANANANA
In[3]:
import mymodule.first
helloModule tale
In[4]:
mymodule.first.helloModule("ttttt")
helloModule ttttt

2.2 测量代码时间¶

%timeit 测量单行代码的运行时间¶

In[5]:
%timeit L =[i**2 for i in range(1000)]
350 µs ± 6.48 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In[6]:
%timeit L =[i**2 for i in range(1000000)]
379 ms ± 8.14 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In[7]:
%timeit L =[i**2 for i in range(10)]
3.52 µs ± 60.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%%timeit 可以测量多行代码的运行时间¶

In[8]:
%%timeitL = []for n in range(1000):    L.append(n**2)
392 µs ± 7.08 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%time 测量单次代码的运行时间¶

In[17]:
%time L=[i**2 for i in range(1000)]
Wall time: 532 µs
In[25]:
%timeL = []for n in range(1000):    L.append(n ** 2)
Wall time: 0 ns
In[27]:
import randomL = [random.random() for i in range(100000)]%timeit L.sort()
1.73 ms ± 104 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In[29]:
L = [random.random() for i in range(100000)]%time L.sort()
Wall time: 39.1 ms
In[30]:
%time L.sort() 
Wall time: 1.99 ms

对于乱序的数据,sort()运行的时间更长,对于已经排好了的数据,sort()运行的时间更短。

其他的魔法命令¶

In[31]:
%lsmagic  #列出所有的魔法命令
Out[31]:
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  %xmodeAvailable 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  %%writefileAutomagic is ON, % prefix IS NOT needed for line magics.

使用 命令?的格式查询魔法命令的使用方法¶

例如: %run?

Python3玩转儿 机器学习(4)

评论关闭