-
Notifications
You must be signed in to change notification settings - Fork 2
/
xorWinnow.rb
248 lines (197 loc) · 5 KB
/
xorWinnow.rb
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
if ARGV[2].nil?
$stderr.puts "
takes as input a binary matrix with row and column headers
uses winnow to do an all v all comparison of the rows,
converts scores into edge weights, and outputs a listing
of thresholded edge weights
arg 0 = matrix
arg 1 = recurrence threshold
arg 2 = edge-weight threshold (no lower than 4)
"
raise "not enough arguments given"
end
#---------------------------------------
require 'Matrix'
require 'winnow'
require 'fileTools'
#extend matrix class
#---------------------------------------
class WinMatrix < Matrix
attr_accessor :classifier, :classifierName
def createClass(name)
@classifier = removeRowByName(name)
@classifierName = name
end
#---------------------
def revertClass
insertRow(@classifierName,@classifier)
@classifier = nil
@classifierName = nil
end
#---------------------
def flipClass
@classifier.flipBits!
end
#---------------------
def flipMatrix
@valMatrix.collect!{|arr|
arr.flipBits!
}
end
#---------------------
def removeLowRecurrenceAtts
sums = self.rowSums
posToRm =[]
sums.each_index{|i|
thresh = (ARGV[1].to_f * self.numCols.to_f).to_i
if (sums[i] < thresh)
posToRm << i
end
}
#remove from end, so indeces don't change.
posToRm.sort.reverse.each{|i|
self.removeRow(i)
}
# $stderr.puts "removed #{posToRm.length} low-recurrence attributes" unless posToRm.length == 0
end
#---------------------
def createInstances
arr = []
@valMatrix.first.each_index{|i|
arr << Instance.new(getColByIndex(i),@classifier[i])
}
return arr
end
#---------------------
def copy
tmp = []
@valMatrix.each{|arr| tmp << Array.new(arr)}
zz = WinMatrix.new(tmp, Array.new(@colHeader), Array.new(@rowHeader))
zz.classifier = self.classifier
zz.classifierName = self.classifierName
return zz
end
end
#-----------------------------------------------
class FalseClass
def to_i
return 0
end
def to_f
return 0.0
end
end
class TrueClass
def to_i
return 1
end
def to_f
return 1.0
end
end
#-----------------------------------------------
class ScoredSample
attr_accessor :score, :classifier
def initialize(score, classifier)
@score = score
@classifier = classifier
end
end
#-----------------------------------------------
class Winnow
def calcSeperability(instances,threshold)
samples = []
# first, calculate the score for each attribute,
# where score = Sum(bit*(weight for that att))
tp,fp,tn,fn = 0,0,0,0
instances.each_index{|i|
aclass = instances[i].last
sum = 0
0.upto(instances[i].length-2){|j|
if instances[i][j] == true
sum += @model.weights[j]
end
}
if sum >= threshold
if instances[i].last == true
tp +=1
else
fp += 1
end
else #sum < threshold
if instances[i].last == true
fn += 1
else
tn += 1
end
end
}
sens =tp.to_f/(tp.to_f+fn.to_f)
spec = tn.to_f/(tn.to_f+fp.to_f)
return (sens + spec)
end
def createEdges(name,instances,tfThreshold)
threshScore = ARGV[2].to_f
scores = {}
@model.attributes.each_index{|i|
scores[@model.weights[i]] = 0
}
if scores.keys.sort.reverse[1] > threshScore
threshScore = scores.keys.sort.reverse[1]
end
@model.attributes.each_index{|i|
score = @model.weights[i]
if score >= threshScore
puts "#{name}\tpp\t#{@model.attributes[i]}\t#{score}"
end
}
end
end
#-----------------------------------------------
def readWinMatrix(file)
colHeader = getHeader(file)
colHeader.delete_at(0)
rowHeader = []
matrix = []
tabRead(file,header=true){|arr|
rowHeader << arr.delete_at(0)
matrix << arr
}
return WinMatrix.new(matrix,colHeader,rowHeader)
end
#---------------------
def runWinnow(matrix)
t_class = true
f_class = false
instances = matrix.createInstances
win = Winnow.new
win.initialize_model(matrix.rowHeader, t_class, f_class, instances)
win.threshold = matrix.numRows+1
win.train_model(instances)
win.createEdges(matrix.classifierName, instances, win.threshold)
end
#============================================
matrix = readWinMatrix(ARGV[0])
matrix.to_f!
matrix.removeLowRecurrenceAtts
matrix.to_binary!
# create a flipped copy so we don't have to keep
# flipping bits back and forth
fMatrix = matrix.copy
#use each attribute as a classifier in turn
rows = Array.new(matrix.rowHeader)
rows.each{|rowName|
#first, flip the class
matrix.createClass(rowName)
matrix.flipClass
runWinnow(matrix)
matrix.flipClass #restore it
#then, flip the matrix
fMatrix.createClass(rowName)
fMatrix.flipClass #class starts out flipped, revert to original
runWinnow(fMatrix)
fMatrix.flipClass #restore it
#finally, restore the matrix to it's original state
matrix.revertClass
fMatrix.revertClass
}