<jupyter_start><jupyter_text>18BCE084<NAME><jupyter_code>import sklearn
from sklearn import metrics,datasets
from sklearn.tree import DecisionTreeClassifier
import numpy as np
from sklearn import model_selection
# spliting data
X,Y=datasets.load_iris(return_X_y=True)

xtrain=X[range(0,150,2),:]
ytrain=Y[range(0,150,2)]
xtest=X[range(1,150,2),:]
ytest=Y[range(1,150,2)]

# making instance of id3 classifier
print("Classification Using ID3 ")
clf=DecisionTreeClassifier(criterion='entropy')
#learning the classifier
clf.fit(xtrain,ytrain)
#predicting the class
ypred=clf.predict(xtest)

print("accuracy ",metrics.accuracy_score(ytest,ypred))
print("confussion metrics ",metrics.confusion_matrix(ytest,ypred))
print()
print(metrics.classification_report(ytest, ypred))
print()

# CART 

print("classification using CART ")
# making instance of cart classifier
model = DecisionTreeClassifier(criterion='gini')
model.fit(xtrain,ytrain)
ypred=model.predict(xtest)
print("accuracy ",metrics.accuracy_score(ytest,ypred))
print("confussion metrics ",metrics.confusion_matrix(ytest,ypred))
print()
print(metrics.classification_report(ytest, ypred))
print()<jupyter_output>Classification Using ID3 
accuracy  0.96
confussion metrics  [[25  0  0]
 [ 0 24  1]
 [ 0  2 23]]

              precision    recall  f1-score   support

           0       1.00      1.00      1.00        25
           1       0.92      0.96      0.94        25
           2       0.96      0.92      0.94        25

    accuracy                           0.96        75
   macro avg       0.96      0.96      0.96        75
weighted avg       0.96      0.96      0.96        75


classification using CART 
accuracy  0.96
confussion metrics  [[25  0  0]
 [ 0 24  1]
 [ 0  2 23]]

              precision    recall  f1-score   support

           0       1.00      1.00      1.00        25
           1       0.92      0.96      0.94        25
           2       0.96      0.92      0.94        25

    accuracy                           0.96        75
   macro avg       0.96      0.96      0.96        75
weighted avg       0.96      0.96      0.96        75<jupyter_text>Decision Tree in ***CATEGORICAL*** Data<jupyter_code>import pandas as pd
import csv
import random 
from sklearn import preprocessing
from sklearn.metrics import classification_report 
trend = 'up,down,down,down,up,up,down,up,up,up,down,down,down,up,down,up,down,up,up,down'.split(',')
variance = 'low,high,low,normal,high,low,normal,high,normal,low,normal,normal,high,high,low,high,high,normal,normal,low'.split(',')
buys = 'yes,no,no,no,yes,yes,yes,no,yes,yes,no,yes,no,yes,yes,no,no,yes,yes,no'.split(',')
table={'trend':trend,'variance':variance,'Purchase':buys}
df = pd.DataFrame(table)
df



label_encoder = preprocessing.LabelEncoder()

for i in df:
  df[i] = label_encoder.fit_transform(df[i])
print("After preprocessing ")
df
data = df[['trend','variance']]
out = df['Purchase']
x_train, x_test, y_train, y_test = model_selection.train_test_split(data, out, test_size = 0.30)
print(x_train)

print("For ID3\n")
clf = DecisionTreeClassifier(criterion = 'entropy')

clf.fit(x_train, y_train)
ypred = clf.predict(x_test)
print(ypred)
print('Accuracy', metrics.accuracy_score(y_test, ypred, normalize = True))

print("For CART\n")
clf = DecisionTreeClassifier(criterion = 'gini')
clf.fit(x_train, y_train)
ypred = clf.predict(x_test)

print(ypred)
print('Accuracy', metrics.accuracy_score(y_test, ypred, normalize = True))<jupyter_output>For ID3

[1 0 1 0 0 0]
Accuracy 0.6666666666666666
For CART

[1 0 1 0 0 0]
Accuracy 0.6666666666666666
{'fraction_non_alphanumeric': 0.12004466778336124, 'fraction_numerical': 0.0661641541038526, 'mean_word_length': 3.3013205282112845, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '650284', 'license': 'Apache-2.0', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1281, 'n_words': 274, 'path': 'Decision Tree Classifier.ipynb', 'repo_name': 'jKaushalb/Machine-Learning', 'size': '3593'}