You are on page 1of 3

CSI3026

Machine Learning

20MICOO13 Shreyas Kumar S

Ans:

import matplotlib.pyplot as plt

from sklearn import preprocessing

from sklearn.tree import DecisionTreeClassifier,plot_tree

from sklearn.model_selection import ParameterGrid, GridSearchCV

from sklearn.metrics import make_scorer,accuracy_score,classification_report

le = preprocessing.LabelEncoder()

df = pd.read_excel("ML2.xlsx")

df["Laptop_Model"] = le.fit_transform(df["Laptop_Model"])

x = df.iloc[:, [0, 2]].values

y = df.iloc[:, 2].values

#Splitting

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.20, random_state = 0)

#Generating Decesion Tree

full_tree=DecisionTreeClassifier(random_state=30)

full_tree.fit(X_train,y_train)

plt.figure(figsize=(4,4),dpi=1000)

plot_tree(full_tree,filled=True)

plt.show()

Output:
After Applying Data Pruning:

max_depth=full_tree.get_depth()

print("The maximum_depth of the tree is:",max_depth)

clf = DecisionTreeClassifier(criterion="gini", max_depth=6)

clf = clf.fit(X_train,y_train)

y_pred = clf.predict(X_test)

print(classification_report(y_test, clf.predict(X_test)))

plt.figure(figsize=(4,4),dpi=1000)

plot_tree(clf,filled=True)
plt.show()

Output:

Conclusion:

After applying the Pruning the Accuracy has increased by 0.1%

You might also like