-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNN.py
137 lines (108 loc) · 3.69 KB
/
NN.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
# -*- coding: utf-8 -*-
__author__ = 'Mage'
import util
from numpy import *
def genMinMaxT(dataSet,begin,end,k,accuracy=4,Z1=0):
""" Get range of T for target k clusters
Input:
dataSet (list): Input dataset
begin (int) : Start value of T
end (int) : End value of T
k (int) : Number of target classifications
accuracy (int): Accurate to the number of decimal places
Z1 (int)) : Index of first clustering center
Output:
min(float),max(float)
Minimum and maximum T of the number of target classifications
"""
status = 0
step = 1
count = accuracy+1
while True:
if status == 0:
#Dichotomy approach
tmp = float((begin+end)/2)
else:
if status%2 == 1:
#Approach the lower limit in steps of step, step / 10 and step / 100
tmp = begin + step/power(10,(status-1)//2)
if status%2 == 0:
#Approach the upper limit in steps of step, step / 10 and step / 100
tmp = end - step/power(10,(status-1)//2)
zs,zsi = genClusters(dataSet,tmp,Z1)
if len(zsi) > k:
print("update begin {:.5f}".format(tmp))
begin = tmp
if len(zsi) < k:
print("update end {:.5f}".format(tmp))
end = tmp
if len(zsi) == k:
if status == 2*count:
break
else:
status = status + 1
print("begin:{:.5f},end:{:.5f}".format(begin,end))
return begin,end
def genClusters(dataSet,T,Z1=0):
""" Get number of clusters
Input:
dataSet (list) : Input dataset
T (float) : Distance threshold
Z1 (int)) : Index of first clustering center
Output:
List of clustering center
"""
if Z1 >= len(dataSet) or Z1 < 0:
Z1 = 0
zs = [dataSet[Z1]]
zsi = [Z1]
for i in range(len(dataSet)):
minDist = util.distEclud(dataSet[i],zs[0])
#print(minDist)
for z in zs:
dist = util.distEclud(dataSet[i],z)
if dist < minDist:
minDist = dist
if minDist > T:
zs.append(dataSet[i])
zsi.append(i)
return zs,zsi
def NN(dataSet,T,Z1=0):
""" Implemention of NN classify
Input:
dataSet (list) : Input dataset
T (float) : Distance threshold
Z1 (int)) : Index of first clustering center
Output:
"""
zs,zsi = genClusters(dataSet,T,Z1)
clusteri,clusterz = util.classify(dataSet,zs,zsi)
return clusteri,clusterz
# for data in dataSet:
if __name__ == "__main__":
dataSet = util.loadDataSet("EEG_feature.txt")
labels = util.loadDataSet("valence_arousal_label.txt")
Z1 = 882 #First cluster center
beginT = 1 #Dist threshold begin val
endT = 1000 #Dist threshold end val
accuracy = 4 #Accurate to the number of decimal places
k = 5 #Target number of clusters
# min,max=genMinMaxT(dataSet,beginT,endT,k,accuracy,Z1)
# print(min,max)
T = 1
zs,zsi = genClusters(dataSet,T,Z1)
print(zsi)
# T = (min+max)/2
# clusteri,clusterz = NN(dataSet,T,Z1)
# for i in range(len(clusteri)):
# print("分类:{:d}".format(i))
# c1=[1.0]
# for inx in clusteri[i]:
# print("编号:{:d},标签:{}".format(inx,labels[inx]))
# for a in util.floatrange(0.01,1,10):
# T = min + (max-min)*a
# print(T)
# zs,zsi = genClusters(dataSet,T,Z1)
# print(zsi)
# for i in zsi:
# print(labels[i])