Recently, I've been taking good care of Jupyter notebook (Python3), I will post it because some tricks have increased.
Jupyter Notebook 4.2.x
Python 3.5.x
For example, it can be used in the Jupyter environment on Docker at https://github.com/mokemokechicken/jupyter-tensorflow.
Decision trees such as sklearn.tree.tree.DecisionTreeClassifier
can output in .dot format, but trying to display them on a notebook is quite annoying.
However, with certain preparations and procedures, you will be able to do that easily.
dot
Define a function like this.
decision_tree_util.py
import os
from tempfile import mkstemp
import subprocess
from sklearn.tree.export import export_graphviz
def convert_decision_tree_to_ipython_image(clf, feature_names=None, class_names=None,
image_filename=None, tmp_dir=None):
dot_filename = mkstemp(suffix='.dot', dir=tmp_dir)[1]
with open(dot_filename, "w") as out_file:
export_graphviz(clf, out_file=out_file,
feature_names=feature_names,
class_names=class_names,
filled=True, rounded=True,
special_characters=True)
from IPython.display import Image
image_filename = image_filename or ('%s.png' % dot_filename)
subprocess.call(('dot -Tpng -o %s %s' % (image_filename, dot_filename)).split(' '))
image = Image(filename=image_filename)
os.remove(dot_filename)
return image
You can use it like this.
notebook
from sklearn import datasets
from sklearn.tree.tree import DecisionTreeClassifier
%matplotlib inline
iris = datasets.load_iris()
X = iris.data
Y = iris.target
clf = DecisionTreeClassifier(max_depth=3)
clf.fit(X, Y)
convert_decision_tree_to_ipython_image(clf, image_filename='tree.png')
You may want to reload because you changed the file you are importing from another location. If you write the following, it will be reloaded every time you run notebook. Before I knew this, I restarted the kernel every time (^^;
%reload_ext autoreload
%autoreload 2
from IPython.core.display import display
Import and
display(data_frame)
Then it will be displayed as usual. If you pass a character string etc., the character string will be output normally.
If you write like this,
notebook
from IPython.core.display import display
from numpy.random.mtrand import normal
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
display('graph1')
sns.distplot(normal(0, 1, size=1000))
sns.distplot(normal(0.1, 2, size=1000))
display('graph2')
sns.distplot(normal(-2, 2, size=1000))
sns.distplot(normal(2, 4, size=1000))
The graphs are frozen together and the graph comes to the bottom.
In that case, you should call plt.show ()
at the right time.
notebook
from IPython.core.display import display
from numpy.random.mtrand import normal
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
display('graph1')
sns.distplot(normal(0, 1, size=1000))
sns.distplot(normal(0.1, 2, size=1000))
plt.show()
display('graph1')
sns.distplot(normal(-2, 2, size=1000))
sns.distplot(normal(2, 4, size=1000))
plt.show()
This can be used not only for script on notebook, but also when you want to output to notebook from ordinary source code.
It was a trick.
Recommended Posts