jupyter notebook sample

使用Linux command

在Linux command前面加入一个感叹号'!'

In [102]:
!python -V
Python 3.6.6 :: Anaconda custom (64-bit)
In [103]:
!pip -V
pip 18.0 from /root/anaconda3/lib/python3.6/site-packages/pip (python 3.6)

查看使用 ! 和 % 的区别

In [104]:
!pwd
/jupyterfile
In [105]:
%pwd
Out[105]:
'/jupyterfile'

魔法命令 (magic function)

使用 %lsmagic 显示全部magic

In [106]:
%lsmagic
Out[106]:
Available line magics:
%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %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  %popd  %pprint  %precision  %profile  %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.

使用 ? 获取帮助信息

In [107]:
%ls?

使用 %time和 %timeit 测试代码运行时间

In [108]:
strings = ['foo', 'foobar', 'baz', 'qux', 'python', 'Guido Van Rossum'] * 100000
method1 = [x for x in strings if x.startswith('foo')]
method2 = [x for x in strings if x[:3] == 'foo']
In [109]:
%time method1 = [x for x in strings if x.startswith('foo')]
CPU times: user 78.8 ms, sys: 2.03 ms, total: 80.9 ms
Wall time: 86.6 ms
In [110]:
%time method2 = [x for x in strings if x[:3] == 'foo']
CPU times: user 54.3 ms, sys: 0 ns, total: 54.3 ms
Wall time: 59.3 ms
In [111]:
%timeit [x for x in strings if x.startswith('foo')]
80.4 ms ± 7.37 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [112]:
%timeit [x for x in strings if x[:3] == 'foo']
55.9 ms ± 4.65 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [113]:
x = 'foobar'
y = 'foo'
In [114]:
 %timeit x.startswith(y)
155 ns ± 6.46 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [115]:
%timeit x[:3] == y
130 ns ± 4 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

使用 %%writefile 生成文件

In [116]:
%%writefile test_peace.py
import numpy as np
print(np.random.randint(1,5))
Overwriting test_peace.py

使用 %run 运行py文件

In [117]:
%run test_peace.py
2

使用 %cp 复制文件

In [118]:
%cp test_peace.py test_load.py

使用 %ls 查看目录下的文件夹与文件

In [119]:
%ls -al
total 76
drwxr-xr-x 4 root root  4096 Sep  8 10:10 ./
drwxr-xr-x 1 root root  4096 Sep  8 08:10 ../
drwxr-xr-x 2 root root  4096 Sep  8 09:42 .ipynb_checkpoints/
-rw-r--r-- 1 root root 25541 Sep  8 10:10 jupyter notebook sample.ipynb
-rw-r--r-- 1 root root 15412 Sep  6 01:32 notebook.tex
drwxr-xr-x 3 root root  4096 Sep  6 03:06 storm/
-rw-r--r-- 1 root root    49 Sep  8 08:54 test.py
-rw-r--r-- 1 root root    48 Sep  8 10:11 test_load.py
-rw-r--r-- 1 root root    48 Sep  8 10:11 test_peace.py
-rw-r--r-- 1 root root    78 Sep  8 07:12 text.txt

查看当前变量类型

In [120]:
%whos
Variable   Type      Data/Info
------------------------------
method1    list      n=200000
method2    list      n=200000
np         module    <module 'numpy' from '/ro<...>kages/numpy/__init__.py'>
strings    list      n=600000
x          str       foobar
y          str       foo

使用 %reset重置当前变量空间

In [121]:
%reset -f
In [122]:
%whos
Interactive namespace is empty.

加载本地.py文件

输入 %load test.py 然后点击Run
会自动将test.py文件中的代码载入cell中

In [123]:
# %load test.py
import pandas as pd
from pandas import DataFrame
In [124]:
dd = [[1, 2, 3],[8, 9, 12]]
d = DataFrame(dd)
print(d.T)
   0   1
0  1   8
1  2   9
2  3  12

使用 %hist 查看历史命令

In [125]:
%hist
print(128888345)
print('abcdjjjjjjefg')
!python -V
import caffe
SolverName = "/root/workspace"
sovler = caffe.AdamSolver(SolverName)
%lsmagis
%lsmagics
%lsmagic
!pip -V
%alias?
%cat?
%cd?
%load test.py
%load test.py
# %load test.py
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "print(123)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
%load test.py
%load test.py
dd = [1, 2, 3]
d = DataFrame(dd)
print(d)
# %load test.py
import pandas as pd
from pandas import DataFrame
dd = [1, 2, 3]
d = DataFrame(dd)
print(d)
dd = [[1, 2, 3],[8, 9, 12]]
d = DataFrame(dd)
print(d)
dd = [[1, 2, 3],[8, 9, 12]]
d = DataFrame(dd)
print(d.T)
!python -V
!pip -V
%lsmagic
%cd?
# %load test.py
import pandas as pd
from pandas import DataFrame
dd = [[1, 2, 3],[8, 9, 12]]
d = DataFrame(dd)
print(d.T)
!pwd
!python -V
!pip -V
!pwd
# %load test.py
import pandas as pd
from pandas import DataFrame
dd = [[1, 2, 3],[8, 9, 12]]
d = DataFrame(dd)
print(d.T)
%lsmagic
%cd?
!python -V
!pip -V
!pwd
# %load test.py
import pandas as pd
from pandas import DataFrame
dd = [[1, 2, 3],[8, 9, 12]]
d = DataFrame(dd)
print(d.T)
%lsmagic
%cd?
%ls?
import numpy as np
%timeit np.sin(24)
import numpy as np
%timeit np.sin(24)
%%timeit
x=np.sin(20)
np.cos(-x)
%%writefile test_peace.py
import numpy as np
print(np.random.randint(1,5))
%run test_peace.py
%pwd
%whos
%cp test_peace.py test_load.py
%ls -al
!python -V
!pip -V
!pwd
%pwd
%lsmagic
%ls?
import numpy as np
%timeit np.sin(24)
%%timeit
x=np.sin(20)
np.cos(-x)
%%writefile test_peace.py
import numpy as np
print(np.random.randint(1,5))
%run test_peace.py
%cp test_peace.py test_load.py
%ls -al
%whos
# %load test.py
import pandas as pd
from pandas import DataFrame
dd = [[1, 2, 3],[8, 9, 12]]
d = DataFrame(dd)
print(d.T)
%whos
# %load test.py
import pandas as pd
from pandas import DataFrame
dd = [[1, 2, 3],[8, 9, 12]]
d = DataFrame(dd)
print(d.T)
!python -V
!pip -V
!pwd
%pwd
%lsmagic
%ls?
import numpy as np
%timeit np.sin(24)
%%timeit
x=np.sin(20)
np.cos(-x)
%%writefile test_peace.py
import numpy as np
print(np.random.randint(1,5))
%run test_peace.py
%cp test_peace.py test_load.py
%ls -al
%whos
# %load test.py
import pandas as pd
from pandas import DataFrame
dd = [[1, 2, 3],[8, 9, 12]]
d = DataFrame(dd)
print(d.T)
%timeit L=[i**2 for i in range(10) ]
import numpy as np
%timeit np.sin(24)
%timeit L=[i**2 for i in range(10) ]
%%timeit
x=np.sin(20)
np.cos(-x)
strings = ['foo', 'foobar', 'baz', 'qux', 'python', 'Guido Van Rossum'] * 100000
method1 = [x for x in strings if x.startswith('foo')]
method2 = [x for x in strings if x[:3] == 'foo']
%time method1 = [x for x in strings if x.startswith('foo')]
 %time method2 = [x for x in strings if x[:3] == 'foo']
%timeit [x for x in strings if x.startswith('foo')]
%timeit [x for x in strings if x[:3] == 'foo']
x = 'foobar'
y = 'foo'
 %timeit x.startswith(y)
%timeit x[:3] == y
%whos
%reset -f
%whos
%hist
!python -V
!pip -V
!pwd
%pwd
%lsmagic
%ls?
strings = ['foo', 'foobar', 'baz', 'qux', 'python', 'Guido Van Rossum'] * 100000
method1 = [x for x in strings if x.startswith('foo')]
method2 = [x for x in strings if x[:3] == 'foo']
%time method1 = [x for x in strings if x.startswith('foo')]
%time method2 = [x for x in strings if x[:3] == 'foo']
%timeit [x for x in strings if x.startswith('foo')]
%timeit [x for x in strings if x[:3] == 'foo']
x = 'foobar'
y = 'foo'
 %timeit x.startswith(y)
%timeit x[:3] == y
%%writefile test_peace.py
import numpy as np
print(np.random.randint(1,5))
%run test_peace.py
%cp test_peace.py test_load.py
%ls -al
%whos
%reset -f
%whos
# %load test.py
import pandas as pd
from pandas import DataFrame
dd = [[1, 2, 3],[8, 9, 12]]
d = DataFrame(dd)
print(d.T)
%hist

◎ 欢迎参与讨论,请在这里发表您的看法、交流您的观点。