<jupyter_start><jupyter_text>The Sparks Foundation  Task 6 Prediction using Decision Tree Algorithm  The purpose is if we feed any new data to this classifier, it would be able to predict the right class accordingly. Import the Following Libraries:     numpy (as np)      pandas      DecisionTreeClassifier from sklearn.tree<jupyter_code>import numpy as np 
import pandas as pd
from sklearn.tree import DecisionTreeClassifier<jupyter_output><empty_output><jupyter_text>Now, read data using pandas dataframe:<jupyter_code>df = pd.read_csv("Iris.csv")
df.head()
df.size<jupyter_output><empty_output><jupyter_text>Pre-processing Using df as the Iris.csv data read by pandas, declare the following variables:       X  as the  Feature Matrix  (data of df)       y  as the  response vector (target)   Remove the column containing the target name since it doesn't contain numeric values.<jupyter_code>X = df[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']].values
X[0:5]<jupyter_output><empty_output><jupyter_text>Now we can fill the target variable.<jupyter_code>y = df["Species"]
y[0:5]<jupyter_output><empty_output><jupyter_text>Setting up the Decision Tree    We will be using train/test split on our decision tree. Let's import train_test_split from sklearn.cross_validation.<jupyter_code>from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>Now  train_test_split  will return 4 different parameters. We will name them:X_trainset, X_testset, y_trainset, y_testset  The  train_test_split  will need the parameters: X, y, test_size=0.3, and random_state=3.  The X and y are the arrays required before the split, the test_size represents the ratio of the testing dataset, and the random_state ensures that we obtain the same splits.<jupyter_code>X_trainset, X_testset, y_trainset, y_testset = train_test_split(X, y, test_size=0.3, random_state=3)
print(X_trainset.shape,y_trainset.shape)
print(X_testset.shape,y_testset.shape)<jupyter_output>(45, 4) (45,)<jupyter_text>Modeling    We will first create an instance of the DecisionTreeClassifier called drugTree.    Inside of the classifier, specify  criterion="entropy"  so we can see the information gain of each node.<jupyter_code>drugTree = DecisionTreeClassifier(criterion="entropy", max_depth = 4)
drugTree<jupyter_output><empty_output><jupyter_text>Next, we will fit the data with the training feature matrix  X_trainset  and training  response vector  y_trainset<jupyter_code>drugTree.fit(X_trainset, y_trainset)<jupyter_output><empty_output><jupyter_text>Prediction    Let's make some predictions on the testing dataset and store it into a variable called predTree.<jupyter_code>predTree = drugTree.predict(X_testset)
print(predTree[0:5])
print(predTree[0:10])
print(y_testset[0:10])<jupyter_output>['Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'
 'Iris-virginica' 'Iris-versicolor' 'Iris-setosa' 'Iris-virginica'
 'Iris-versicolor']
47         Iris-setosa
3          Iris-setosa
31         Iris-setosa
25         Iris-setosa
15         Iris-setosa
118     Iris-virginica
89     Iris-versicolor
6          Iris-setosa
103     Iris-virginica
65     Iris-versicolor
Name: Species, dtype: object<jupyter_text>Evaluation    Next, let's import metrics from sklearn and check the accuracy of our model.<jupyter_code>from sklearn import metrics
import matplotlib.pyplot as plt
print("DecisionTrees's Accuracy: ", metrics.accuracy_score(y_testset, predTree))<jupyter_output>DecisionTrees's Accuracy:  0.9555555555555556<jupyter_text>__Accuracy classification score__ computes subset accuracy: the set of labels predicted for a sample must exactly match the corresponding set of labels in y_true.  In multilabel classification, the function returns the subset accuracy. If the entire set of predicted labels for a sample strictly match with the true set of labels, then the subset accuracy is 1.0; otherwise it is 0.0.     Visualization    Lets visualize the tree<jupyter_code>!conda install -c conda-forge pydotplus -y
!conda install -c conda-forge python-graphviz -y
from sklearn.externals.six import StringIO
import pydotplus
import matplotlib.image as mpimg
from sklearn import tree
%matplotlib inline
dot_data = StringIO()
filename = "drugTree.png"
featureNames = df.columns[0:4]
targetNames = df["Species"]. unique().tolist()
out = tree.export_graphviz(drugTree, feature_names=featureNames, out_file=dot_data, class_names=np.unique(y_trainset), 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')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08175305520438264, 'fraction_numerical': 0.01580278128950695, 'mean_word_length': 5.940058479532164, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '479290', 'license': 'MIT', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1517, 'n_words': 447, 'path': 'TSF.ipynb', 'repo_name': 'Jainish-Shah-tech/Prediction-Using-Decision-Tree-Algorithm', 'size': '4746'}