Permalink
Browse files

merge

  • Loading branch information...
cathywu committed Jan 20, 2012
2 parents a0a0553 + e5e4243 commit d9d435b4ae008b04c5c070c5e89657ed199868cf
Showing with 7 additions and 5 deletions.
  1. +7 −5 classifier.py
View
@@ -119,7 +119,6 @@ def classify(self, vec, binary=False):
mx = -sys.maxint
mx_cls = 0
point = ones(self.nfeatures)
-
for feature in vec:
if feature in self.index:
if binary:
@@ -149,12 +148,13 @@ 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:
+ self.binary = True
return ((str(cls) + " ") if cls else "") + \
" ".join(["-".join(str(i).split()) + ":1"
for i in vec if (not self.restrictFeatures) or
@@ -177,7 +177,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
@@ -187,11 +188,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):

0 comments on commit d9d435b

Please sign in to comment.