In [1]:
a = 123
In [2]:
b = a + 42
In [3]:
print(b)
In [4]:
# Do we still have access to previous variable?
print("a + b =", a + b)
In [12]:
# Magic commands
%lsmagic
Out[12]:
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')
Out[1]:
In [2]:
# Command out to shell
!echo "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 [ ]: