Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14271

visualization a Decision Trees with pydotplus

$
0
0

in a project of Decision TreeI wrote this code, but it gets a specific error and I consulted with AI , the problem is not solved, you can see where the problem comes from.

import numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom sklearn import preprocessingdf = pd.read_csv('E:\mostafa folder\mashin learning\S5recommendersystems\Files\drug200.csv')df.head()df.describe()df.columnsx = df[['Age', 'Sex', 'BP', 'Cholesterol', 'Na_to_K']].valuesxy = df[['Drug']].valuesyfrom sklearn import preprocessingle_sex = preprocessing.LabelEncoder()le_sex.fit(['F','M'])x[:,1] = le_sex.transform(x[:,1]) le_BP = preprocessing.LabelEncoder()le_BP.fit([ 'LOW', 'NORMAL', 'HIGH'])x[:,2] = le_BP.transform(x[:,2])le_Chol = preprocessing.LabelEncoder()le_Chol.fit([ 'NORMAL', 'HIGH'])x[:,3] = le_Chol.transform(x[:,3]) x[0:4]from sklearn.model_selection import train_test_splitx_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=3)print('shape of train : ',x_train.shape,y_train.shape)print('shape of test : ',x_test.shape,y_test.shape)from sklearn.tree import DecisionTreeClassifiertree = DecisionTreeClassifier(criterion="entropy", max_depth = 6)tree.fit(x_train,y_train)pretree = tree.predict(x_test)print (pretree [0:5])print (y_test [0:5])from sklearn import metricsprint("DecisionTrees's Accuracy: ", metrics.accuracy_score(y_test, pretree))from  io import StringIOimport pydotplusimport matplotlib.image as mpimgfrom sklearn.tree import DecisionTreeClassifierfrom sklearn.tree import export_graphviz%matplotlib inline dot_data = StringIO()featuresnames = df.columnsfilename = 'mosi.png'out = export_graphviz(tree,feature_names = featuresnames)from sklearn.tree import export_graphvizdot_data = StringIO()filename = 'ee.png'#featurenames = df.columns[0:5]out = export_graphviz(tree, feature_names=df.columns[0:5], out_file=dot_data, class_names= np.unique(y_train), filled=True, special_characters=True, rotate=False)graph = pydotplus.graph_from_dot_data(dot_data.getvalue())graph.write_png(filename)img = mpimg.imread(filename)plt.figure(figsize=(100, 200))plt.imshow(img, interpolation='nearest')

And this problem is taken from this line

out = export_graphviz(tree, feature_names=df.columns[0:5], out_file=dot_data, class_names= np.unique(y_train), filled=True, special_characters=True, rotate=False)

<module 'sklearn.tree' from 'C:\\Users\\PARDAZESHGARA\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\sklearn\\tree\\__init__.py'> is not an estimator instance.

help me guys!!!!


Viewing all articles
Browse latest Browse all 14271

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>