Vector.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. return str(self.features)
  22. def writePickledData(filename):
  23. v = FeatureVector()
  24. v.features = [1, 2, 3, 4]
  25. v.activefeatures = [1, 2, 3, 4]
  26. vs = [v,v]
  27. with open(filename, 'wb') as file:
  28. pickle.dump(vs, file)
  29. def readPickledData(filename):
  30. with open(filename, 'rb') as file:
  31. x = pickle.load(file)
  32. # print(x)
  33. return x
  34. def main():
  35. fv = FeatureVector()
  36. writePickledData("test.bin")
  37. readPickledData("test.txt")
  38. if __name__ == '__main__':
  39. main()