In [1]:
a = 123

In [2]:
b = a + 42

In [3]:
print(b)


165

Is this actually markdown?

appears to be.

What is `raw NBConvert`? Googling that for myself, it's Raw [Jupyter] Notebook Format.

Oh cool, headings are just markdown, too.


In [4]:
# Do we still have access to previous variable?
print("a + b =", a + b)


a + b = 288

In [12]:
# Magic commands
%lsmagic


Out[12]:
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 [23]:
# Output not exported.
# Get help on a particular cell magic
?%%capture

In [25]:
# Output not exported.
%pycat hello.py

In [1]:
# %load hello.py
# Using the above will copy-paste the below on first run, causing the code to get exported.
# http://scikit-learn.org/stable/tutorial/basic/tutorial.html

import matplotlib.pyplot as plt
# MacOS rendering problem fix: https://stackoverflow.com/questions/29433824/unable-to-import-matplotlib-pyplot-as-plt-in-virtualenv#comment64137123_35107136
import numpy as np
# import pickle
from sklearn import datasets
from sklearn import svm
from sklearn.externals import joblib

print('\n\n\n*** numpy ***\n\n\n')
a = np.arange(15).reshape(3, 5)
print('np.arange(15).reshape(3, 5):\n', a)

print('')

zeros = np.zeros((10, 2))
print('np.zeros((10, 2))\n', zeros)

print('\n\n\n*** sklearn ***\n\n\n')
iris = datasets.load_iris()
digits = datasets.load_digits()
print('datasets must be in (n_samples, n_features) shape.')
print('Shape of original digit images (cannot be consumed by scikit learn):', digits.images.shape)
# Need to flatten the 8x8 image into a vactor 64 length.
print('Shape of digits dataset that can be consumed by scikitlearn:', digits.data.shape)
# print('digits descriptor:', digits.DESCR)

clf = svm.SVC(gamma=0.001, C=100.)
# fit == learn
clf.fit(digits.data[:-1], digits.target[:-1])

# predict values based of previous training
x = clf.predict(digits.data[-1:])
print('Predict the value of the last digit:', x)

# Can save (serialize) model data via pickle.
# s = pickle.dumps(clf)
# but this seems not recommended,
# instead user their to-file serializer...
joblib.dump(clf, 'saved-classifier.digits.pkl')




*** numpy ***



np.arange(15).reshape(3, 5):
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]

np.zeros((10, 2))
 [[0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]]



*** sklearn ***



datasets must be in (n_samples, n_features) shape.
Shape of original digit images (cannot be consumed by scikit learn): (1797, 8, 8)
Shape of digits dataset that can be consumed by scikitlearn: (1797, 64)
Predict the value of the last digit: [8]
Out[1]:
['saved-classifier.digits.pkl']

In [2]:
# Command out to shell
!echo "Hello world"


Hello world

In [3]:
# Making interactive HTML widgets and handling them in the python code
# see: https://blog.dominodatalab.com/interactive-dashboards-in-jupyter/
# For ipython notebooks... maybe?
from ipywidgets import widgets
# For HTML widgets... having trouble seeing which is the defacto one to use, above or below?
# from IPython.html import widgets
# Nope, the above appears to be deprecated, use ipywidgets
from IPython.display import display

# Implicit widget defintion
# TODO: How do I pass in things like `placeholder` here, which seems easier to do in widgets.Text?
def callback(x=''):
    print(x)
widgets.interact(callback, x='')

# Explicit widget definition
text = widgets.Text(
    placeholder="Type in something and hit return",
    continuous_update=True
)
# event callback
text.on_submit(lambda el: print(el.value))
display(text)



In [ ]: