Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Made SVMs use frequency/presence info for testing.

  • Loading branch information...
commit e5e42439a9bebf25ab5c3938292bf535a170eb1e 1 parent 7edfef3
@pranjalv123 pranjalv123 authored
Showing with 9 additions and 7 deletions.
  1. +9 −7 classifier.py
View
16 classifier.py
@@ -119,7 +119,6 @@ def classify(self, vec):
mx = -sys.maxint
mx_cls = 0
point = ones(self.nfeatures)
-
for feature in vec:
if feature in self.index:
point[self.index[feature]] += vec[feature]
@@ -146,17 +145,18 @@ def __init__(self, restrictFeatures=False):
self.data = SparseDataSet(0)
self.svm = SVM(optimizer='liblinear')
self.restrictFeatures = restrictFeatures
-
+ self.binary = False
def vectorToString(self, vec, cls = False, binary=False):
# granted, this is kind of silly
# creates a string of the format "[class if point is labeled] feature1:value1 feature2:value2..."
# where the only allowed features are the ones in restrictFeatures, if we're restricting the features
if binary:
- return ((str(cls) + " ") if cls else "") +
+ self.binary = True
+ return ((str(cls) + " ") if cls else "") + \
" ".join(["-".join(str(i).split()) + ":1"
for i in vec if (not self.restrictFeatures) or
(i in self.restrictFeatures)]) + "\n"
- return ((str(cls) + " ") if cls else "") +
+ return ((str(cls) + " ") if cls else "") + \
" ".join(["-".join(str(i).split()) + ":" + str(vec[i])
for i in vec if (not self.restrictFeatures) or
(i in self.restrictFeatures)]) + "\n"
@@ -174,7 +174,8 @@ def compile(self):
self.data = SparseDataSet(self.filename)
self.file = open(self.filename)
self.svm.train(self.data)
-
+ print self.data
+# self.validate(3)
def validate(self, n):
self.compile()
print self.data
@@ -184,11 +185,12 @@ def classify(self, pt):
self.compile()
f = tempfile.NamedTemporaryFile(delete=False)
fname = f.name
- f.write(self.vectorToString(pt))
+ f.write(self.vectorToString(pt, binary = self.binary))
f.close()
data = SparseDataSet(fname)
os.remove(fname)
- return int(self.svm.test(data, verbose=0).getPredictedLabels()[0])
+ r = self.svm.test(data, verbose=0).getPredictedLabels()[0]
+ return int(r)
class MaximumEntropyClassifier(Classifier):
def __init__(self, restrictFeatures=False):
Please sign in to comment.
Something went wrong with that request. Please try again.