1

I am using Python (Pycharm community edition 2016) I've created a working model using Random Forest, and am very keen to see one of the trees visualized. I have researched a lot of info about how the Graphviz package can be used to do this, but I haven't been able to see any code snippets of it working. Below you can see some of my code that creates my model and I even plot its accuracy using the Precicion Recall Curve. How would I now include code to visualize one of the trees ?

###Split data into Training&Testing data-use stratified sampling ###
from sklearn.cross_validation import train_test_split 
X = Actual_DataFrame.copy()
y = X.pop('Attrition')
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.30, 
random_state=42, stratify=y)
###----------------------------------------------------------------------###

### Procedure:- (Model) - RF              ###
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier()
rf.fit(X_train, y_train)
predictions2 = rf.predict(X_test)
print ("(Model )")
print("Accuracy:",accuracy_score(y_test, predictions2))
print("Cohen's Kappa Score : ",cohen_kappa_score(y_test, predictions2))
print(classification_report(y_test, predictions2))
precision, recall, _ = precision_recall_curve(y_test, predictions2, 
pos_label=1)
average_precision = average_precision_score(y_test, predictions2)
print (precision)
plt.plot(recall, precision, label='area = %0.2f' % average_precision, 
color="green")
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Precision Recall Curve - RF')
plt.legend(loc="lower right")
plt.show()
###--------------------------------------------------------------------###

Any help appreciated Paul

skintPaul
  • 11
  • 1
  • 4
  • Possible duplicate of [Plot trees for a Random Forest in Python with Scikit-Learn](https://stackoverflow.com/questions/40155128/plot-trees-for-a-random-forest-in-python-with-scikit-learn) – desertnaut Apr 17 '18 at 12:11

0 Answers0