本项目为AI全栈课程《Python从小白到精通》的配套补充,期望大家熟悉基本notebook开发环境后,掌握一些高阶的Notebook使用技巧,提高学习和工作效率。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

Jupyter notebook(Jupyter lab)是aistudio实训平台默认的开发工具,同时也数据科学从业者最常使用的工具。相比vs code, pycharm等IDE(集成开发环境), notebook足够简单,可以让你把所有的精力放在数据挖掘探索和算法开发验证上。其代码结构是以单元格来组织和分割,一个单元格可以有一行或者若干行代码,完成一个单元格后可以立马运行查看中间结果并以及可视化,这种方式的方式的优点显而易见:
代码组织和运行方式非常符合数据科学工作者的思维习惯。可以一边做,然后根据运行的中间结果在思考下一步的操作。面对一个复杂问题时,往往需要多次分步骤尝试。而在IDE里面,每次要想知道中间结果需要打断点调试,每次都需要从头运行,没有notebook方便
配合markdown笔记,可以做出媲美教科书的效果
因为其易用和可扩展,现在很多大学都用来作上课的课件工具。另外你写完的notebook也可以发给别人(.ipynb文件),在Anaconda中可以直接运行,配合markdown可以完美再现你的思维过程! 本项目为AI全栈课程《Python从小白到精通》的配套补充,期望大家熟悉基本notebook开发环境后,掌握一些高阶的Notebook使用技巧,提高学习和工作效率。主要包括以下内容:
同时可以点击帮助-》快捷键列表查看所有内置的快捷键

Notebook正常情况下,只有最后一行的代码的结果会输出, 如下我们读取一个文件到df中,写了df.shape, df.columns和df.head,只有最后一行的df.head结果会输出,例如:
import pandas as pd
df = pd.read_csv('demo.csv')
df.shape # 正常情况下,因为此代码不是该单元格最后一行,此结果不会输出df.columns # 正常情况下,因为此代码不是该单元格最后一行,此结果不会输出df.head() # 只会输出该代码的结果EQP_ID MODEL_Type CURR_TEMPERATURE 0 RS4 QMO_2DCP1 32.9 1 RS4 QMO_2DCP1 32.4 2 RS4 QMO_2DCP1 33.0 3 RS4 QMO_2DCP1 32.9 4 RS4 QMO_2DCP1 32.9
# 如果想把其他有结果也输出,只要增加一个单元格运行下面两句代码即可:""" from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = "all" """
'\nfrom IPython.core.interactiveshell import InteractiveShell \nInteractiveShell.ast_node_interactivity = "all"\n'
# 尝试一个单元格多个输出from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = "all"df.head() df.tail() df.shape df['EQP_ID'].value_counts()
EQP_ID MODEL_Type CURR_TEMPERATURE 0 RS4 QMO_2DCP1 32.9 1 RS4 QMO_2DCP1 32.4 2 RS4 QMO_2DCP1 33.0 3 RS4 QMO_2DCP1 32.9 4 RS4 QMO_2DCP1 32.9
EQP_ID MODEL_Type CURR_TEMPERATURE 377 A6L QMO_2DCP1 174.2 378 A6L QMO_2DCP1 171.6 379 A6L QMO_2DCP1 169.2 380 A6L QMO_2DCP1 166.8 381 A6L QMO_2DCP1 164.5
(382, 3)
A6L 132 RS6 111 Q5L 98 RS4 41 Name: EQP_ID, dtype: int64
# 先使用cat命令查看,在notebook中执行只要在命令前加!即可print("demo脚本内容如下:")
!cat demo.pydemo脚本内容如下:
print("this is a demo")
print('------' * 10)
import os
print('当前的目录为:')
print(os.getcwd())
print('------' * 10)
print('当前目录下的文件:')
print(os.listdir())
print('------' * 10)# 使用!python命令执行该脚本!python demo.py
this is a demo ------------------------------------------------------------ 当前的目录为: /home/aistudio ------------------------------------------------------------ 当前目录下的文件: ['data', 'demo.py', '.data', 'demo.csv', '.node.started', '.ipython', '.systemlogs', '.jupyter', '.cache', '.yarn', '.aistudiofs.download.success', '.codelab-jupyter.log', '.bash_history', '.ipynb_checkpoints', '.local', '.bashrc', '.aistudiofs', 'machine.csv', 'main.ipynb', '.conda', '.vscode.log', '.python_history', '.bash_logout', '.viminfo', '.virtual_documents', '.ssh', '.aistudiofs.success', '.homedata.success', '.dataset.download', '.pip', '.npm', '.profile', '.config', '.webide'] ------------------------------------------------------------
!pwd # 查看当前目录!ls # 显示当前目录下得文件和目录
/home/aistudio data demo.csv demo.py machine.csv main.ipynb
# 直接在单元格内使用pip命令安装相关的库,不需要切换到终端去安装!pip install pandas
通过上面得介绍可以发现在notebook中可以方便的执行shell命令,熟练使用可以省去和终端来回切换,提高工作效率。 下面介绍一些在开发中高频使用的shell命令。
这个有点特殊,在指令cd前加“%”,而不是“!”号。 这点需要注意
!pwd # 显示当前目录# 切换目录,可以使用相对路径或绝对路径。推荐使用绝对路径%cd /home/aistudio/work !pwd # 显示切换之后的目录
/home/aistudio [Errno 2] No such file or directory: '/home/aistudio/work' /home/aistudio /home/aistudio
# 查看当前系统进程信息!ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND aistudio 1 0.0 0.0 4528 728 ? Ss 10:07 0:00 /bin/sh -c /o aistudio 7 0.0 0.0 13312 3352 ? S 10:07 0:00 /bin/bash /op root 33 0.0 0.0 65536 3152 ? Ss 10:07 0:00 /usr/sbin/ssh aistudio 44 0.0 0.0 13324 3428 ? S 10:07 0:00 bash /opt/con aistudio 46 1.0 0.0 41645240 147244 ? Sl 10:07 2:40 /opt/conda/en aistudio 100 0.0 0.0 4528 788 ? S 10:07 0:00 /bin/sh -c pl aistudio 101 1.7 0.0 5758164 89156 ? Sl 10:07 4:17 /opt/conda/en aistudio 130 0.0 0.0 26236 11184 ? S 10:07 0:00 /opt/conda/en aistudio 131 0.0 0.0 225112 47484 ? Sl 10:07 0:01 /opt/conda/en aistudio 138 1.2 0.0 152228 48060 ? Sl 10:07 3:06 /opt/conda/en aistudio 142 1.2 0.0 152228 48120 ? Sl 10:07 3:07 /opt/conda/en aistudio 264 1.7 0.0 3132680 224776 ? Sl 10:08 4:18 /opt/conda/en aistudio 269 0.2 0.1 658804 345120 ? Sl 10:08 0:40 /opt/conda/en aistudio 271 0.0 0.0 52864 26952 ? S 10:08 0:01 /opt/conda/en aistudio 273 0.0 0.0 42996 24368 ? S 10:08 0:00 /opt/conda/en aistudio 275 0.0 0.0 171708 49880 ? Sl 10:08 0:06 /opt/conda/en aistudio 26729 0.0 0.0 42280 24256 ? S 14:02 0:00 /opt/conda/en aistudio 26787 0.1 0.0 2889464 93768 ? Sl 14:02 0:01 /opt/conda/en aistudio 27558 0.6 0.0 3098712 103336 ? Ssl 14:07 0:02 /opt/conda/en aistudio 28175 0.0 0.0 37852 3404 pts/0 Rs+ 14:13 0:00 ps aux
%cd /home/aistudio/ !ps aux > systemid.txt
/home/aistudio
可以看到使用重定向命令后,屏幕不在有输出。相应的在/home/aistudio/目录下回出现一个systemid.txt文件,内容和上面得一样
主要有cat, head, tail等命令。这些在深度学习项目中查看一些标注文件会用得比较多。通过查看一个标注文件得前5行可以大概了解文件和标签的结构。 这里举例使用head查看一个文件得前n行。其他命令可以自行尝试。
%cd /home/aistudio/ !head -5 systemid.txt
/home/aistudio USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND aistudio 1 0.0 0.0 4528 728 ? Ss 10:07 0:00 /bin/sh -c /opt/conda/include/container-start.sh aistudio 7 0.0 0.0 13312 3352 ? S 10:07 0:00 /bin/bash /opt/conda/include/container-start.sh root 33 0.0 0.0 65536 3152 ? Ss 10:07 0:00 /usr/sbin/sshd aistudio 44 0.0 0.0 13324 3428 ? S 10:07 0:00 bash /opt/conda/envs/webide/bin/kernel_seperation_start.sh
可以指定 -L n用于显示目录的层级。 另外还有cp, mv, rm等文件的拷贝删除等操作。这里不在赘述。
!tree -L 2 /home/aistudio/
/home/aistudio/ ├── data ├── demo.csv ├── demo.py ├── machine.csv ├── main.ipynb └── systemid.txt 1 directory, 5 files
在MATLAB中,直接有一个窗口可以显示当前所有的变量及其相应的值,这个功能非常强大,Anaconda中Spyder也有类似的功能。 其实notebook也可以,直接使用%who和%whos魔法命令即可。其中:
# 先创建一些变量a, b, c = 10, 20.5, 'zhangsan'd = {'Name' : 'PaddlePaddle', 'Age': 18}
e = ['cat', 'dog']# 显示当前环境中的所有变量%who
InteractiveShell a b c d df e pd
# 显示当前环境中的所有变量、类型和值%whos
Variable Type Data/Info --------------------------------------------- InteractiveShell MetaHasTraits <class 'IPython.core.inte<...>eshell.InteractiveShell'> a int 10 b float 20.5 c str zhangsan d dict n=2 df DataFrame EQP_ID MODEL_Type CU<...>n\n[382 rows x 3 columns] e list n=2 pd module <module 'pandas' from '/o<...>ages/pandas/__init__.py'>
这个比较常用。主要有time,和timeit两个魔法命令,在结合行模式和单元格模式就有4种用法,具体如下:
所以time只统计执行一次的时间;而timeit会反复执行很多次统计平均时间,这个比只执行一次要准确一点
%time [i for i in range(100) if i%7 == 0]
CPU times: user 0 ns, sys: 10 µs, total: 10 µs Wall time: 14.1 µs
[0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]
%timeit [i for i in range(100) if i%7 == 0]
4.75 µs ± 40.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
# 先定义两个二维数组import numpy as np m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] n = [[2, 2, 2], [3, 3, 3], [4, 4, 4]] m_, n_ = np.array(m), np.array(n) m_,n_ m_.shape
%%timeitdef array_add_forloop():
res = np.zeros((3,3)) for row in range(m_.shape[0]): for col in range(m_.shape[1]):
res[row,col] = int(m_[row,col] + n_[row,col]) return res
array_add_forloop()%%timeitdef array_add_matrix():
return m_+ n_
array_add_matrix()在Notebook中,matplotlib是最常用的绘图库,并且Notebook中有相应的魔法命令用于辅助绘图功能:
下面举例说明
# %matplotlib inline省略plt.show, 可以加在最前面的单元格,全局有效import numpy as npimport matplotlib.pyplot as plt %matplotlib inline x = np.linspace(-10,10,100) y = np.sin(x) plt.plot(x, y)
# 加上";"抑制其他输出import numpy as npimport matplotlib.pyplot as plt %matplotlib inline x = np.linspace(-10,10,100) y = np.sin(x) plt.plot(x, y);
from tqdm import tqdmimport timefor i in tqdm(range(100)): # 此处写业务逻辑
time.sleep(0.1)# 默认情况下np会显示小数点后8位import numpy as np np.random.rand(2,2)
array([[0.69865144, 0.55332125],
[0.97711927, 0.15941402]])# 设置成显示2位, 设置后全局有效import numpy as np np.set_printoptions(precision=2) np.random.rand(2,2)
array([[0.45, 0.68],
[0.01, 0.2 ]])在notebook中,当文件的行和列过多时,pandas默认不显示所有的信息。此时可以使用如下,命令显示:
%cd /home/aistudio/import pandas as pd
df_machine = pd.read_csv("machine.csv")
df_machine/home/aistudio
Time Step_Number wafer_names fault_name lotid lot_id \
0 11.9460 4 l29011.txm calibration lot011 lot011
1 13.0280 4 l29011.txm calibration lot011 lot011
2 14.0490 4 l29011.txm calibration lot011 lot011
3 15.1329 4 l29011.txm calibration lot011 lot011
4 16.1390 4 l29011.txm calibration lot011 lot011
... ... ... ... ... ... ...
10765 104.6880 5 l33431.txm calibration lot061 lot061
10766 105.6870 5 l33431.txm calibration lot061 lot061
10767 106.7190 5 l33431.txm calibration lot061 lot061
10768 107.7500 5 l33431.txm calibration lot061 lot061
10769 108.7660 5 l33431.txm calibration lot061 lot061
recipe 151821807 151820951 151821601 151821602 151821603 \
0 recipe1 4 751 753 132 0
1 recipe1 4 751 753 134 0
2 recipe1 4 751 755 134 0
3 recipe1 4 751 753 133 0
4 recipe1 4 751 754 132 0
... ... ... ... ... ... ...
10765 recipe1 5 752 753 131 0
10766 recipe1 5 752 754 134 0
10767 recipe1 5 752 754 132 0
10768 recipe1 5 752 754 132 0
10769 recipe1 5 751 752 132 0
151821604 151822195 151822196 151822197 151822198 151822215 \
0 626 100 1227 9408 9019 -362
1 620 99 1229 9431 9029 -1455
2 599 102 1221 9389 9114 -1056
3 586 100 1201 9445 9031 -587
4 587 102 1182 9456 9043 -124
... ... ... ... ... ... ...
10765 538 102 1226 9365 8973 1161
10766 534 102 1227 9426 8934 606
10767 531 102 1227 9357 8937 1249
10768 534 100 1227 9434 8920 1155
10769 529 101 1226 9346 8990 799
151822252 151822253 151822254 151822255 151821769 151821770 \
0 26 16599 20028 -296 16848 360
1 26 16568 20042 -676 16796 350
2 25 16442 20146 -291 16512 344
3 25 16960 20148 -262 17020 352
4 25 16564 20226 -547 16440 346
... ... ... ... ... ... ...
10765 28 16920 19110 7 16464 346
10766 28 16640 18982 134 16658 346
10767 28 16593 19056 -34 16714 348
10768 28 16522 19046 139 16560 353
10769 28 16815 19114 72 16630 355
151821788 151821789 151821797
0 0 27594 49
1 0 27440 49
2 0 27276 49
3 0 27330 50
4 0 27262 50
... ... ... ...
10765 0 28446 51
10766 0 28444 51
10767 0 28230 51
10768 0 28278 51
10769 0 28240 51
[10770 rows x 27 columns]# 这里举例显示所有的列,其他可以自行尝试pd.set_option('display.max_columns', None)
df_machine.head()Time Step_Number wafer_names fault_name lotid lot_id recipe \ 0 11.9460 4 l29011.txm calibration lot011 lot011 recipe1 1 13.0280 4 l29011.txm calibration lot011 lot011 recipe1 2 14.0490 4 l29011.txm calibration lot011 lot011 recipe1 3 15.1329 4 l29011.txm calibration lot011 lot011 recipe1 4 16.1390 4 l29011.txm calibration lot011 lot011 recipe1 151821807 151820951 151821601 151821602 151821603 151821604 \ 0 4 751 753 132 0 626 1 4 751 753 134 0 620 2 4 751 755 134 0 599 3 4 751 753 133 0 586 4 4 751 754 132 0 587 151822195 151822196 151822197 151822198 151822215 151822252 \ 0 100 1227 9408 9019 -362 26 1 99 1229 9431 9029 -1455 26 2 102 1221 9389 9114 -1056 25 3 100 1201 9445 9031 -587 25 4 102 1182 9456 9043 -124 25 151822253 151822254 151822255 151821769 151821770 151821788 \ 0 16599 20028 -296 16848 360 0 1 16568 20042 -676 16796 350 0 2 16442 20146 -291 16512 344 0 3 16960 20148 -262 17020 352 0 4 16564 20226 -547 16440 346 0 151821789 151821797 0 27594 49 1 27440 49 2 27276 49 3 27330 50 4 27262 50
<br/>
以上就是N多技巧让你的Notebook起飞的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号