-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathML.py
669 lines (481 loc) · 15.1 KB
/
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
import numpy as np
#1-D ARRAY
arr=np.array([1,2,3])
print(arr)
#2-D ARRAY
a=np.array([[1,2,3],[4,5,6]]) m
print(a)
#DIMENSIONS OF ARRAY
print(arr.ndim)
print(a.ndim)
#SHAPE OF ARRAY
print(arr.shape)
print(a.shape)
#changing shape
a.shape=(6,)
print(a)
#question
import numpy as np
arr=np.array([
[1,2,3],
[4,5,6],
[7,8,9]
])
print(arr)
print("dimension of array is",arr.ndim)
print('size of array is',arr.shape)
N=int(input('Enter value of N '))
arr1=arr*N
print(arr1)
arr.shape=(9,)
print('New dimensions is ',arr.ndim)
print(sum(arr))
print(max(arr))
print(min(arr))
#changing it to 3d
arr.shape=(3,3,1)
print(arr)
#arange(start=0,end,diff) , function,where end value is excluded
arr=np.arange(1,51,.25)
print(arr)
#linspace(start,stop,num=50,endpoint=True,retstep=False,dtype=None,axis=0)
#to generate 50 elements from 1 to 10 where 10 is excluded
arr=np.linspace(1,10,endpoint=False)
print(arr)
#to generate 9 elements from 1 to 10 where 10 is included
arr=np.linspace(1,10,9)
print(arr)
#ones(shape,dtype=None) ret a new array of shape given filled with one
arr=np.ones([5,5,5,5,5])
print('array is',arr)
print('array size is',arr.size)
print('array dimensions is',arr.ndim)
print('array shape is',arr.shape)
#zeros same as ones rather filled with zeroes
arr=np.zeros([5,5,5,5,5])
print('array is',arr)
print('array size is',arr.size)
print('array dimensions is',arr.ndim)
print('array shape is',arr.shape)
#MATRIX OPERATIONS
a=np.array([
[1,2,3],
[4,5,6],
[7,8,9]
])
b=np.ones([3,3])
print((a+b))
print((a-b))
print((a*b))#element by element matrix multiplication
print((a.dot(b)))#normal matrix multiplcation
#PANDAS
#pandas series
import pandas as pd
data=pd.Series([11,14,55,64,32,12])
print(data)
#index and values
print(data.index)
print(data.values)
#Addition of two Series
import pandas as pd
students=['Girish Attri','Neha Dhamija','Ravi Verma','Ankit Jain','Nikhil']
term1_marks=[43,35,65,45,49]
term2_marks=[42,49,43,40,42]
s1=pd.Series(term1_marks, students)
s2=pd.Series(term2_marks, students)
total=s1+s2
print(total)
#accessing values
term1_marks=[43,35,65,45,49]
students=['Girish Attri','Neha Dhamija','Ravi Verma','Ankit Jain','Nikhil']
s=pd.Series(term1_marks, students)
print(s['Girish Attri'])
#Filtering
students=['Girish Attri','Neha Dhamija','Ravi Verma','Ankit Jain','Nikhil']
term1_marks=[43,35,65,45,49]
s=pd.Series(term1_marks, students)
pass_students=s[s>50]
fail_students=s[s<50]
print(pass_students, end="\n\n")
print(fail_students, end="\n\n")
#series from dictionary
marks={
"Girish Attri" : 84,
"Ravi verma" : 74,
"Ankit Jain" : 84,
"Nikhil Rastogi" : 89,
"Neha Dhamija" : 84,
"Shrishti Verma" : 90
}
s=pd.Series(marks)
print(s)
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
marks={
"Girish Attri" : 84,
"Ravi verma" : 74,
"Pankaj Sharma" : None,
"Ankit Jain" : 84,
"Nikhil Rastogi" : 89,
"Sanjay Gupta" : None,
"Neha Dhamija" : 84,
"Shrishti Verma" : 90
}
s=pd.Series(marks)
print(s, end="\n\n")
#Filtering NaN
print(s.dropna(), end="\n\n")
#Filling NaN
print(s.fillna(0), end="\n\n")
#Filling Nan with astype
print(s.fillna(0).astype(int), end="\n\n")
marks={
"Girish Attri" : 84,
"Ravi verma" : 74,
"Pankaj Sharma" : None,
"Ankit Jain" : 84,
"Nikhil Rastogi" : 89,
"Sanjay Gupta" : None,
"Neha Dhamija" : 84,
"Shrishti Verma" : 90
}
s=pd.Series(marks)
print(s, end="\n\n")
#Filtering NaN
print(s.dropna(), end="\n\n")
#Filling NaN
print(s.fillna(0), end="\n\n")
#Filling Nan with astype
print(s.fillna(0).astype(int), end="\n\n")
marks={
"Girish Attri" : 84,
"Ravi verma" : 74,
"Pankaj Sharma" : None,
"Ankit Jain" : 84,
"Nikhil Rastogi" : 89,
"Sanjay Gupta" : None,
"Neha Dhamija" : 84,
"Shrishti Verma" : 90
}
s=pd.Series(marks)
print(s, end="\n\n")
#Filtering NaN
print(s.dropna(), end="\n\n")
#Filling NaN
print(s.fillna(0), end="\n\n")
#Filling Nan with astype
print(s.fillna(0).astype(int), end="\n\n")
marks={
"Girish Attri" : 84,
"Ravi verma" : 74,
"Pankaj Sharma" : None,
"Ankit Jain" : 84,
"Nikhil Rastogi" : 89,
"Sanjay Gupta" : None,
"Neha Dhamija" : 84,
"Shrishti Verma" : 90
}
s=pd.Series(marks)
print(s, end="\n\n")
#Filtering NaN
print(s.dropna(), end="\n\n")
#Filling NaN
print(s.fillna(0), end="\n\n")
#Filling Nan with astype
print(s.fillna(0).astype(int), end="\n\n")marks={
"Girish Attri" : 84,
"Ravi verma" : 74,
"Pankaj Sharma" : None,
"Ankit Jain" : 84,
"Nikhil Rastogi" : 89,
"Sanjay Gupta" : None,
"Neha Dhamija" : 84,
"Shrishti Verma" : 90
}
s=pd.Series(marks)
print(s, end="\n\n")
#Filtering NaN
print(s.dropna(), end="\n\n")
#Filling NaN
print(s.fillna(0), end="\n\n")
#Filling Nan with astype
print(s.fillna(0).astype(int), end="\n\n")marks={
"Girish Attri" : 84,
"Ravi verma" : 74,
"Pankaj Sharma" : None,
"Ankit Jain" : 84,
"Nikhil Rastogi" : 89,
"Sanjay Gupta" : None,
"Neha Dhamija" : 84,
"Shrishti Verma" : 90
}
s=pd.Series(marks)
print(s, end="\n\n")
#Filtering NaN
print(s.dropna(), end="\n\n")
#Filling NaN
print(s.fillna(0), end="\n\n")
#Filling Nan with astype
print(s.fillna(0).astype(int), end="\n\n")marks={
"Girish Attri" : 84,
"Ravi verma" : 74,
"Pankaj Sharma" : None,
"Ankit Jain" : 84,
"Nikhil Rastogi" : 89,
"Sanjay Gupta" : None,
"Neha Dhamija" : 84,
"Shrishti Verma" : 90
}
s=pd.Series(marks)
print(s, end="\n\n")
#Filtering NaN
print(s.dropna(), end="\n\n")
#Filling NaN
print(s.fillna(0), end="\n\n")
#Filling Nan with astype
print(s.fillna(0).astype(int), end="\n\n")marks={
"Girish Attri" : 84,
"Ravi verma" : 74,
"Pankaj Sharma" : None,
"Ankit Jain" : 84,
"Nikhil Rastogi" : 89,
"Sanjay Gupta" : None,
"Neha Dhamija" : 84,
"Shrishti Verma" : 90
}
s=pd.Series(marks)
print(s, end="\n\n")
#Filtering NaN
print(s.dropna(), end="\n\n")
#Filling NaN
print(s.fillna(0), end="\n\n")
#Filling Nan with astype
print(s.fillna(0).astype(int), end="\n\n")
#Creating DataFrames using Pandas Series
years = [2014, 2015, 2016, 2017]
shop1 = pd.Series([2409.14, 2941.01, 3496.83, 3119.55], index=years)
shop2 = pd.Series([1203.45, 3441.62, 3007.83, 3619.53], index=years)
shop3 = pd.Series([3412.12, 3491.16, 3457.19, 1963.10], index=years)
shops_df = pd.concat( [shop1, shop2, shop3] ) #Column Wise Concatenation
print(shops_df, end="\n\n")
shops_df = pd.concat( [shop1,shop2,shop3], axis=1)
print(shops_df, end="\n\n")
#Column Names
years = [2014, 2015, 2016, 2017]
shop1 = pd.Series([2409.14, 2941.01, 3496.83, 3119.55], index=years)
shop2 = pd.Series([1203.45, 3441.62, 3007.83, 3619.53], index=years)
shop3 = pd.Series([3412.12, 3491.16, 3457.19, 1963.10], index=years)
shops_df = pd.concat( [shop1,shop2,shop3], axis=1)
col_names = ['Shop 1', 'Shop 2', 'Shop 3']
shops_df.columns = col_names
print(shops_df, end="\n\n")
#Dataframes from Dictionaries'
cities = {"name": ["London", "Berlin", "Madrid", "Rome",
"Paris", "Vienna", "Bucharest", "Hamburg",
"Budapest", "Warsaw", "Barcelona",
"Munich", "Milan"],
"country": ["England", "Germany", "Spain", "Italy",
"France", "Austria", "Romania",
"Germany", "Hungary", "Poland", "Spain",
"Germany", "Italy"],
"population": [8615246, 3562166, 3165235, 2874038,
2273305, 1805681, 1803425, 1760433,
1754000, 1740119, 1602386, 1493900,
1350680],
}
city_frame = pd.DataFrame(cities)
city_frame
print(city_frame["population"]) #as dictionary like way
#or
print(city_frame.population) #as attribute
area = [1572, 891.85, 605.77, 1285,
105.4, 414.6, 228, 755,
525.2, 517, 101.9, 310.4,
181.8]
# area could have been designed as a list, a Series, an array or a scalar
city_frame["area"] = area
print(city_frame)
# Matplotlib
#Matplotlib is a plotting library for Python. It is used along with NumPy to provide an environment that is an effective open source alternative for MatLab
#Importing pyplot
from matplotlib import pyplot as plt
#Plotting to our canvas
plt.plot([1,2,3],[4,5,1])
#Showing what we plotted
plt.show()
x = ['2013','2014','2015','2016','2017']
y = [434,543,346,634,564]
plt.plot(x,y)
plt.title('ABC Pvt Ltd.')
plt.ylabel('Sales')
plt.xlabel('Year')
plt.show()
# **Histograms**
import matplotlib.pyplot as plt
population_ages = [22,55,62,45,21,22,34,42,42,4,99,102,110,120,121,122,130,111,115,112,80,75,65,54,44,43,42,48]
bins = [0,10,20,30,40,50,60,70,80,90,100,110,120,130]
plt.hist(population_ages, bins, histtype='bar', rwidth=0.8)
plt.xlabel('Age Groups')
plt.ylabel('Count')
plt.title('Population Chart')
plt.show()
#Pie Chart
import matplotlib.pyplot as plt
voting = [54354,65443,34634,23423,1233]
candidates = ['A','B','C','D','E']
cols = ['c','m','r','b','g']
plt.pie(voting,
labels=candidates,
colors=cols,
startangle=90,
shadow= True,
explode=(0,0,0,0,0.1),
autopct='%d%%'
)
plt.title('Candidate wise Voting %')
plt.show()
#Scikit Learn LinearRegression Classifier
#---------------------------------------
import numpy as np
from sklearn.linear_model import LinearRegression
x = np.array([1,2,3,4,5])
x.shape=(-1,1)
y = np.array([3,4,5,6,7])
#Create Classifier Object
clf = LinearRegression()
#Train the Classifier
clf.fit(x,y)
#Evaluating the Accuracy of the Model
score=clf.score(x,y)
print("Accuracy of The Model : ", score)
#Predictions
predict_x=np.array([6,8,9])
predict_x.shape=(-1,1)
predict_y=clf.predict(predict_x)
print("Prediction Data : ", predict_y)
LabelEncoder
-------------
Encode labels with value between 0 and n_classes-1.
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit([1, 2, 2, 6])
LabelEncoder()
le.classes_
array([1, 2, 6])
le.transform([1, 1, 2, 6])
array([0, 0, 1, 2]...)
le.inverse_transform([0, 0, 1, 2])
array([1, 1, 2, 6])
Handling Null/NaN data in a dataset.
Filling Missing Values with Mean data.
s1=pd.Series([34,65,34,None,43,54,None,23,54,65,34,None])
#Mean
mn=s1.mean()
#Fill
s1=s1.fillna(mn)
print(s1)
Imputer
--------
For various reasons, many real world datasets contain missing values, often encoded as blanks, NaNs or other placeholders. Such datasets however are incompatible with scikit-learn estimators which assume that all values in an array are numerical, and that all have and hold meaning. A basic strategy to use incomplete datasets is to discard entire rows and/or columns containing missing values. However, this comes at the price of losing data which may be valuable (even though incomplete). A better strategy is to impute the missing values, i.e., to infer them from the known part of the data.
sklearn.impute.SimpleImputer
-----------------------------
The SimpleImputer class provides basic strategies for imputing missing values. Missing values can be imputed with a provided constant value, or using the statistics (mean, median or most frequent) of each column in which the missing values are located.
#Mean
from sklearn.impute import SimpleImputer
import pandas as pd
import numpy as np
s1=np.array([34,65,34,None,43,54,None,23,54,65,34,None])
s1.shape=(-1,1)
imp = SimpleImputer(missing_values=np.nan, strategy='mean')
imp.fit(s1)
#Filling Data
s1=imp.transform(s1)
print(s1)
#Most Frequent
from sklearn.impute import SimpleImputer
import pandas as pd
import numpy as np
s1=np.array([34,65,34,None,43,54,None,34,54,65,34,None])
s1.shape=(-1,1)
imp = SimpleImputer(strategy='most_frequent')
imp.fit(s1)
#Filling Data
s1=imp.transform(s1)
print(s1)
import numpy as np
from sklearn.linear_model import LinearRegression
#Converting Text Data to Numbers
def Mapper(data):
unique=list(set(data))
unique.sort()
count=1
map_data={}
for value in unique:
map_data[value]=count
count+=1
return map_data
def ConvertTextToNumber(data,maps):
for i in range(len(data)):
data[i]=maps[data[i]]
xdata=['A','B','C','D','A','C','B','D','C','E']
ydata=[1.1,3.0,4.2,7,1.2,4.3,3.1,7.7,4.5,10.2]
maps=Mapper(xdata)
ConvertTextToNumber(xdata,maps)
x = np.array(xdata)
x.shape=(-1,1)
y = np.array(ydata)
#Create Classifier Object
clf = LinearRegression()
#Train the Classifier
clf.fit(x,y)
#Evaluating the Accuracy of the Model
score=clf.score(x,y)
print("Accuracy of The Model : ", score)
#Predictions
predictdata=['A','E']
ConvertTextToNumber(predictdata,maps)
predict_x=np.array(predictdata)
predict_x.shape=(-1,1)
predict_y=clf.predict(predict_x)
print("Prediction Data : ", predict_y)
#PRE PROCESSING PART
import numpy as np
from sklearn import preprocessing , neighbors , svm , model_selection
import pandas as pd
df= pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data')
df.replace('?',-99999,inplace=True)
col_names=['id','clump_thickness','cell_size','cell shape','adhesion','epithelial','nuclei','brandchlomatin','nucleoli','mitoses','class']
df.columns=col_names
print(df.head())
df.drop(['id'],1,inplace=True)
X=np.array(df.drop(['class'],1))
y=np.array(df['class'])
X_train,X_test,y_train,y_test = model_selection.train_test_split(X,y,test_size=0.2)
clf=neighbors.KNeighborsClassifier()
clf.fit(X_train,y_train)
confidence=clf.score(X_test,y_test)
print("Confidence is :- ", int(confidence*100),'%')
example_measures=np.array([4,.2,1,1,1,2,3,2,1])
example_measures.shape=(1,-1)
prediction=clf.predict(example_measures)
print('Cancer state : ','Benign' if prediction==2 else 'Malignant')
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split , GridSearchCV
pipeline=Pipeline([('clf',svm.SVC(kernel='linear',C=1,gamma=1))])
params={'clf_C': (0.1,0.5,1,2,5,10,20)
}
clf=svm.SVC()
clf.fit(X_train,y_train)
confidence=clf.score(X_test,y_test)
print("Confidence is :- ", int(confidence*100),'%')
example_measures=np.array([4,.2,1,1,1,2,3,2,1])
example_measures.shape=(1,-1)
prediction=clf.predict(example_measures)
print('Cancer state : ','Benign' if prediction==2 else 'Malignant')
clf=svm.SVC()
clf.fit(X_train,y_train)
confidence=clf.score(X_test,y_test)
print("Confidence is :- ", int(confidence*100),'%')
example_measures=np.array([4,.2,1,1,1,2,3,2,1])
example_measures.shape=(1,-1)
prediction=clf.predict(example_measures)
print('Cancer state : ','Benign' if prediction==2 else 'Malignant')