Install graphiz on AWS Sagemaker - xgboost

I'm on a Jupyter notebook using Python3 and trying to plot a tree with code like this:
import xgboost as xgb
from xgboost import plot_tree
plot_tree(model, num_trees=4)
On the last line I get:
~/anaconda3/envs/python3/lib/python3.6/site-packages/xgboost/plotting.py in to_graphviz(booster, fmap, num_trees, rankdir, yes_color, no_color, **kwargs)
196 from graphviz import Digraph
197 except ImportError:
--> 198 raise ImportError('You must install graphviz to plot tree')
199
200 if not isinstance(booster, (Booster, XGBModel)):
ImportError: You must install graphviz to plot tree
How do I install graphviz so I can see the plot_tree?

I was finally able to learn that Conda has a package which can install it for you. I was able to get it installed by running the command:
!conda install python-graphviz --yes
Note the --yes is only needed if the installation needs to verify adding/changing other packages since the Jupyter notebook is not interactive once it is running.

Related

ModuleNotFoundError: No module named 'numpy' AWS SageMaker Studio Lab

I installed the numpy with Conda, pip and pip3, also tried installing from the requirements.txt file but I am still getting the following issue. Has anyone encountered this before and could please kindly suggest something?
ModuleNotFoundError Traceback (most recent call last)
/tmp/ipykernel_732/1818554654.py in
1 import os
----> 2 import numpy as np
3 import matplotlib.pyplot as plt
4 import pandas as pd
5 import random
ModuleNotFoundError: No module named 'numpy'
numpy is built-in the default:Python environment in Studio Lab. If you open a new notebook with the File -> New -> Notebook, and choose the default:Python kernel, you should be able to import numpy without having to install the package.
If you would like to have a reproducible environment with a set of packages, see this sample repo on creating custom environments.
Numpy isn't preinstalled as a module you will need to install it yourself using pip or conda. Using notebooks just include a code block with the following:
!python -m pip install numpy
Suggestion to compile a list of required modules in a 'requirements.txt' file and install using:
!python -m pip install -r requirements.txt
Just as an FYI, the base package list is as follows:
Python 3.9 bzip2 build-essential curl git libgl1-mesa-glx nano rsync
unzip wget ca-certificates pip ipykernel-6.4
Have a look at this link for more info.
SageMaker - Manage your environment

How to plot an image using PIL or Matplotlib

I am trying to plot an image in python using either PIL or Matplotlib. I have tried both
import matplotlib.pyplot as plt
and
from PIL import Image
But I get the same error:
ImportError: cannot import name '_imaging' from 'PIL'
I have updated pillow and matplotlib packages but no success
You can read the image using open cv and then plot it using matplotlib.
img_name = cv2.imread("image.jpeg")
plt.imshow(img_name)
plt.show()
This usually happens when folk use python and pip from different packages, distributions or versions. I mean you install a package with pip into one package/distribution/version and then try and use it from a different package/distribution/version of Python.
So, if you normally start Python using:
python
then run:
type python
Whereas if you normally start Python using:
python3
then run:
type python3
On my machine I get:
type python3
python3 is /Library/Frameworks/Python.framework/Versions/3.9/bin/python3
so now I know which Python I am using.
Now you need to see which pip you are using. So, if you normally start pip with:
pip
then run:
type pip
Whereas if you normally start pip with:
pip3
then run:
type pip3
On my machine I get:
type pip3
pip3 is /Library/Frameworks/Python.framework/Versions/3.9/bin/pip3
So now you can see that my pip3 is installing packages for python3 exactly where it can find them.

Unable render neural network structural graph on kaggle notebook

I got this message on Kaggle notebook
'Failed to import pydot. You must `pip install pydot` and install graphviz (https://graphviz.gitlab.io/download/), ', 'for `pydotprint` to work.'
Rendering NN structural graphs doesn't work. What is the problem? Is it a problem on Kaggle side? Code I was running was very simple.
The first thing to do is to turn on internet support on your kaggle notebook and install the following packages via pip packet manager:
!pip install pydot
!pip install pydotplus
!pip install graphviz

Using Tensorflow with GPU on Debian 8.10 Jessie

I have Tensorflow installed on the Debian Server using virtual environment
export url=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.5.0-cp34-cp34m-linux_x86_64.whl
pip3 install --user $url
This installs tensorflow-gpu==1.5.0 on ~/.local/lib/python3.4/site-packages
But when I import tensor flow package in Ipython notebook like:
import tensorflow as tf
I get the following error:
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory
In /usr/local/cuda-8.0 I do have ./lib64/libcublas.so.8.0. Then why can't Tensorflow find it? Now do I need to install cuda-9.0? Unfortunately, I don't have root permissions on the system. Is there a way around this?
Thanks in advance,

ipython cannot search matplotlib while using tensorflow and jupyter also has import error

I am using python 2.7 in Ubuntu and recently updated tensorflow 0.12.1.
I installed jupyter today for my sample code of tf and I need to use matplotlib. It does not find module name matplotlib and ipython in tensorflow has same error.
1. How can I set path in virtualenv or ipython or jupyter?
After activate tensorflow, I need to use jupyter notebook.
This below in the script for error does not work.
import sys
sys.path.append('my/path/to/module/folder')
import module-of-interest
2. other information: My environments are below.
mickyefromsd#DEKSTOP~$source ~/tensorflow/bin/activate
When I find matplotlib by python script under TF condition and before TF activation, it has below;
/usr/lib/python2.7/dist-packages/matplotlib/
When I type 'which ipython', it has below (not by /usr/bin/ipython) ;
/home/mickeyfromd/tensorflow/bin/ipython
Btw, /tensorflow/lib/python2.7/site-packages/ it has ipython and jupyter.
(not in the same path of matplotlib)
3. My ipython under TF cannot find my existing matplotlib.
(tensorflow) mickeyfromd#DK-DESKTOP:~$ ipython
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
In [1]: import matplotlib ImportError: No module named matplotlib
4. I wanted to setup virtualenv, so I just run this
I followed this site. site:http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs
(tensorflow) mickeyfromd#ipython kernelspec install-self --user
.....
Installed kernelspec python2 in /home/mickeyfromd/.local/share/jupyter/kernels/python2
(tensorflow) mickeyfromd#DK-DESKTOP:~$
I cannot move the the folder (in the second step)
How can I make ipython to have path for Matplotlib?
That import error is due to change in environment of the jupyter notebook. You might have installed the packages in one environment and you are running the jupyter notebook in another environment.
I have got two environments (envs) in my Anaconda folder ( I have Anaconda3 folder to be specific ).
(windows key+cmd ) -> open the windows command prompt run as administrator.
Activate (name of the environment) -> eg.: activate tensorflow-gpu
Start installing packages using conda install
Note: For each environment you need to install all the packages you want to use, separately using the same process. This solution is for windows users, might work for linux users not sure though.
Additionally to make sure your conda environment is up to date run:
conda update conda
conda update anaconda
check this out : https://pradyumnamajumder.wordpress.com/2017/09/30/solution-to-the-python-packages-import-error-in-jupyter/