-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_5_dhruvdhayal_ai_ml.py
384 lines (304 loc) · 12.3 KB
/
day_5_dhruvdhayal_ai_ml.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# -*- coding: utf-8 -*-
"""Day-5_DHRUVDHAYAL_AI/ML.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/19OXvgcpMRY0mhkAEBHN6MeOPG2EvWzOa
#MACHINE LEARNING.
We, will train and test the model such that , model can learn from previous past experiences, surrounding of it's enviornment, by gathering and collecting all data & information in the form of a Knowledge then, it will practically Implement in rel-world implementations to perform all the task and behave just as like a Humans called as "Machine Learning".
#1. Supervised Learning
#2. Unsupervised Learning.
#3. Other Tools and Techniques like: SVM, RainForest Methods, etc.
"""
from sklearn import datasets; #Used for Importing the Valueable Datasets Values.
from sklearn import svm; #Used for Classification Modelling svm-support vector Machines.
from sklearn import metrics; #Used for Evaluation of the Model.
from sklearn import model_selection; #Used for Model Selection.
#Now, load the main_datasets;
data=datasets.load_digits();
print(type(data));
#print("\n Given Data is: \n\n",data);
#Now, printing the Values of the Data.
print("\n --> Given Values of the Data are: \n\n",data);
#Now, we need to describe the values of the data.
print(data.DESCR);
#Now, after Implementing the Main datasets.
#We, need to divide it ir further in training and testing phase.
#Now, in this, we have to get feature extraction of attributes(data) & labels.
X=data.data;
print(type(X));
print(X.shape);
Y=data.target;
print(type(Y));
print(Y.shape);
#Here, X->Represents the values of the Data and Information in the form of the Attributes.
#Next, Y->Represents the Values of the Labels for each Datasets Attributes.
#Now, we are representing the Values of the datasets data in the form of the Digits.
import matplotlib.pyplot as plt;
import matplotlib.image as mpimg;
import numpy as np;
#Visualise the Data Images.
image=data.images;
y=data.target;
print(type(image));
print(image.shape);
num=1270;
sample_im=image[num,:,:];
label=y[num];
plt.figure(1,figsize=(5,5));
plt.imshow(sample_im,cmap="gray");
plt.title(" Actual Target: "+str(label));
plt.axis("off");
#Now, we are just representing the Values of the Data and Information in the form of the Images present in the given Datasets.
import matplotlib.pyplot as plt;
import matplotlib.image as mpimg;
import numpy as np;
#Visualise the Given Datasets.
image=data.images;
y=data.target;
print(type(image));
print(image.shape);
num=1270;
sample_im=image[num,: ,:];
label=y[num];
plt.figure(1,figsize=(5,5));
plt.imshow(sample_im,cmap="gray");
plt.title(" Acual Target : "+str(label));
plt.axis("off");
plt.show();
"""#Task-1
--> Solve, the Problem Statement that you need to show the collection of data and infromation in datasets in the form of random numbers and represent it in the form of the 7*7 Matrix randomly form of Representations.
"""
#First, we need to implement the values of the Libraries.
import matplotlib.pyplot as plt;
import matplotlib.image as imag;
import numpy as np;
#Visualising the Values of the Data.
images=data.images;
y=data.target;
print("\n Type of the Images in Datasets: ",type(image));
print("\n Total Length of the Images Data are: ",image.shape);
#Now, making the 7*7 Matrix to generate the Random Numbers from the datasets and arrange it in the proper manner.
for i in range(49):
num=np.random.randint(0,1797);
samples_im=images[num,: ,: ];
label=y[num];
plt.subplot(7,7,i+1);
plt.figure(1,figsize=(8,6));
plt.imshow(samples_im,cmap="gray");
#plt.title(" Actual Target: "+str(label));
plt.axis("off");
#Now, we need to show the Values of datasets in the form of random Images.
#Importing all the Libraries of it!
import matplotlib.pyplot as plt;
import matplotlib.image as imag;
import numpy as np;
#Visualise the Values of the Data.
image=data.images;
y-data.target;
print("\n --> Type of the Image is: ",type(image));
print("\n --> Total Length of the Images Data are: ",image.shape);
num=1270;
sample_im=image[num, : , :];
label=y[num];
plt.figure(1,figsize=(5,5));
plt.imshow(sample_im,cmap="gray");
plt.title(" Actual Target: \n"+str(label));
plt.axis("off");
plt.show();
#Now, representing all the data and information in the datasets randomly representation by using the (7*7) Matrix.
import matplotlib.pyplot as plt;
import matplotlib.image as imag;
import numpy as np;
#Visualising the values of the Data.
image=data.images;
y=data.target;
print("\n --> Type of the Images present in the Datasets are: ",type(image));
print("\n --> Total Length of the Datasets present in Images are: ",image.shape);
for i in range(49):
num=np.random.randint(0,1797);
samples_imp=image[num, : , :];
label=y[num];
plt.subplot(7,7,i+1);
plt.figure(1,figsize=(8,6));
#plt.title(" Actual Target: "+str(label));
plt.imshow(samples_imp,cmap="gray");
plt.axis("off");
#Showing the Values of the main Iamges (7*7) Matrix.
plt.show();
"""#Task-2.
--> Our Main, Task is to generate all the random numbers but in the ascending order.
"""
#Importing all the Required and needed Libraries.
import matplotlib.pyplot as plt # Fixed import for subplot
import matplotlib.image as imag
import numpy as np;
#Visuaising the Main data values.
image=data.images;
y=data.target;
print("\n --> Type of the Data contained in particular datasets: ",type(image));
print("\n --> Total Size of the Image Datasets are: ",image.shape);
for i in range(100):
num=np.random.randint(0,1797);
samples_imp=image[num, : , :];
label=y[num];
plt.subplot(10,10,i+1); # Now using plt for subplot
plt.figure(1,figsize=(9,7));
plt.imshow(samples_imp,cmap="gray");
#plt.title(" Actual Target: "+str(label));
plt.axis("off");
#Showing the Valued Graph given Below!
plt.show();
#Importing all the Required and needed Libraries.
import matplotlib.pyplot as plt # Fixed import for subplot
import matplotlib.image as imag
import numpy as np;
#Visuaising the Main data values.
image=data.images;
y=data.target;
print("\n --> Type of the Data contained in particular datasets: ",type(image));
print("\n --> Total Size of the Image Datasets are: ",image.shape);
# Figure out the index of each digit
digit_indices = [np.where(y == i)[0] for i in range(10)]
# Plot digits in increasing order
fig, axes = plt.subplots(10, 10, figsize=(9, 7))
for i in range(10):
for j in range(10):
index = digit_indices[i][j]
axes[i, j].imshow(image[index], cmap="gray")
axes[i, j].axis("off")
#Showing the Graph Finally!
plt.show()
#Now, we have to split the values of the Benchmark.
# like: 60:40 Ratio's.
# Let's we have the 70:40 Ratio, so it contain 70-Testing, 40-Training.
# 80 : 20.
ratio=0.3; #Ratio = 0.3 defines that 30% Data split it into the Testing Phase.
Xtrain,Xtest,ytrain,ytest=model_selection.train_test_split(X,y,test_size=ratio,random_state=5);
print("\n 1. Training Datasets: ",Xtrain.shape," ",ytrain.shape);
print("\n 2. Testing Datasets: ",Xtest.shape," ",ytest.shape);
"""#Creating the Classification Model by using the SVM.
#(SVM) Stands for "Support Vector Machine".
"""
#Creating the Classification Model by using the SVM.
# Support vector Machine (SVM).
clf_model=svm.SVC();
#We, need to train the Model by using the Training Datasets.
clf_model=clf_model.fit(Xtrain,ytrain);
#We, need to pass the Trained model in terms of testing.
#we, also mention labels of testing data.
ypred=clf_model.predict(Xtest);
#Showing the Classification of that model.
print(ypred.shape);
print(ytest.shape);
#Now, we need to measure the overall performance and accuracy in order to measure.
acc=metrics.accuracy_score(ypred,ytest);
conf_matrix=metrics.confusion_matrix(ypred,ytest);
report=metrics.classification_report(ypred,ytest);
#=============== Printing and Displaying all the Values! ===============
print("\n 1. Total Accuracy of the Model are: ",acc);
print("\n-------------------------------------------------------");
print("\n 2. Confusion Matrix given here: \n\n",conf_matrix);
print("\n-------------------------------------------------------");
print("\n 3. Classification overall report: \n\n",report);
print("\n-------------------------------------------------------");
"""#Confusion Matrix of that Particular Datasets."""
#We, are showing the Values of the Confusion Matrix are given Below!
import seaborn as sns;
sns.heatmap(conf_matrix,annot=True,cmap='jet');
plt.title(" Confusion Matrix showing the Digit Datasets!");
plt.show();
"""#Task-4
--> 1. Load the iris Data-Sets.
--> 2. Understand the Dataset and describe by 'DESCR'.
--> 3. And Implement the 70:30 & 60:40 Train Test Split.
--> 4. And Compare it's Accuracy, Precision, Reacll & F1-Score.
--> 5. For, Each Split.
"""
#Now, we are the the Iris-Datasets.
from sklearn import datasets;
from sklearn import svm;
from sklearn import model_selection;
from sklearn import metrics;
#Visualise and load the Datasets Simply!
data=datasets.load_iris();
print(type(data));
#Now, we have to show the Values of the Data.
print("\n 1. Given Iris Datasets after Loading properly Given Below! : \n\n",data);
#print("\n 2. Total Length of the Iris-Datasets are: ",data.shape);
#Understand the Dataset and describe by 'DESCR'.
#Describing the Datasets are by using the DESCR.
print(data.DESCR);
#Now, we splitting the Datasets into ration of Testing and Training Phases.
ratio=0.3;
Xtest,Xtrain,ytest,ytrain=model_selection.train_test_split(X,y,test_size=ratio,random_state=5);
print("\n 1. Training Datasets: ",Xtrain.shape," ",ytrain.shape);
print("\n 2. Testing Datasets: ",Xtest.shape," ",ytest.shape);
#Now, we need to train the Modelby using the SVM.
#SUPORT VECTOR MACHINE - (SVM) generally used for classification of the Models.
clf_model=svm.SVC();
#Train the Model in terms of training.
clf_model=clf_model.fit(Xtrain,ytrain);
#Now, mention the labels in testing for classifications.
#Now, we used to predict by the trained model.
ypred=clf_model.predict(Xtest);
#Showing the Actual Label and predicted Labels.
print("\n Predicted Values: ",ypred.shape);
print("\n Testing Values: ",ytest.shape);
#Now, we need to compare it's valius of the Accuracy, Precision, and it's F1-Score.
acc=metrics.accuracy_score(ypred,ytest);
confu_matrix=metrics.confusion_matrix(ypred,ytest);
report=metrics.classification_report(ypred,ytest);
#Printing and Displaying all the Values in the Iris Classifications are given Below!
print("\n---------------------- IRIS DATASETS PERFORMANCE ----------------------");
print("\n 1. Total Accuracy are: ",acc);
print("\n---------------------------------------------------------");
print("\n 2. Confusion Matrix are Given Below! \n\n",confu_matrix);
print("\n---------------------------------------------------------");
print("\n 3. Classification Report have been given Below! \n\n",report);
print("\n---------------------------------------------------------");
# We, also show the values of the Confusion Matrix.
import seaborn as sns;
import matplotlib.pyplot as plt;
sns.heatmap(confu_matrix,annot=True,cmap="jet");
plt.title("Iris Recognization Datasets with Labels!");
#Showing the finall plotting of the Graph.
plt.figure(1,figsize=(10,8));
plt.show();
# load the iris dataset
# understand the datset from its DESCR
# and implement the 60:40 and 70:30 ratio train test split
# and compare its acc, precision, recall and f1_score
# for each split
from sklearn import metrics,datasets,model_selection,svm
data = datasets.load_iris()
X = data.data
y = data.target
Result = np.zeros((2,4))
ratio= [0.4,0.3]
for i in range(len(ratio)):
Xtrain,Xtest,ytrain,ytest = model_selection.train_test_split(X,y,test_size=ratio[i],random_state=5)
# create the model
clf_model = svm.SVC()
# train the model
clf_model = clf_model.fit(Xtrain,ytrain)
# test the model
ypred = clf_model.predict(Xtest)
# accuracy
temp_acc = metrics.accuracy_score(ypred,ytest)
# precision
temp_pre = metrics.precision_score(ypred,ytest,average='macro')
# recall
temp_rec = metrics.recall_score(ypred,ytest,average='macro')
# f1 score
temp_f1 = metrics.f1_score(ypred,ytest,average='macro')
list_result = [temp_acc,temp_pre,temp_rec,temp_f1]
Result[i,:]=list_result
print(Result)
res = ['Accuracy','Precision','Recall','F1_score']
import pandas as pd
res_df = pd.DataFrame(Result,columns=res,index=['60:40','70:30'])
print(res_df)
res_df.T.plot(kind = 'bar')
"""#----Here,isthe END OF DAY-5 Based on SupervisedLearning.----
#========================== DAY-5 ===========================
"""