-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project1_Q1(b)_Alishbah_Fahad_1001924185.py
205 lines (179 loc) · 8.59 KB
/
Project1_Q1(b)_Alishbah_Fahad_1001924185.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
#!/usr/bin/env python
# coding: utf-8
# # Q1(b)
# ### Importing Libraries and Data
# In[1]:
import numpy as np
Training_Data=np.genfromtxt(r"C:\Users\alish\OneDrive\Documents\Alishbah\CSE6363_Machine Learning\Project-1\axf4185_project_1\dataset\Training Data.csv", delimiter=',', skip_header=1, usecols = (0, 1, 2))
Label=np.genfromtxt(r"C:\Users\alish\OneDrive\Documents\Alishbah\CSE6363_Machine Learning\Project-1\axf4185_project_1\dataset\Training Data.csv", delimiter=',', skip_header=1, usecols = 3, dtype=str)
Test_Data=np.genfromtxt(r"C:\Users\alish\OneDrive\Documents\Alishbah\CSE6363_Machine Learning\Project-1\axf4185_project_1\dataset\Test Data.csv", delimiter=',', skip_header=1, usecols = (0, 1, 2))
print("Training Data:")
print(Training_Data)
print()
print("Label:")
print(Label)
print()
print("Test Data:")
print(Test_Data)
# ### KNN MODEL using Euclidean Distance
# In[2]:
def KNN_MODEL_Euclidean(Train, Test, k):
### Calculating Euclidean Distance
def EDM(A,B):
difference = A[:,None] - B[None,:]
power_difference = np.power(np.abs(difference), 2)
return power_difference.sum(axis=-1) ** (1 / 2)
### Finding Closest Neighbor by using Euclidean Distance
def closest_neighbor_EDM(Train, Test, k):
distances = EDM(Training_Data,Test_Data)
indices = np.argsort(distances, 0)
distances = np.sort(distances,0)
return indices[0:k, : ], distances[0:k, : ]
### Predicting the Gender by using Euclidean Distance
def prediction_EDM(Train,Target,Test,k):
indices, distances = closest_neighbor_EDM(Training_Data, Test_Data, k)
Target = Target.flatten()
rows, columns = indices.shape
prediction = list()
for j in range(columns):
T = list()
for i in range(rows):
cell = indices[i][j]
T.append(Target[cell])
prediction.append(max(T,key=T.count))
prediction=np.array(prediction)
return prediction
return EDM(Training_Data,Test_Data),closest_neighbor_EDM(Training_Data,Test_Data, k),prediction_EDM(Training_Data,Label,Test_Data,k)
# ### KNN MODEL using Manhattan Distance
# In[3]:
def KNN_MODEL_Manhattan(Train, Test, k):
### Calculating Manhattan Distance
def ManhattanDM(A, B):
return np.abs(A[:,0,None] - B[:,0]) + np.abs(A[:,1,None] - B[:,1]) + np.abs(A[:,2,None] - B[:,2])
### Finding Closest Neighbor by using Manhattan Distance
def closest_neighbor_Manhattan(Train, Test, k):
distances_Manhattan = ManhattanDM(Training_Data,Test_Data)
indices = np.argsort(distances_Manhattan, 0)
distances_Manhattan = np.sort(distances_Manhattan,0)
return indices[0:k, : ], distances_Manhattan[0:k, : ]
### Predicting the Gender by using Manhattan Distance
def prediction_Manhattan(Train,Target,Test,k):
indices, distances_Manhattan = closest_neighbor_Manhattan(Train,Test,k)
Target = Target.flatten()
rows, columns = indices.shape
prediction = list()
for j in range(columns):
T = list()
for i in range(rows):
cell = indices[i][j]
T.append(Target[cell])
prediction.append(max(T,key=T.count))
prediction=np.array(prediction)
return prediction
return ManhattanDM(Training_Data, Test_Data),closest_neighbor_Manhattan(Training_Data, Test_Data, k), prediction_Manhattan(Training_Data,Label,Test_Data,k)
# ### KNN MODEL using Minkowski Distance
# In[4]:
def KNN_MODEL_Minkowski(Train, Test, k):
### Calculating Minkowski Distance
def MinkowskiDM(A,B,p):
difference = A[:,None] - B[None,:]
power_difference = np.power(np.abs(difference), p)
return power_difference.sum(axis=-1) **(1 / p)
### Finding Closest Neighbor by using Minkowski Distance
def closest_neighbor_Minkowski(Train, Test, k):
distances_Minkowski = MinkowskiDM(Training_Data,Test_Data,3)
indices = np.argsort(distances_Minkowski, 0)
distances_Minkowski = np.sort(distances_Minkowski,0)
return indices[0:k, : ], distances_Minkowski[0:k, : ]
### Predicting the Gender by using Minkowski Distance
def prediction_Minkowski(Train,Target,Test,k):
indices, distances_Minkowski = closest_neighbor_Minkowski(Train,Test,k)
Target = Target.flatten()
rows, columns = indices.shape
prediction = list()
for j in range(columns):
T = list()
for i in range(rows):
cell = indices[i][j]
T.append(Target[cell])
prediction.append(max(T,key=T.count))
prediction=np.array(prediction)
return prediction
return MinkowskiDM(Training_Data,Test_Data,3),closest_neighbor_Minkowski(Training_Data, Test_Data, k), prediction_Minkowski(Training_Data,Label,Test_Data,k)
# ### KNN MODEL using Euclidean, Manhattan, and Minkowski Distances
# In[5]:
def KNN_MODEL(Train, Test, k):
### Calculating Euclidean Distance
def EDM(A,B):
difference = A[:,None] - B[None,:]
power_difference = np.power(np.abs(difference), 2)
return power_difference.sum(axis=-1) ** (1 / 2)
### Finding Closest Neighbor by using Euclidean Distance
def closest_neighbor_EDM(Train, Test, k):
distances = EDM(Training_Data,Test_Data)
indices = np.argsort(distances, 0)
distances = np.sort(distances,0)
return indices[0:k, : ], distances[0:k, : ]
### Predicting the Gender by using Euclidean Distance
def prediction_EDM(Train,Target,Test,k):
indices, distances = closest_neighbor_EDM(Train, Test, k)
Target = Target.flatten()
rows, columns = indices.shape
prediction = list()
for j in range(columns):
T = list()
for i in range(rows):
cell = indices[i][j]
T.append(Target[cell])
prediction.append(max(T,key=T.count))
prediction=np.array(prediction)
return prediction
### Calculating Manhattan Distance
def ManhattanDM(A, B):
return np.abs(A[:,0,None] - B[:,0]) + np.abs(A[:,1,None] - B[:,1]) + np.abs(A[:,2,None] - B[:,2])
### Finding Closest Neighbor by using Manhattan Distance
def closest_neighbor_Manhattan(Train, Test, k):
distances_Manhattan = ManhattanDM(Training_Data,Test_Data)
indices = np.argsort(distances_Manhattan, 0)
distances_Manhattan = np.sort(distances_Manhattan,0)
return indices[0:k, : ], distances_Manhattan[0:k, : ]
### Predicting the Gender by using Manhattan Distance
def prediction_Manhattan(Train,Target,Test,k):
indices, distances_Manhattan = closest_neighbor_Manhattan(Train,Test,k)
Target = Target.flatten()
rows, columns = indices.shape
prediction = list()
for j in range(columns):
T = list()
for i in range(rows):
cell = indices[i][j]
T.append(Target[cell])
prediction.append(max(T,key=T.count))
prediction=np.array(prediction)
return prediction
### Calculating Minkowski Distance
def MinkowskiDM(A,B,p):
difference = A[:,None] - B[None,:]
power_difference = np.power(np.abs(difference), p)
return power_difference.sum(axis=-1) **(1 / p)
### Finding Closest Neighbor by using Minkowski Distance
def closest_neighbor_Minkowski(Train, Test, k):
distances_Minkowski = MinkowskiDM(Training_Data,Test_Data,3)
indices = np.argsort(distances_Minkowski, 0)
distances_Minkowski = np.sort(distances_Minkowski,0)
return indices[0:k, : ], distances_Minkowski[0:k, : ]
### Predicting the Gender by using Minkowski Distance
def prediction_Minkowski(Train,Target,Test,k):
indices, distances_Minkowski = closest_neighbor_Minkowski(Train,Test,k)
Target = Target.flatten()
rows, columns = indices.shape
prediction = list()
for j in range(columns):
T = list()
for i in range(rows):
cell = indices[i][j]
T.append(Target[cell])
prediction.append(max(T,key=T.count))
prediction=np.array(prediction)
return prediction
return EDM(Training_Data,Test_Data),closest_neighbor_EDM(Training_Data,Test_Data, k),prediction_EDM(Training_Data,Label,Test_Data,k),ManhattanDM(Training_Data, Test_Data),closest_neighbor_Manhattan(Training_Data, Test_Data, k), prediction_Manhattan(Training_Data,Label,Test_Data,k), MinkowskiDM(Training_Data,Test_Data,3),closest_neighbor_Minkowski(Training_Data, Test_Data, k), prediction_Minkowski(Training_Data,Label,Test_Data,k)