-
Notifications
You must be signed in to change notification settings - Fork 0
/
kNN.py
150 lines (128 loc) · 4.94 KB
/
kNN.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
from math import sqrt
import numpy as np
def cosine(dataA, dataB):
if type(dataA) is list and type(dataB) is list:
if len(dataA) != len(dataB):
print("Not same length!")
return -1
dataLength = len(dataA)
AB = sum([dataA[i]*dataB[i] for i in range(dataLength)])
normA = sqrt(sum([i**2 for i in dataA]))
normB = sqrt(sum([i**2 for i in dataB]))
denominator = normA * normB
if denominator == 0:
return 0
return AB/denominator
else:
print("Input data invalid!")
return -1
def pearson(dataA, dataB):
if type(dataA) is list and type(dataB) is list:
if len(dataA) != len(dataB):
print("Not same length!")
return -1
dataLength = len(dataA)
intersection = [i for i in range(dataLength) if dataA[i]!=0 and dataB[i]!=0]
#intersection = [i for i in range(dataLength)]
if len(intersection) == 0:
return 0
avgA = np.mean([i for i in dataA if i != 0])
avgB = np.mean([i for i in dataB if i != 0])
#avgA = np.mean([i for i in dataA])
#avgB = np.mean([i for i in dataB])
numerator = sum([(dataA[i] - avgA)*(dataB[i] - avgB) for i in intersection])
deviationA = sqrt(sum([(dataA[i]-avgA)**2 for i in intersection]))
deviationB = sqrt(sum([(dataB[i]-avgB)**2 for i in intersection]))
if(deviationA * deviationB) == 0:
return 0
return numerator/(deviationA*deviationB)
def kNN(data, measure, k = None):
#simulatedData = [[round(np.corrcoef(i,j)[0,1],3) for j in data] for i in data]
simulatedData = [[round(measure(i,j),3) for j in data] for i in data]
#'''
print("Similarity Data")
for i in simulatedData:
print(i)
#'''
print(k)
newData = []
for i in range(len(data)):
newData.append([])
for j in range(len(data[i])):
if(data[i][j] == 0):
#print("i,j = ", i,j)
userCol = [(simulatedData[i][index], index) for index in range(len(simulatedData[i]))]
#print("userCol")
userCol.sort(reverse = True)
#print(userCol)
#taken = userCol[1:k+1]
taken = []
countk = 0
#print("k:",k)
for l in range(0,len(userCol)):
# print(l)
# print("data: ",data[userCol[l][1]][j])
if userCol[l][1] == i:
continue
if (data[userCol[l][1]][j] != 0):
countk += 1
taken.append(userCol[l])
if (countk == k):
#print("noooowat")
break
#print("taken")
#print(taken)
#print(data)
#k = len(taken)
predictingMean = np.mean([index for index in data[i] if index != 0])
similarUserMean = [np.mean([l for l in data[index[1]] if l != 0]) for index in taken]
#print("SimilarUserMean")
#print(similarUserMean)
#print([data[i][taken[k][1]] for k in range(len(taken))])
#print()
#print(simulatedData[i][taken[k][1]], data[taken[k][1]][j], similarUserMean[k])
numerator = sum([simulatedData[i][taken[index][1]]*(data[taken[index][1]][j] - similarUserMean[index]) for index in range(len(taken))])
#print(predictingMean)
denominator = sum([simulatedData[i][taken[index][1]] for index in range(len(taken))])
#print(denominator)
#print([simulatedData[i][taken[k][1]] for k in range(len(taken))])
#print()
if (denominator == 0):
newData[i].append(0)
continue
res = predictingMean + numerator/denominator
newData[i].append(round(res,3))
else:
newData[i].append(data[i][j])
return newData
'''
from scipy import spatial
table2 = [[round(1 - spatial.distance.cosine(i, j),3) for j in data] for i in data]
print()
for i in table2:
print(i)
table3 = [[round(pearson(i,j),3) for j in data] for i in data]
print()
for i in table3:
print(i)
table4 = [[np.corrcoef(i,j)[0,1] for j in data] for i in data]
print()
for i in table4:
print(i)
'''
def readFile(fileString):
return np.loadtxt(fileString, delimiter=" ", dtype = "int").tolist()
if __name__ == "__main__":
fileString = r'F:\Tut\Sem6\Information System\testData\test3.txt'
data = readFile(fileString)
print("DATA:")
for i in data:
print(i)
print("KNN Pearson:")
blah = kNN(data, pearson, 2)
for i in blah:
print(i)
print("\nKNN Cosine")
bleh = kNN(data, cosine, 2)
for i in bleh:
print(i)