ML Lab6.Ipynb - Colaboratory
ML Lab6.Ipynb - Colaboratory
ML Lab6.Ipynb - Colaboratory
ipynb - Colaboratory
DECISION TREE
1 import pandas as pd
2 import numpy as np
3 import seaborn as sns
4 import matplotlib.pyplot as plt
5 from google.colab import files
6 uploaded = files.upload()
7
1 dt_df = pd.read_csv("Bill.csv")
1 dt_df.describe()
1
dt_df.isnull().sum()*100/dt_df.shape[0]
Variance 0.0
Skewness 0.0
Curtosis 0.0
Entropy 0.0
Class 0.0
dtype: float64
1
X_dt = dt_df.drop('Class',axis=1)
2
y_dt = dt_df['Class']
1
from sklearn.model_selection import train_test_split
3
X_train,X_test,y_train,y_test=train_test_split(X_dt,y_dt,test_size=0.2)
https://colab.research.google.com/drive/1fb1GxUhTNcGPM5E0YNyfB3YyegB03GFC#scrollTo=bjc56ErlCh4M&printMode=true 1/5
5/10/22, 12:39 AM 191389_ML_Lab6.ipynb - Colaboratory
1
X_train.head()
1
y_train.head()
286 0
412 0
493 0
369 0
732 0
1
import statsmodels.api as sm
3
X_train_sm = sm.add_constant(X_train)
4
dt_lm = sm.OLS(y_train, X_train_sm).fit()
/usr/local/lib/python3.7/dist-packages/statsmodels/tsa/tsatools.py:117: FutureWarning
x = pd.concat(x[::order], 1)
1
print(dt_lm.summary())
==============================================================================
Df Model: 4
==============================================================================
------------------------------------------------------------------------------
==============================================================================
https://colab.research.google.com/drive/1fb1GxUhTNcGPM5E0YNyfB3YyegB03GFC#scrollTo=bjc56ErlCh4M&printMode=true 2/5
5/10/22, 12:39 AM 191389_ML_Lab6.ipynb - Colaboratory
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly spec
1
from sklearn.tree import DecisionTreeClassifier
3
clf_CART = DecisionTreeClassifier()
4
clf_ID3 = DecisionTreeClassifier(criterion = 'entropy') #criterion is by default gini
5
#For ID3 criterion is entropy
7
clf_CART.fit(X_train,y_train)
8
clf_ID3.fit(X_train,y_train)
DecisionTreeClassifier(criterion='entropy')
1
y_pred=clf_CART.predict(X_test)
1
from sklearn.metrics import confusion_matrix,classification_report
3
print(confusion_matrix(y_test,y_pred))
4
print(classification_report(y_test,y_pred))
[[149 4]
[ 1 121]]
1
#CART Decision Tree
3
from sklearn.tree import plot_tree
5
plt.figure(figsize=(25,10))
6
plot_tree(clf_CART, filled=True)
7
plt.show()
https://colab.research.google.com/drive/1fb1GxUhTNcGPM5E0YNyfB3YyegB03GFC#scrollTo=bjc56ErlCh4M&printMode=true 3/5
5/10/22, 12:39 AM 191389_ML_Lab6.ipynb - Colaboratory
1
#ID3 Decision Tree
3
from sklearn.tree import plot_tree
5
plt.figure(figsize=(25,10))
6
plot_tree(clf_ID3, filled=True)
7
plt.show()
https://colab.research.google.com/drive/1fb1GxUhTNcGPM5E0YNyfB3YyegB03GFC#scrollTo=bjc56ErlCh4M&printMode=true 4/5
5/10/22, 12:39 AM 191389_ML_Lab6.ipynb - Colaboratory
https://colab.research.google.com/drive/1fb1GxUhTNcGPM5E0YNyfB3YyegB03GFC#scrollTo=bjc56ErlCh4M&printMode=true 5/5