Vector.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. try:
  2. import cPickle as pickle
  3. except ImportError:
  4. import pickle
  5. class FeatureVector:
  6. def __init__(self):
  7. self.features = []
  8. self.activefeatures = []
  9. self.classification = None
  10. # set which features are active using a binary list
  11. # ex: For 2nd, 4th and 5th features to be active, pass [0,1,0,1,1]
  12. def setFeatures(self, binlist):
  13. activefeatures = []
  14. if len(binlist) != len(self.features):
  15. print("Feature choice list must equal length of feature list")
  16. for b in range(len(binlist)):
  17. if binlist[b] == 1:
  18. activefeatures.append(self.features[b])
  19. self.activefeatures = activefeatures
  20. def __repr__(self):
  21. for f in self.features:
  22. print(f)
  23. print("Classification:" + self.classification)
  24. def writePickledData(filename):
  25. with open(filename, 'wb') as file:
  26. pickle.dump(set([1, 2, 3]), file)
  27. def readPickledData(filename):
  28. with open(filename, 'rb') as file:
  29. x = pickle.load(file)
  30. print(x)
  31. return x
  32. def main():
  33. fv = FeatureVector()
  34. writePickledData("test.txt")
  35. readPickledData("test.txt")
  36. if __name__ == '__main__':
  37. main()