基础常用命令
切换为markdown:Esc+M 长按ESC
切换为Code:Esc+Y
删除选中的单元格:Esc+D
剪切:Esc+X
复制:Esc+C
粘贴:Esc+V
执行代码并插入下一行:Shift/Alt+Enter
修改notebook的保存路径方法
(1)打开Anaconda Prompt运行这句代码 jupyter notebook --generate-config
(2)之后cmd会返回一个文件(含有详细的路径)
(3)找到这个py文件,用pycharm打开,修改“c.NotebookApp.notebook_dir = ‘D:/python/Projects’”
(4)找到对应的“Jupyte Notebook”快捷图标,鼠标右击 – 属性 – 目标,去掉后面的 “%USERPROFILE%/”(很重要)
(5)重启
高级语法
%run
# 这个指令就是调用pyCharm中写好代码
%run first/first.py
hello("renjie")
hello, renjie
# 更常用的其实是“在pyCharm中新建一个【_init_.py】的py文件
# 目的是让所有的开放脚本形成一个模块”
# 然后直接使用【import first.first】或【from first import first】
import first.first
hello("machine learning")
hello, machine learning
%timeit 测试性能
# 自动运行1000次,最快7次,选取最快的平均值308±13.3微秒
%timeit L = [i**2 for i in range(1000)]
308 µs ± 13.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%%timeit
# 一个%只能操控一句代码,俩个%则可以运行一段代码,且必须在代码第一行
L = [] #申请一个空List集合
for i in range(1000):
L.append(i ** 2)
339 µs ± 16.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%time / %%time 仅运行一次
%time L = [i**2 for i in range(100000)]
Wall time: 32.7 s
import random
L = [random.random() for i in range(1000000)]
%timeit L.sort()
30.5 ms ± 1.56 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit L.sort()
# 显然结果不同,因为对于已经排好序的,自然很快
28.8 ms ± 1.69 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%lsmagic 可以展示所有其他魔法命令
%lsmagic
Available line magics:
%alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %conda %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 %pip %popd %pprint %precision %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 %xmode
Available 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 %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.
代码自动补全功能
执行如下命令
/*更换数据源为国内清华源*/
pip install jupyter_contrib_nbextensions -i https://pypi.tuna.tsinghua.edu.cn/simple
jupyter nbextensions_configurator enable --user
重新打开jupyter notebook,找到Nbextensions,然后选择Hinterland即可
如果找不到这些插件,则执行如下命令
jupyter contrib nbextension install --user --skip-running-check
如果想要更换主题
参考《jupyter notebook使用技巧(1):更换主题》
安装之前请先将pip升级到最新。
python -m pip install --upgrade pip
如果下载比较卡,可以临时改用国内源,即在命令结尾加上-i
https://pypi.tuna.tsinghua.edu.cn/simple,后续安装都可以。
python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
安装jupyterthemes。
pip install jupyterthemes -i https://pypi.tuna.tsinghua.edu.cn/simple
如果出现错误信息:
Cannot uninstall 'jupyter-client'. It is a distutils installed project and thus we cannot accurate ly determine which files belong to it which would lead to only a partial uninstall.
解决办法直接到anaconda安装目录下,搜索对应包的名字,删掉对应文件夹和.egg-info文件即可
重新运行安装命令,完成jupyterthemes的安装。
jt -l //主题列表
Available Themes:
chesterish
grade3
gruvboxd
gruvboxl
monokai
oceans16
onedork
solarizedd
solarizedl
//选择monikai:
jt -t monokai
//更换主题
jt -r
//设置主题参数
jt -t oceans16 -f consolamono -tf ptmono -ofs 10 -nfs 13 -tfs 13 -fs 12 -T -N -lineh 140
如果打开notebook还是报错Internal server error 500
更新jupyter notebook
conda update jupyter notebook