分享

大邓强力推荐-jupyter notebook使用小技巧

 大邓的Python 2021-02-23

1. 快捷键

在jupyter notebook菜单栏有Help按钮,可以查看jupyter的快捷键

2. 将多个变量输出

一般jupyter notebook默认只打印最后一个变量的结果。比如

  1. from pydataset import data

  2. quakes = data('quakes')

  3. quakes.head(10) #前10行数据

  4. quakes.tail(3)  #后3行数据

通过设置InteractiveShell.astnodeinteractivity参数为all,就可以让所有的变量或者声明都能显示出来

  1. from IPython.core.interactiveshell import InteractiveShell

  2. InteractiveShell.ast_node_interactivity = 'all'

  1. from pydataset import data

  2. quakes = data('quakes')

  3. quakes.head(10) #前10行数据

  4. quakes.tail(3)  #后3行数据

3. 问号?

除了Help菜单能让我们快读查看numpy、pandas、scipy和matplotlib库,其实在cell中使用 ?可以查看库、函数、方法和变量的信息。

  1. #查看库的信息

  2. import os

  3. ?os

  1. #查看函数信息

  2. ?print()


  1. #查看变量信息

  2. a = [1,2,3,4]

  3. ?a


4. 在notebook中画图

作图最常用的就是matplotlib,记得在cell中写上这句

  1. %matplotlib inline

  1. %matplotlib inline

  2. import pandas as pd

  3. series = pd.Series([1,3,5,6,2])

  4. series.plot(kind='pie')

5. IPython魔法命令

查看当前工作目录

  1. %pwd

执行上面的代码,得到

  1. '/Users/suosuo/Desktop/20180820 jupyter notebook技巧'

更改当前工作目录

  1. #更改当前工作目录

  2. %cd /Users/suosuo/Desktop

查看目录文件列表

  1. #查看目录文件列表

  2. %ls /Users/suosuo/Desktop/用python文本分析

执行上面的代码,得到

  1.    01-configuration.zip  

  2.    02-base.zip

  3.    03-crawler.zip    

  4.    04-textprocess.zip  

  5.    05-textprocess.zip

写入文件

  1. #写入文件,向test.py中写入print('测试%%writefile魔法')

  2. %%writefile test.py

  3. print('测试%%writefile魔法')

执行上面的代码,得到

  1.    Writing test.py

运行脚本

  1. #运行脚本

  2. %run test.py

执行上面的代码,得到

  1.    测试%%writefile魔法

查看当前变量

  1. #查看当前变量

  2. a = 1

  3. b = [1,2,3,4]

  4. %whos

执行上面的代码,得到

  1.    Variable   Type        Data/Info

  2.    --------------------------------

  3.    a          int         1

  4.    b          list        n=4

  5.    pd         module      <module 'pandas' from '/L<...>ages/pandas/__init__.py'>

  6.    s          NoneType    None

  7.    series     Series      0    1\n1    3\n2    5\n3<...>  6\n4    2\ndtype: int64

清除全部变量

  1. #清除全部变量

  2. a = 1

  3. b = [1,2,3,4]

  4. %reset

执行上面的代码,得到

  1. Once deleted, variables cannot be recovered. Proceed (y/[n])? y

测试单行运行时间

  1. #测试单行运行时间

  2. %timeit x = [i**2 for i in range(10000)]

  3. %timeit y = [i**2 for i in x]

执行上面的代码,得到

  1. 5.16 ms ± 215 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

  2. 4.82 ms ± 190 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

6. 执行shell命令

命令行的命令前面加个 !即可在notebook中进行。

比如我们想要安装jieba库,需要打开终端输入

  1. pip3 install jieba

现在,我们可以在notebook中输入下面命令安装jieba

  1. !pip3 install jieba

  1. Collecting jieba

  2. [?25l  Downloading https://files.pythonhosted.org/packages/71/46/c6f9179f73b818d5827202ad1c4a94e371a29473b7f043b736b4dab6b8cd/jieba-0.39.zip (7.3MB)

  3. [K    100% |████████████████████████████████| 7.3MB 284kB/s ta 0:00:01

  4. [?25hInstalling collected packages: jieba

  5.  Running setup.py install for jieba ... [?25ldone

  6. [?25hSuccessfully installed jieba-0.39

7. markdown标记语言

markdown语法作用
#有几个#就是几级标题
**两对**夹住的内容变为斜体
-无序列表

一级标题

  1. # 一级标题

二级标题

  1. ## 二级标题

三级标题

  1. ### 三级标题


有序列表

  1. 元素1

  2. 元素2

  3. 元素3

  1. 有序列表

  2. 1. 元素1

  3. 2. 元素2

  4. 3. 元素3


无序列表

  • 元素1

  • 元素2

  • 元素3

  1. 无序列表

  2. - 元素1

  3. - 元素2

  4. - 元素3


函数作用
print()打印
help()查看帮助文档
  1. |函数|作用|

  2. |---|---|

  3. |print()|打印|

  4. |help()|查看帮助文档|

8. 使用LaTex写公式

当我们在markdown编辑模式下输入

  1. $P(A|B)=\frac{P(B|A)P(A)}{P(B)}$

会被MathJax渲染成

  1. import requests

  2. ?requests.get()


9. 为jupyter扩展插件

执行下面操作

  1. !pip3 install jupyter_contrib_nbextensions

  2. !jupyter contrib nbextension install

  3. !jupyter_contrib_nbextensions

我们的jupyter notebook发生的了变化,如下图所示,多了nbextensions

而在.ipynb文件中增加了下图的这个按钮,点击该按钮我们就可以使用jupyter的展示功能(浏览器PPT功能)

  1. !pip3 install jupyter_contrib_nbextensions

  2. !jupyter contrib nbextension install

  3. !jupyter_contrib_nbextensions

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多