<jupyter_start><jupyter_text>Decision Trees with Sklearn This notebook implements and measures a Decision Tree classification model in Sklearn.* Method: [Decision Tree](http://scikit-learn.org/stable/modules/tree.html)* Dataset: Iris  Imports<jupyter_code>from os import environ

import numpy as np

from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import accuracy_score

import matplotlib.pyplot as plt
import graphviz

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Load and Prepare the Data<jupyter_code># Load the dataset
data = load_iris()
# Get information on the dataset
print(data.DESCR)
# Split the data into labels (targets) and features
label_names = data['target_names']
labels = data['target']

feature_names = data['feature_names']
features = data['data']

# View the data
print(label_names)
print(labels[0])
print("")
print(feature_names)
print(features[0])
# Create test and training sets
X_train, X_test, Y_train, Y_test = train_test_split(features,
                                                    labels,
                                                    test_size=0.33,
                                                    random_state=42)<jupyter_output><empty_output><jupyter_text>Fit a Decision Tree Model<jupyter_code># Create an instance of the Decision Tree classifier
model = tree.DecisionTreeClassifier()

# Train the model
model.fit(X_train, Y_train)<jupyter_output><empty_output><jupyter_text>Create Predictions<jupyter_code># Create predictions
predictions = model.predict(X_test)
print(predictions)
# Predict the probability of each class
pred_probs = model.predict_proba(X_test)
print(pred_probs[0])
# Create a plot to compare actual class (Y_test) and the predicted class (predictions)
fig = plt.figure(figsize=(20,10))
plt.scatter(Y_test, predictions)
plt.xlabel("Actual Class: $Y_i$")
plt.ylabel("Predicted Class: $\hat{Y}_i$")
plt.title("Actual vs. Predicted Class: $Y_i$ vs. $\hat{Y}_i$")
plt.show()<jupyter_output><empty_output><jupyter_text>Model Evaluation  AccuracyThe accuracy score is either the fraction (default) or the count (normalize=False) of correct predictions.<jupyter_code>print("Accuracy Score: %.2f" % accuracy_score(Y_test, predictions))<jupyter_output><empty_output><jupyter_text>K-Fold Cross ValidationThis estimates the accuracy of an SVM model by splitting the data, fitting a model and computing the score 5 consecutive times. The result is a list of the scores from each consecutive run.<jupyter_code># Get scores for 5 folds over the data
clf = tree.DecisionTreeClassifier()
scores = cross_val_score(clf, data.data, data.target, cv=5)

# Print the scores and mean score
print("Scores: {}".format(scores))
print("Mean Score: %0.2f" % np.mean(scores))<jupyter_output><empty_output><jupyter_text>View the Tree<jupyter_code>dot_data = tree.export_graphviz(model,
                                out_file=None, 
                                feature_names=data.feature_names,  
                                class_names=data.target_names,  
                                filled=True,
                                rounded=True,  
                                special_characters=True)  
graph = graphviz.Source(dot_data) 
graph<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09012235153685468, 'fraction_numerical': 0.005371530886302597, 'mean_word_length': 3.7817403708987163, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '236741', 'license': 'Apache-2.0', 'n_tokens_mistral': 1031, 'n_tokens_neox': 994, 'n_words': 265, 'path': 'code/day_6/4 - Decision Trees with Sklearn.ipynb', 'repo_name': 'anjijava16/data-analytics-machine-learning-big-data', 'size': '3351'}