大鄧強力推薦-jupyter notebook使用小技巧

1. 快捷鍵

在jupyter notebook菜單欄有Help按鈕,能夠查看jupyter的快捷鍵node

2. 將多個變量輸出

通常jupyter notebook默認只打印最後一個變量的結果。好比python

  
  
   
   
            
   
   
  1. web

  2. shell

  3. 瀏覽器

  4. 微信

from pydataset import dataquakes = data('quakes')quakes.head(10) #前10行數據quakes.tail(3)  #後3行數據

經過設置InteractiveShell.astnodeinteractivity參數爲all,就可讓全部的變量或者聲明都能顯示出來
markdown

  
  
   
   
            
   
   
  1. app

  2. 機器學習

from IPython.core.interactiveshell import InteractiveShellInteractiveShell.ast_node_interactivity = 'all'
  
  
   
   
            
   
   
  1. 函數

from pydataset import dataquakes = data('quakes')quakes.head(10) #前10行數據quakes.tail(3)  #後3行數據

3. 問號?

除了Help菜單能讓咱們快讀查看numpy、pandas、scipy和matplotlib庫,其實在cell中使用 ?能夠查看庫、函數、方法和變量的信息。

  
  
   
   
            
   
   
#查看庫的信息import os?os

  
  
   
   
            
   
   

#查看函數信息?print()

  
  
   
   
            
   
   

#查看變量信息a = [1,2,3,4]?a

4. 在notebook中畫圖

做圖最經常使用的就是matplotlib,記得在cell中寫上這句

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

5. IPython魔法命令

查看當前工做目錄

  
  
   
   
            
   
   
%pwd

執行上面的代碼,獲得

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

更改當前工做目錄

  
  
   
   
            
   
   
#更改當前工做目錄%cd /Users/suosuo/Desktop

查看目錄文件列表

  
  
   
   
            
   
   
#查看目錄文件列表%ls /Users/suosuo/Desktop/用python文本分析

執行上面的代碼,獲得

  
  
   
   
            
   
   
   01-configuration.zip      02-base.zip    03-crawler.zip        04-textprocess.zip      05-textprocess.zip

寫入文件

  
  
   
   
            
   
   
#寫入文件,向test.py中寫入print('測試%%writefile魔法')%%writefile test.pyprint('測試%%writefile魔法')

執行上面的代碼,獲得

  
  
   
   
            
   
   
   Writing test.py

運行腳本

  
  
   
   
            
   
   
#運行腳本%run test.py

執行上面的代碼,獲得

  
  
   
   
            
   
   
   測試%%writefile魔法

查看當前變量

  
  
   
   
            
   
   
#查看當前變量a = 1b = [1,2,3,4]%whos

執行上面的代碼,獲得

  
  
   
   
            
   
   
   Variable   Type        Data/Info    --------------------------------    a          int         1    b          list        n=4    pd         module      <module 'pandas' from '/L<...>ages/pandas/__init__.py'>    s          NoneType    None    series     Series      0    1\n1    3\n2    5\n3<...>  6\n4    2\ndtype: int64

清除所有變量

  
  
   
   
            
   
   
#清除所有變量a = 1b = [1,2,3,4]%reset

執行上面的代碼,獲得

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

測試單行運行時間

  
  
   
   
            
   
   
#測試單行運行時間%timeit x = [i**2 for i in range(10000)]%timeit y = [i**2 for i in x]

執行上面的代碼,獲得

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

6. 執行shell命令

命令行的命令前面加個 !便可在notebook中進行。

好比咱們想要安裝jieba庫,須要打開終端輸入

  
  
   
   
            
   
   
pip3 install jieba

如今,咱們能夠在notebook中輸入下面命令安裝jieba

  
  
   
   
            
   
   
!pip3 install jieba
  
  
   
   
            
   
   
Collecting jieba[?25l  Downloading https://files.pythonhosted.org/packages/71/46/c6f9179f73b818d5827202ad1c4a94e371a29473b7f043b736b4dab6b8cd/jieba-0.39.zip (7.3MB)[K    100% |████████████████████████████████| 7.3MB 284kB/s ta 0:00:01[?25hInstalling collected packages: jieba  Running setup.py install for jieba ... [?25ldone[?25hSuccessfully installed jieba-0.39

7. markdown標記語言

markdown語法 做用
# 有幾個#就是幾級標題
** 兩對**夾住的內容變爲斜體
- 無序列表

一級標題

  
  
   
   
            
   
   
# 一級標題

二級標題

  
  
   
   
            
   
   
## 二級標題

三級標題

  
  
   
   
            
   
   
### 三級標題

有序列表

  1. 元素1

  2. 元素2

  3. 元素3

  
  
   
   
            
   
   
有序列表1. 元素12. 元素23. 元素3

無序列表

  • 元素1

  • 元素2

  • 元素3

  
  
   
   
            
   
   
無序列表- 元素1- 元素2- 元素3

函數 做用
print() 打印
help() 查看幫助文檔
  
  
   
   
            
   
   
|函數|做用||---|---||print()|打印||help()|查看幫助文檔|

8. 使用LaTex寫公式

當咱們在markdown編輯模式下輸入

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

會被MathJax渲染成

  
  
   
   
            
   
   

import requests?requests.get()

9. 爲jupyter擴展插件

執行下面操做

  
  
   
   
            
   
   
!pip3 install jupyter_contrib_nbextensions!jupyter contrib nbextension install !jupyter_contrib_nbextensions

咱們的jupyter notebook發生的了變化,以下圖所示,多了nbextensions

而在.ipynb文件中增長了下圖的這個按鈕,點擊該按鈕咱們就可使用jupyter的展現功能(瀏覽器PPT功能)

  
  
   
   
            
   
   

!pip3 install jupyter_contrib_nbextensions!jupyter contrib nbextension install !jupyter_contrib_nbextensions


往期文章

100G Python學習資料:從入門到精通! 免費下載    

上百G文本數據集等你來認領|免費領取  

Python 3.7中dataclass的終極指南(一) 

Python 3.7中dataclass的終極指南(二)

2017年度15個最好的數據科學領域Python庫    

使用Pandas更好的作數據科學

推薦系統與協同過濾、奇異值分解

機器學習之使用邏輯迴歸識別圖片中的數字

應用PCA降維加速模型訓練

使用sklearn作天然語言處理-1 

使用sklearn作天然語言處理-2

機器學習|八大步驟解決90%的NLP問題    

Python圈中的符號計算庫-Sympy

Python中處理日期時間庫的使用方法 

如何從文本中提取特徵信息? 

視頻講解】Scrapy遞歸抓取簡書用戶信息

美團商家信息採集神器 

用chardect庫解決網頁亂碼問題



本文分享自微信公衆號 - 大鄧和他的Python(DaDengAndHisPython)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索