-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsbm.py
More file actions
182 lines (142 loc) · 3.71 KB
/
fsbm.py
File metadata and controls
182 lines (142 loc) · 3.71 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys,io
import numpy as np
from time import time
from random import sample
from math import sqrt, log2
class fsbm(object):
"""Feature Selection Based on Modularity"""
def __init__(self, data, distanse = "cosine", profile=True):
self.time = 0
self.distanse = distanse
self.profile = profile
self.data = np.matrix(data, dtype = float)
self.fftime = 0
self.eiitime= 0
self.as2time= 0
self.a2stime= 0
def multi(self,i):
t0 = time()
index = self.mask.A[:,i]
self.fftime += time()-t0
t0 = time()
eii = np.compress(index, np.compress(index, self.A, axis=0) , axis=1).sum()
self.eiitime += time()-t0
t0 = time()
as2 = self.a[self.mask[:,i].T].sum() ** 2
self.as2time += time()-t0
t0 = time()
a2s = self.a2[self.mask[:,i].T].sum()
self.a2stime += time()-t0
return(eii - as2 + a2s)
def ranking(self, sampling = "All"):
print("Feature Seelection Based on Modularity :D")
print("sampling :",sampling)
# サンプリング
n = self.data.shape[0]
if sampling == "sqrt":
self.data = np.matrix(sample(self.data.tolist(), int(sqrt(n))))
elif sampling == "sqrtlog":
self.data = np.matrix(sample(self.data.tolist(), int(sqrt(n)*log2(n))))
self.mask = self.data.astype(bool)
print("Instance :",self.data.shape[0])
print("Features :",self.data.shape[1])
sys.stdout.flush()
# 正規化
def normalize():
if self.profile:
print("normalize : ",end="")
t0 = time()
for i in range(self.data.shape[0]):
self.data[i] = np.divide(self.data[i], np.linalg.norm(self.data[i]))
t1 = time() - t0
self.time += t1
if self.profile:
print(t1,"sec")
sys.stdout.flush()
# 隣接行列の計算
def adjacent():
if self.profile:
print("Ad.matrix : ",end="")
t0 = time()
self.A = self.data.dot(self.data.T)
for i in range(len(self.data)):
self.A[i,i] = 0
self.a = np.array(self.A.sum(0)) # 早いほうで
self.MM = self.a.sum()
self.a = np.divide(self.a,self.MM)
self.A = np.divide(self.A,self.MM)
t1 = time() - t0
self.time += t1
if self.profile:
print(t1,"sec")
sys.stdout.flush()
# スコアの計算
def socoring():
if self.profile:
print("socoring : ",end="")
# t0 = time()
self.a2 = self.a ** 2
# p = Pool(12)
# self.rank = p.map(self.multi,[i for i in range(self.data.shape[1])], 100000)
self.rank = []
t0 = time()
for i in np.arange(self.data.shape[1]):
# t0 = time()
index = self.mask.A[:,i]
# self.fftime += time()-t0
# t0 = time()
# eii = np.compress(index, np.compress(index, self.A, axis=0) , axis=1).sum()
eii = self.A[index,index]
# self.eiitime += time()-t0
# t0 = time()
# as2 = self.a[self.mask[:,i].T].sum() ** 2
as2 = self.a[0,index].sum() ** 2
# self.as2time += time()-t0
# t0 = time()
a2s = self.a2[0,index].sum()
# self.a2stime += time()-t0
self.rank.append(eii - as2 + a2s)
t1 = time() - t0
self.time += t1
if self.profile:
print(t1,"sec")
print("TotalTime :",self.time,"sec\n")
sys.stdout.flush()
# print("density :",self.A.nnz/self.A.shape[0]**2)
normalize()
adjacent()
socoring()
print("ff :",self.fftime)
print("eii:",self.eiitime)
print("as2:",self.as2time)
print("a2s:",self.a2stime)
return(self.rank)
if __name__ == '__main__':
# d=[\
# [1 ,0 ,0 ],\
# [1 ,1 ,0 ],\
# [1 ,0 ,0 ],\
# [0 ,0 ,1 ],\
# [0 ,0 ,1 ],\
# [0 ,1 ,1 ]\
# ]
# d=[\
# [1 ,0 ,0 ,0],\
# [1 ,0 ,0 ,0],\
# [1 ,1 ,0 ,1],\
# [0 ,1 ,1 ,1],\
# [0 ,0 ,1 ,0],\
# [0 ,0 ,1 ,0]\
# ]
d=[[1,0,0,0],
[1,0,0,0],
[1,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1]]
n=fsbm(d)
data=d
print(n.ranking(sampling="all"))