jupyter ppt

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

1. 快捷键

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

jupyter ppt_第1张图片
image

2. 将多个变量输出

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

from pydataset import data
quakes = data('quakes')
quakes.head(10) #前10行数据
quakes.tail(3) #后3行数据

jupyter ppt_第2张图片
image

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

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'

from pydataset import data
quakes = data('quakes')
quakes.head(10) #前10行数据
quakes.tail(3) #后3行数据

jupyter ppt_第3张图片
image

3. 问号?

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

查看库的信息

import os
?os

jupyter ppt_第4张图片
image

查看函数信息

?print()

jupyter ppt_第5张图片
image

查看变量信息

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

jupyter ppt_第6张图片
image

4. 在notebook中画图

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

%matplotlib inline
import pandas as pd
series = pd.Series([1,3,5,6,2])
series.plot(kind='pie')

jupyter ppt_第7张图片
image

5. IPython魔法命令

jupyter ppt_第8张图片
image

查看当前工作目录

%pwd

执行上面的代码,得到

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

更改当前工作目录

%cd /Users/suosuo/Desktop

查看目录文件列表

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

写入文件

向test.py中写入print('测试%%writefile魔法')`
%%writefile test.py
print('测试%%writefile魔法')

运行脚本

%run test.py

查看当前变量

a = 1
b = [1,2,3,4]
%whos

清除全部变量

a = 1
b = [1,2,3,4]
%reset

测试单行运行时间

%timeit x = [i2 for i in range(10000)]
%timeit y = [i
2 for i in x]

6. 执行shell命令

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

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

pip3 install jieba

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

!pip3 install jieba

7. markdown标记语言

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

一级标题

二级标题

三级标题


有序列表

  1. 元素1

  2. 元素2

  3. 元素3


无序列表

  • 元素1

  • 元素2

  • 元素3


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

8. 使用LaTex写公式

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

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

会被MathJax渲染成

jupyter ppt_第9张图片
image

import requests
?requests.get()

9. 为jupyter扩展插件

执行下面操作

!pip3 install jupyter_contrib_nbextensions
!jupyter contrib nbextension install
!jupyter_contrib_nbextensions

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

jupyter ppt_第10张图片
image
jupyter ppt_第11张图片
image

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

jupyter ppt_第12张图片
image
image
  1. !pip3 install jupyter_contrib_nbextensions
  2. !jupyter contrib nbextension install
  3. !jupyter_contrib_nbextensions

你可能感兴趣的:(jupyter ppt)