1
+ import pandas as pd
2
+ from sklearn .preprocessing import MinMaxScaler #sadece veri normalizasyonu için sklearn kullandım
3
+ scaler = MinMaxScaler ()
4
+ import time
5
+ start = time .time ()
6
+ i = 0
7
+ j = 1
8
+ y = 0
9
+ deger = 0
10
+ liste_ar = []
11
+ son = 0
12
+
13
+ ###############################################################
14
+ df = pd .read_excel ("Dry_Bean_Dataset.xlsx" )
15
+ df .iloc [:,0 :- 1 ] = scaler .fit_transform (df .iloc [:,0 :- 1 ].to_numpy ()) # verileri 0-1 arasında normalize ettik
16
+ df1 = df .copy ()
17
+ df1 ['index_col' ] = df .index
18
+ ###############################################################
19
+
20
+
21
+ train = df .sample (frac = 0.7 ,random_state = 61 ) #train sample
22
+
23
+ test = df .drop (train .index ) #test sample
24
+
25
+ hesaplanacak = pd .DataFrame (train .iloc [0 :1 ,:]) #sürekli değisecek olan classını tahmın edecegımız veri
26
+
27
+ y_sinif = hesaplanacak ["Class" ].values [0 ] # var olan classımız budur
28
+
29
+ hesaplanacak = hesaplanacak .drop ("Class" ,axis = 1 ) #hesaplayacagımızın sadece verılerını tutuyoruz classı cıkardık.
30
+
31
+ train_class = train .drop ("Class" ,axis = 1 ) # var olan tumm verıler classları ayır
32
+
33
+ train_class = train .drop (hesaplanacak .index )
34
+
35
+
36
+ len_test = hesaplanacak .shape [0 ]
37
+ len_train = train_class .shape [0 ]
38
+ len_columns = train_class .shape [1 ]
39
+
40
+
41
+ def hesapla_class (liste_ar ,k_changed ):
42
+
43
+ sorted_lenght = sorted (liste_ar ,key = lambda liste_ar :liste_ar [1 ], reverse = False )# uzaklıklara göre sıraliyoruz
44
+ count = 0
45
+ clas_aga = []
46
+ while (1 ):
47
+ if (count == k_changed ):
48
+ break
49
+ indexler = (sorted_lenght [count ][0 ])
50
+ clas_aga .append (df1 [df1 ["index_col" ]== indexler ]["Class" ].values [0 ])
51
+ count += 1
52
+ y_head = max (clas_aga ,key = clas_aga .count )
53
+ liste_ar .clear ()
54
+ return y_head
55
+
56
+ count_true = 0
57
+ count_false = 0
58
+
59
+ def knn (df ,train_class ,hesaplanacak ,y_sinif ,k_changed ):
60
+ global count_true
61
+ global count_false
62
+ global len_columns
63
+ global liste_ar
64
+ global i
65
+ global y
66
+ global deger
67
+ global j
68
+ global son
69
+ try :
70
+ while (1 ):
71
+
72
+ if (y == len_columns - 1 ):
73
+ sonuc = deger ** 0.5
74
+
75
+ liste_ar .append ((train_class .index .values [i ],sonuc )) # indexleri ve uzaklıkları lıste_ar'ın içine atıyoruz ileride sıralamak için
76
+ #print(liste_ar)
77
+ sonuc = 0
78
+ deger = 0
79
+ i += 1
80
+ y = 0
81
+ if (len_train <= i ): # doğruluk oranı ıcın hesaplamaları bunun ıcınde anlık olarak tutuyorum
82
+
83
+ tahmin = hesapla_class (liste_ar ,k_changed )
84
+ #print("Class",y_sinif)
85
+
86
+ if (y_sinif == tahmin ):
87
+ #print("{} GERCEK DEGERİM , {} TAHMİN DEGERİM ".format(y_sinif,tahmin))
88
+ count_true += 1
89
+ else :
90
+ #print("{} GERCEK DEGERİM , {} TAHMİN DEGERİM ".format(y_sinif,tahmin))
91
+ count_false += 1
92
+
93
+ j += 1
94
+ hesaplanacak = pd .DataFrame (df .iloc [j :(j + 1 ),:])
95
+ y_sinif = hesaplanacak ["Class" ].values [0 ]
96
+ hesaplanacak = hesaplanacak .drop ("Class" ,axis = 1 ) #class_ hesaplanacakın classını tuttuk
97
+ train_class = df .drop ("Class" ,axis = 1 ) # var olan tumm verıler classları ayır
98
+ train_class = df .drop (hesaplanacak .index )
99
+
100
+ sonuc = 0
101
+ deger = 0
102
+ i = 0
103
+ y = 0
104
+
105
+ continue
106
+
107
+ deger = deger + (train_class .iloc [i ,y ]- hesaplanacak .iloc [0 ,y ])** 2 #öklid hesabı için karekok harıc olan kısım
108
+ y += 1
109
+
110
+ except :
111
+
112
+ son = (count_true / (count_true + count_false ))* 100
113
+ #print("doğruluk orani",son)
114
+
115
+ try :
116
+ k_changed = int (input ("Bir 'K' parametresi giriniz " ))
117
+ except :
118
+ print ("Hatali giris cikis yapiliyor" )
119
+ exit ()
120
+ knn (train ,train_class ,hesaplanacak ,y_sinif ,k_changed )
121
+ print ("Train için doğruluk orani {:.2f} " .format (son ))
122
+
123
+ print ("----------" )
124
+
125
+ i = 0
126
+ j = 1
127
+ y = 0
128
+ deger = 0
129
+ liste_ar = []
130
+ count_true = 0
131
+ count_false = 0
132
+ son = 0
133
+ hesaplanacak = pd .DataFrame (test .iloc [0 :1 ,:])
134
+
135
+ y_sinif = hesaplanacak ["Class" ].values [0 ]
136
+
137
+ hesaplanacak = hesaplanacak .drop ("Class" ,axis = 1 ) #class_ hesaplanacakın classını tuttuk
138
+
139
+ train_class = test .drop ("Class" ,axis = 1 ) # var olan tumm verıler classları ayır
140
+
141
+ train_class = test .drop (hesaplanacak .index )
142
+
143
+ len_test = hesaplanacak .shape [0 ]
144
+ len_train = train_class .shape [0 ]
145
+ len_columns = train_class .shape [1 ]
146
+
147
+ knn (test ,train_class ,hesaplanacak ,y_sinif ,k_changed )
148
+ print ("Test için doğruluk orani {:.2f} " .format (son ))
149
+
150
+ end = time .time ()
151
+ sonucc = end - start
152
+ print ("Hesaplama zamani" ,sonucc )
0 commit comments