Posts Tagged

jupyter-notebook

Visualize a Decision Tree with Sklearn

Step 1: Install the libraries
sudo apt-get install graphviz

pip install graphviz
pip install pydotplus
pip install sklearn
pip install pydot pip install pandas

Do the imports

import pydotplus
import pandas as pd
from sklearn import tree
from io import StringIO
import pydot
Step 2: Initialize the dataframe
data = [ 
    (0, 5, 0), 
    (1, 6, 0), 
    (2, 7, 1), 
    (3, 8, 1), 
    (4, 9, 1)
]
df = pd.DataFrame(data, index=range(5), columns=['x1','x2','y'])
Step 3: Train the decision tree
x_columns = ['x1','x2']

model = tree.DecisionTreeClassifier()
trained_model = model.fit(df[x_columns], df['y'])
Step 4: Display the decision tree

Two options

Option A: You want to save the decision tree as a file

dotfile = StringIO()

tree.export_graphviz(
    trained_model,  
    out_file        = dotfile,
    feature_names   = x_columns, 
    class_names     = ['[y=0]', '[y=1]'], # Ascending numerical order
    filled          = True,
    rounded         = True
)

(graph,) = pydot.graph_from_dot_data(dotfile.getvalue())
graph.write_png("tree.png")

 

This should generate an image named “tree.png” in your current directory

Option B: You want to display the decision tree in your Jupyter notebook

from IPython.display import Image

out_file = tree.export_graphviz(
    trained_model,
    feature_names   = x_columns,
    class_names     = ['[y=0]', '[y=1]'],# Ascending numerical order
    filled          = True,
    rounded         = True
)
graph = pydotplus.graph_from_dot_data(out_file)
Image(graph.create_png())

In either case this is the tree you should get

 

References:

https://scikit-learn.org/stable/modules/generated/sklearn.tree.export_graphviz.html

 

Jupyter notebook as a service on Ubuntu 18.04 with Python 3

How amazing would it be to start your computer, visit localhost:8888, and  know that your Jupyter Notebook is waiting for you, up and running ? Well here is how to make this dream a reality on Ubuntu 18.04 with Python 3.

 

Step 1: Install Jupyter Notebook

 

Install pip3 and the python header files

sudo apt update 
sudo apt install python3-pip python3-dev

I also want the packages I install in my notebook not to be in conflict with the other python packages installed on the system, so I’m creating a virtual environment for Jupyter only. If the 2 lines of code below don’t work for you, check out this post for the full instructions.

virtualenv -p python3 notebook-env
. notebook-env/bin/activate

Your command line prompt should now be preceded by (notebook-env) to signal that you are working in the virtual environment you just created. As you will see by typing

(notebook-env) naysan@hp:~$ which pip3
/home/naysan/notebook-env/bin/pip3

From now on (until you deactivate the environment), the pip3 executable you will call is the one from your notebook environment. Use this pip3 binary to install jupyter

(notebook-env) naysan@hp:~$ pip3 install jupyter

Launch jupyter 

(notebook-env) naysan@hp:~$ jupyter-notebook

 

This command should open your browser at  localhost:8888. Now I don’t know about you, but since I launched the notebook from my home directory, there are a bunch of directories listed in the notebook interface that have nothing to do with where I’d like to store my python notebooks. Here is a way to dedicate a clean, separate directory for your Jupyter experiments.

Shut down your kernel with Ctrl+C, then

# Back to the HOME directory
(notebook-env) naysan@hp:~$ cd

# Create a clean new directory dedicated to your notebooks
(notebook-env) naysan@hp:~$ mkdir my-notebooks

# Use this directory as your --notebook-dir argment as you re-launch jupyter-notebook
(notebook-env) naysan@hp:~$ jupyter-notebook --notebook-dir=/home/naysan/my-notebooks

Much cleaner, right?

 

Step 2: Setup Jupyter-notebook as a service

 

This step is made easier if we setup a password for the notebook beforehand, otherwise Jupyter will ask you to use a very long token to access it. Shut down your kernel with Ctrl+C, then

(notebook-env) naysan@hp:~$ jupyter notebook password
Enter password: **********
Verify password: **********
[NotebookPasswordApp] Wrote hashed password to /home/naysan/.jupyter/jupyter_notebook_config.json

 

Locate your jupyter-notebook binary

(notebook-env) naysan@hp:~$ which jupyter-notebook 
/home/naysan/notebook-env/bin/jupyter-notebook 

Create a file named jupyter.service that contains the following content. Don’t forget to replace my personal info – like my user name, Naysan – with what works  for you

[Unit]
Description=Jupyter Notebook

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/bin/bash -c ". /home/naysan/notebook-env/bin/activate;jupyter-notebook --notebook-dir=/home/naysan/my-notebooks"
User=naysan
Group=naysan
WorkingDirectory=/home/naysan/my-notebooks
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

 

It is now time to install the service

(notebook-env) naysan@hp:~$ sudo cp jupyter.service /etc/systemd/system/

# Use the enable command to ensure that the service starts whenever the system boots
(notebook-env) naysan@hp:~$ sudo systemctl enable jupyter.service

(notebook-env) naysan@hp:~$ sudo systemctl daemon-reload
(notebook-env) naysan@hp:~$ sudo systemctl start jupyter.service
(notebook-env) naysan@hp:~$ systemctl status jupyter.service 
● jupyter.service - Jupyter Notebook
   Loaded: loaded (/etc/systemd/system/jupyter.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-09-07 12:18:08 EDT; 53min ago
 Main PID: 32316 (bash)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/jupyter.service
           ├─32316 /bin/bash -c . /home/naysan/notebook-env/bin/activate;jupyter-notebook --notebook-dir=/home/naysan/my-notebooks
           └─32319 /home/naysan/notebook-env/bin/python3 /home/naysan/notebook-env/bin/jupyter-notebook --notebook-dir=/home/naysan/my-notebooks

Sep 07 12:18:08 hp bash[32316]: [I 12:18:08.473 NotebookApp] http://localhost:8888/

Step 3: Witness the magic

Visit http://localhost:8888/

Enter your password, and voilà, you are all set !