预备知识
Markdown
Linux 常用命令
Jupyter
魔法命令
IPython提供了许多魔法命令,使得在 IPython 环境中的操作更加得心应手。
魔法命令都以%或者%%开头。以%开头的成为行命令(line magics),%%开头的称为单元命令(cell magics)。行命令只对命令所在的行有效,而单元命令则必须出现在单元的第一行,对整个单元的代码进行处理。
参考阅读: IPython Magic Commands
查看与帮助
%lsmagic #查看所有的魔法命令
Available line magics:
%alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %conda %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %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 %rep %rerun %reset %reset_selective %rm %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 %%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.
%magic #查看各个命令的说明
#查看具体命令的说明 在命令后添加?
%save?
Docstring:
Save a set of lines or a macro to a given filename.
Usage:
%save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
Options:
-r: use 'raw' input. By default, the 'processed' history is used,
so that magics are loaded in their transformed version to valid
Python. If this option is given, the raw input as typed as the
command line is used instead.
-f: force overwrite. If file exists, %save will prompt for overwrite
unless -f is given.
-a: append to the file instead of overwriting it.
This function uses the same syntax as %history for input ranges,
then saves the lines to the filename you specify.
It adds a '.py' extension to the file if you don't do so yourself, and
it asks for confirmation before overwriting existing files.
If `-r` option is used, the default extension is `.ipy`.
File: ~/anaconda3/lib/python3.7/site-packages/IPython/core/magics/code.py
数据分析中常用的魔法命令
%whos: 显示当前所有变量,含名称及详情%who <DType>: 显示对应数据类型的变量名称%who_ls: Outputs a LIST of all interactive variables in your environment%who_ls <DType>: Reduces the output LIST to interactive variables of type "DType"%pinfo <variable>: Get detailed information about the variable.%env: Get and set environmental variables.%matplotlib inline: Displaying matlpotlib graphs in jupyter notebook%load <file_name>: Load an external file.%alias?: 常用的命令总结%matplotlib inline: 将matplotlib画出的图直接显示在Notebook之中%timeit: 测试单行语句的执行时间%%timeit: 测试整个单元中代码的执行时间%%prun: 调用profile模块,对单元的代码进行性能剖析%%writefile: 写入文件%run <file name>: 写入文件