-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaseStudyStep9.py
More file actions
185 lines (127 loc) · 4.05 KB
/
Copy pathCaseStudyStep9.py
File metadata and controls
185 lines (127 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import (
accuracy_score,
confusion_matrix,
classification_report,
)
Border = "-"*30
##########################################
# Step1 : Load the Data Set
##########################################
print(Border)
print("Step1 : Load the DataSet")
print(Border)
DataPath = "iris.csv"
df = pd.read_csv(DataPath)
print("Dataset Loaded Succefully")
print("Initial enteries form dataset :")
print(df.head())
#print(df.tail()) -> for tail
##########################################
# Step2 : Data Analysis (EDA)
##########################################
print(Border)
print("Step2 : Data Analysis (EDA)")
print(Border)
print("Shape of dataset ",df.shape)
print("Column name :", list(df.columns))
print("Missing values per column : ")
print(df.isnull().sum())
print("Class distribution (species count)")
print(df["species"].value_counts())
print("Satatical report of Dataset :")
print(df.describe())
#####################################################
# Step3 : Decide independent & dependent variables
####################################################
print(Border)
print("Step3 : Decide independent & dependent variables")
print(Border)
# X : Independent Variable (features)
# Y : Dependent Variable (lables)
feature_col = [
"sepal length (cm)",
"sepal width (cm)",
"petal length (cm)",
"petal width (cm)"
]
X = df[feature_col]
Y = df["species"]
print("X Shape :",X.shape)
print("Y Shape :",Y.shape)
#####################################################
# Step4 : visualization of dataset
####################################################
print(Border)
print("Step4 : visualization of dataset")
print(Border)
#Scatter plot
plt.figure(figsize=(7,5))
for sp in df["species"].unique():
temp = df[df["species"] == sp]
plt.scatter(temp["petal length (cm)"],temp["petal width (cm)"],label = sp)
plt.title("Marvellous Iris Case study")
plt.xlabel("petal length (cm)")
plt.ylabel("petal width (cm)")
plt.legend()
plt.grid()
plt.show()
#####################################################
# Step5 : Split dataset for Traning & testing
####################################################
print(Border)
print("Step5 : Split dataset for Traning & testing")
print(Border)
X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size=0.5, random_state=42)
print("Data set splitting Activity Done ")
print("X :", X.shape) #(150,4)
print("Y :", Y.shape) #(150,)
print("X_train :",X_train.shape) #(75,4)
print("X_test :",X_test.shape) #(75,4)
print("Y_train :",Y_train.shape) #(75,)
print("Y_test :",Y_test.shape) #(75,)
#####################################################
# Step6 : Bulid the model
####################################################
print(Border)
print("Step6 : Bulid the model")
print(Border)
model = DecisionTreeClassifier(max_depth=5)
print("Model gets Created succesfully")
#####################################################
# Step7 : Train the model
####################################################
print(Border)
print("Step7 : Train the model")
print(Border)
model.fit(X_train,Y_train)
print("Model tranaied sucssfully")
#####################################################
# Step8 : Test the model
####################################################
print(Border)
print("Step8 : Test the model")
print(Border)
Y_pred = model.predict(X_test)
print("Model testing done")
print("Expected Answer :")
print(Y_test)
print("Predicted answer :")
print(Y_pred)
#####################################################
# Step9 : Evaluate the model performance
####################################################
print(Border)
print("Step9 : Evaluate the model performance")
print(Border)
accuracy = accuracy_score(Y_test,Y_pred)
print("accuracy of model is :", accuracy*100)
print("Confustion matrix")
cm = confusion_matrix(Y_test,Y_pred)
print(cm)
print("Classification Report")
print(classification_report(Y_test,Y_pred))