Export PDF of decisiontree - python

i'm using scikitlearn to introduce me to Machine Learning, i' m following this tutorial link to yt
but if i try to export the pdf decision tree i have this error:
i try to do: open -w review iris.pdf
and the result is :
Impossibile ottenere un descrittore di file che si riferisce alla console
if i compile from the terminal i have the error:
Traceback (most recent call last) File "fstraining.py", line 2, in <module>
import graphviz ImportError: No module named graphviz
Thanks for the attention

Once you have built your decision tree clf, simply:
from sklearn.externals.six import StringIO
from sklearn.tree import export_graphviz
import pydotplus
# Export resulting tree to DOT source code string
dot_data = export_graphviz(clf,
feature_names=col_names,
out_file=None,
filled=True,
rounded=True)
#Export to pdf
pydot_graph = pydotplus.graph_from_dot_data(dot_data)
pydot_graph.write_pdf('tree.pdf')
This answer is adapted from here:

Related

How to resolve the ImportError: cannot import name 'DesicionTreeClassifier' from 'sklearn.tree' in python?

Hello there,
I am new to python and I was trying out a project on jupyter notebook when I encountered an error which I couldn't resolve. I'd really appreciate some help.
This is my code:
import pandas as pd
from sklearn.tree import DesicionTreeClassifier
music_data = pd.read_csv(r'C:\python\python382\music.csv')
X=music_data.drop(columns=['genre'])
y=music_data['genre']
model=DesicionTreeClassifier()
model.fit(X,y)
music_data
And i got the output as :
ImportError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2540/2462038274.py in <module>
1 import pandas as pd
----> 2 from sklearn.tree import DesicionTreeClassifier #using desicion tree algo here to make model[we import DesicionTree module from tree module which is imported from sklearn library]
3 music_data = pd.read_csv(r'C:\python\python382\music.csv')
4
5 ##Cleaning and segregating data
ImportError: cannot import name 'DesicionTreeClassifier' from 'sklearn.tree' (C:\python\python382\lib\site-packages\sklearn\tree\__init__.py)
Thank you.
You have missspelled the fumction name DesicionTreeClassifier is in reality DecisionTreeClassifier

Getting error while getting decision tree as PNG file

i am trying to learn machine learn through Python from W3School. I am trying to get mydecisiontree. PNG using PyDotPlus
I am using pip PyCharm professional 2020.3
the code is as follow:
import numpy as np
import matplotlib.pyplot as plt
import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.image as pltimg
df = pandas.read_csv("shows.csv")
d = {'UK' : 0,'USA' : 1, 'N' : 2}
df['Nationality'] = df['Nationality'].map(d)
d = {'YES' : 1, "NO" : 0}
df['Go'] = df['Go'].map(d)
features = ['Age', 'Experience', 'Rank', 'Nationality']
X = df[features]
y = df['Go']
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
data = tree.export_graphviz(dtree, out_file=None, feature_names=features)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png('mydecisiontree.png')
img=pltimg.imread('mydecisiontree.png')
img_plot = plt.imshow(img)
plt.show()
Although the PyCharm shows no error but when i run the code it cant make the PNG file and gives an error on the line:
graph.write_png('mydecisiontree.png')
It shows the following error:
File "direc.....\venv\lib\site-packages\pydotplus\graphviz.py", line 1960, in create
'GraphViz\'s executables not found')
pydotplus.graphviz.InvocationException: GraphViz's executables not found
I can't see the problem. How to solve this?
PyCharm doesn't show an error because your code doesn't contain any. The problem is in your environment. Have you installed GraphViz (using pip install graphviz) in the environment that you are using to run it?
Also see the answers here:
GraphViz not working when imported inside PydotPlus (GraphViz's executables not found)
Graphviz's executables are not found (Python 3.4)
I had that problem too,
I installed graphviz from: here
and it solved.

Python - dot file to png file not found error

I am trying to convert dot file into a png or jpeg file where I can view the Random Forest Tree. I am following this tutorial: https://towardsdatascience.com/how-to-visualize-a-decision-tree-from-a-random-forest-in-python-using-scikit-learn-38ad2d75f21c.
I am getting error FileNotFoundError: [WinError 2] The system cannot find the file specified
I can see that tree.dot is there and I am able to open it. Trying to find why it is not reading it? Thanks.
from sklearn.datasets import load_iris
iris = load_iris()
# Model (can also use single decision tree)
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=10)
# Train
model.fit(iris.data, iris.target)
# Extract single tree
estimator = model.estimators_[5]
from sklearn.tree import export_graphviz
# Export as dot file
export_graphviz(estimator, out_file='tree.dot',
feature_names = iris.feature_names,
class_names = iris.target_names,
rounded = True, proportion = False,
precision = 2, filled = True)
<<error occurs here>>
# Convert to png using system command (requires Graphviz)
from subprocess import call
call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])
# Display in jupyter notebook
from IPython.display import Image
Image(filename = 'tree.png')
I ran through docker - ubuntu image and ran: RUN apt-get install graphviz -y in the Dockerfile. It started to work. Then used dot -Tpng tree.dot -o tree.png

I canĀ“t find the load_data function from rasa_nlu.training_data

Rasa NLU version (e.g. 0.7.3): rasa-nlu-0.11.3
Used backend / pipeline : spacy_sklearn
Operating system : Windows 10
Issue:
I am trying to follow the sample code for training as stated in the rasa website.
from rasa_nlu.training_data import load_data
from rasa_nlu.config import RasaNLUConfig
from rasa_nlu.model import Trainer
training_data = load_data('data/examples/rasa/demo-rasa.json')
trainer = Trainer(RasaNLUConfig("sample_configs/config_spacy.json"))
trainer.train(training_data)
model_directory = trainer.persist('./projects/default/')
But I can't find the load_data function from rasa_nlu.training_data, therefore, I get the following error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-07f2f1a6c2ac> in <module>()
----> 1 from rasa_nlu.training_data import load_data
2 from rasa_nlu.config import RasaNLUConfig
3 from rasa_nlu.model import Trainer
ImportError: cannot import name 'load_data'
Can someone please help me?
use from rasa_nlu.converters import load_data instead of from rasa_nlu.training_data import load_data
See
http://rasa-nlu.readthedocs.io/en/latest/python.html
https://nlu.rasa.ai/0.11.3/python.html
Solution
from rasa.shared.nlu.training_data.loading import load_data
More info: https://github.com/RasaHQ/rasa/issues/1536

Scipy installation, its installed why the traceback?

I am running the following code (this is the beginning snippet):
# Import necessary modules and functions
########################################
# time module, for timing it.
from time import time
global starttime
starttime = time()
# psutil for indentifying processor and memory usage
#import psutil
# csv module for loading csv files
import csv
# numpy, a standard module for working with arrays
import numpy as np
# for randomly shuffling arrays
from random import shuffle
# for computing mean squared errors
from sklearn import metrics
# importing modules that contain models we will use
from sklearn import linear_model, ensemble, gaussian_process
from sklearn import neural_network, svm
# importing memory profiler to enable us to determine peak memory usage
from memory_profiler import profile
# Helpful functions
###################
#psutilpercent = psutil.virtual_memory()
#print "\n", " --> Memory Check 1 Percent:", str(psutilpercent.percent) + "%\n"
# This function is for simplifying reading CSV files
def readCSV(path):
"""
Read a CSV file of floats, with no headder
"""
data = []
mycsv = csv.reader(open(path), delimiter="|")
for counter, row in enumerate(mycsv):
if counter != 0:
data.append(row)
return np.asarray(data, dtype=np.float32)
However I am receiving the traceback:
Traceback (most recent call last):
File "C:\Users\regression_v6.py", line 38, in <module>
from sklearn import metrics
File "C:\Python27\lib\site-packages\sklearn\__init__.py", line 32, in <module>
from .base import clone
File "C:\Python27\lib\site-packages\sklearn\base.py", line 10, in <module>
from scipy import sparse
ImportError: No module named scipy
But my computer shows Scipy as installed, here is a view of Control Panel\All Control Panel Items\Programs and Features:
Any ideas?
Did you install a special version of scipy? as your screenshot shows 'scipy_umfpack'
I can successfully import sklearn using the official version downloaded from sourceforge: http://sourceforge.net/projects/scipy/files/scipy/

Categories

Resources