Esc + F
will find and replace only in the code, not taking into account the output;Esc + O
- switch to the cell output;%matplotlib inline
;%matplotlib notebook
is an interactive mode, but it works very slowly, since the graphics are processed on the server side; # list available python magics %lsmagic
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 %%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.
#%env - without arguments lists environmental variables %env OMP_NUM_THREADS=4
env: OMP_NUM_THREADS=4
!pip install numpy !pip list | grep Theano
Requirement already satisfied (use --upgrade to upgrade): numpy in /Users/axelr/.venvs/rep/lib/python2.7/site-packages Theano (0.8.2)
%matplotlib inline from matplotlib import pyplot as plt import numpy
# if you don't put semicolon at the end, you'll have output of function printed plt.hist(numpy.linspace(0, 1, 1000)**1.5);
from sklearn.cross_validation import train_test_split # show the sources of train_test_split function in the pop-up window train_test_split??
# you can use ? to get details about magics, for instance: %pycat?
Show a syntax-highlighted file through a pager. This magic is similar to the cat utility, but it will assume the file to be Python source and will show it with syntax highlighting. This magic command can either take a local filename, an url, an history range (see %history) or a macro as argument :: %pycat myscript.py %pycat 7-27 %pycat myMacro %pycat http://www.example.com/myscript.py
# this will execute all the code cells from different notebooks %run ./2015-09-29-NumpyTipsAndTricks1.ipynb
[49 34 49 41 59 45 30 33 34 57] [172 177 209 197 171 176 209 208 166 151] [30 33 34 34 41 45 49 49 57 59] [209 208 177 166 197 176 172 209 151 171] [1 0 4 8 6 5 2 9 7 3] ['a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j'] ['b' 'a' 'e' 'i' 'g' 'f' 'c' 'j' 'h' 'd'] ['a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j'] [1 0 6 9 2 5 4 8 3 7] [1 0 6 9 2 5 4 8 3 7] [ 0.93551212 0.75079687 0.87495146 0.3344709 0.99628591 0.34355057 0.90019059 0.88272132 0.67272068 0.24679158] [8 4 5 1 9 2 7 6 3 0]
[-5 -4 -3 -2 -1 0 1 2 3 4] [0 0 0 0 0 0 1 2 3 4] ['eh' 'cl' 'ah' ..., 'ab' 'bm' 'ab'] ['ab' 'ac' 'ad' 'ae' 'af' 'ag' 'ah' 'ai' 'aj' 'ak' 'al' 'am' 'an' 'bc' 'bd' 'be' 'bf' 'bg' 'bh' 'bi' 'bj' 'bk' 'bl' 'bm' 'bn' 'cd' 'ce' 'cf' 'cg' 'ch' 'ci' 'cj' 'ck' 'cl' 'cm' 'cn' 'de' 'df' 'dg' 'dh' 'di' 'dj' 'dk' 'dl' 'dm' 'dn' 'ef' 'eg' 'eh' 'ei' 'ej' 'ek' 'el' 'em' 'en' 'fg' 'fh' 'fi' 'fj' 'fk' 'fl' 'fm' 'fn' 'gh' 'gi' 'gj' 'gk' 'gl' 'gm' 'gn' 'hi' 'hj' 'hk' 'hl' 'hm' 'hn' 'ij' 'ik' 'il' 'im' 'in' 'jk' 'jl' 'jm' 'jn' 'kl' 'km' 'kn' 'lm' 'ln' 'mn'] [48 33 6 ..., 0 23 0] ['eh' 'cl' 'ah' ..., 'ab' 'bm' 'ab'] ['eh' 'cl' 'ah' ..., 'ab' 'bm' 'ab'] ['bf' 'cl' 'dn' ..., 'dm' 'cn' 'dj'] ['bf' 'cl' 'dn' ..., 'dm' 'cn' 'dj']
[ 2.29711325 1.82679746 2.65173344 ..., 2.15286813 2.308737 2.15286813] 1000 loops, best of 3: 1.09 ms per loop The slowest run took 8.44 times longer than the fastest. This could mean that an intermediate result is being cached. 10000 loops, best of 3: 21.5 µs per loop
0.416 0.416
# %load http://matplotlib.org/mpl_examples/pylab_examples/contour_demo.py
data = 'this is the string I want to pass to different notebook' %store data del data # deleted variable
Stored 'data' (str)
# in second notebook I will use: %store -r data print data
this is the string I want to pass to different notebook
# pring names of string variables %who str
data
%%time import time time.sleep(2) # sleep for two seconds
CPU times: user 1.23 ms, sys: 4.82 ms, total: 6.05 ms Wall time: 2 s
# measure small code snippets with timeit ! import numpy %timeit numpy.random.normal(size=100)
The slowest run took 13.85 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: 6.35 µs per loop
%%writefile pythoncode.py import numpy def append_if_not_exists(arr, x): if x not in arr: arr.append(x) def some_useless_slow_function(): arr = list() for i in range(10000): x = numpy.random.randint(0, 10000) append_if_not_exists(arr, x)
Overwriting pythoncode.py
# shows highlighted source of the newly-created file %pycat pythoncode.py
from pythoncode import some_useless_slow_function, append_if_not_exists
# shows how much time program spent in each function %prun some_useless_slow_function()
26338 function calls in 0.713 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 10000 0.684 0.000 0.685 0.000 pythoncode.py:3(append_if_not_exists) 10000 0.014 0.000 0.014 0.000 {method 'randint' of 'mtrand.RandomState' objects} 1 0.011 0.011 0.713 0.713 pythoncode.py:7(some_useless_slow_function) 1 0.003 0.003 0.003 0.003 {range} 6334 0.001 0.000 0.001 0.000 {method 'append' of 'list' objects} 1 0.000 0.000 0.713 0.713 <string>:1(<module>) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
%load_ext memory_profiler
# tracking memory consumption (show in the pop-up) %mprun -f append_if_not_exists some_useless_slow_function()
('',)
Line # Mem usage Increment Line Contents ================================================ 3 20.6 MiB 0.0 MiB def append_if_not_exists(arr, x): 4 20.6 MiB 0.0 MiB if x not in arr: 5 20.6 MiB 0.0 MiB arr.append(x)
import line_profiler lp = line_profiler.LineProfiler() lp.add_function(some_useless_slow_function) lp.runctx('some_useless_slow_function()', locals=locals(), globals=globals()) lp.print_stats()
Timer unit: 1e-06 s Total time: 1.27826 s File: pythoncode.py Function: some_useless_slow_function at line 7 Line # Hits Time Per Hit % Time Line Contents ============================================================== 7 def some_useless_slow_function(): 8 1 5 5.0 0.0 arr = list() 9 10001 17838 1.8 1.4 for i in range(10000): 10 10000 38254 3.8 3.0 x = numpy.random.randint(0, 10000) 11 10000 1222162 122.2 95.6 append_if_not_exists(arr, x)
#%%debug filename:line_number_for_breakpoint # Here some code that fails. This will activate interactive context for debugging
# %pdb # def pick_and_take(): # picked = numpy.random.randint(0, 1000) # raise NotImplementedError() # pick_and_take()
%%ruby puts 'Hi, this is ruby.'
Hi, this is ruby.
%%bash echo 'Hi, this is bash.'
Hi, this is bash.
!pip install cython fortran-magic
%load_ext Cython
%%cython def myltiply_by_2(float x): return 2.0 * x
myltiply_by_2(23.)
46.0
%load_ext fortranmagic
/Users/axelr/.venvs/rep/lib/python2.7/site-packages/IPython/utils/path.py:265: UserWarning: get_ipython_cache_dir has moved to the IPython.paths module warn("get_ipython_cache_dir has moved to the IPython.paths module")
%%fortran subroutine compute_fortran(x, y, z) real, intent(in) :: x(:), y(:) real, intent(out) :: z(size(x, 1)) z = sin(x + y) end subroutine compute_fortran
compute_fortran([1, 2, 3], [4, 5, 6])
array([-0.95892429, 0.65698659, 0.41211849], dtype=float32)
!pip install https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tarball/master !pip install jupyter_nbextensions_configurator !jupyter contrib nbextension install --user !jupyter nbextensions_configurator enable --user
import os from IPython.display import display, Image names = [f for f in os.listdir('../images/ml_demonstrations/') if f.endswith('.png')]
names = !ls ../images/ml_demonstrations/*.png names[:5]
['../images/ml_demonstrations/colah_embeddings.png', '../images/ml_demonstrations/convnetjs.png', '../images/ml_demonstrations/decision_tree.png', '../images/ml_demonstrations/decision_tree_in_course.png', '../images/ml_demonstrations/dream_mnist.png']
Reconnect to kernel
option allows you to reconnect to the working kernel without interrupting the calculations, and see the last output (although some of the output will still be lost).Source: https://habr.com/ru/post/316826/
All Articles