Changeset 91d4ae in git for machine_learning/model/predictor.py
- Timestamp:
- Jul 29, 2019, 2:45:44 PM (5 years ago)
- Branches:
- (u'fieker-DuVal', '117eb8c30fc9e991c4decca4832b1d19036c4c65')(u'spielwiese', 'c5facdfddea2addfd91babd8b9019161dea4b695')
- Children:
- eb2904af1f3fb2d653378807ed3b18780cfd4dbc
- Parents:
- d93ae5668f2da84265ee686cd190828895b49cba
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
machine_learning/model/predictor.py
rd93ae5 r91d4ae 12 12 # Local imports 13 13 from common.keyword_vector import vector_distance, count_occurances, \ 14 read_dictionary14 read_dictionary, normalise_vector 15 15 from common.lookuptable import create_table 16 16 from common.constants import KEYWORDS_FILE … … 48 48 for x in X: # pylint: disable=invalid-name 49 49 # find the closest vector 50 51 50 min_val = float("inf") 52 min_vec = None 51 index = -1 52 i = 0 53 53 for vec in self.vectors: 54 54 dist = vector_distance(x, vec) 55 55 if dist < min_val: 56 56 min_val = dist 57 min_vec = vec 57 index = i 58 i = i + 1 58 59 59 60 # find corresponding filename 60 index = list(self.vectors).index(min_vec)61 61 file = self.files[index] 62 62 ret_list.append(file) … … 70 70 print("Running some tests") 71 71 predictor = HelpPagePredictor() 72 vector1 = {"hello":1, "bye":4, "pizza": 10}73 vector2 = {"hello":2, "bye":3, "pizza": 1}74 vector3 = {"hello":3, "bye":9, "pizza": 3}72 vector1 = normalise_vector([1, 4, 10]) 73 vector2 = normalise_vector([2, 3, 1]) 74 vector3 = normalise_vector([3, 9, 3]) 75 75 76 76 vectors = np.array([vector1, vector2, vector3]) … … 78 78 print(vectors) 79 79 print(files) 80 print() 80 81 81 testvec = {"hello":1, "bye":1, "pizza": 1} 82 testvec = normalise_vector([1, 1, 1]) 83 print("test vector:") 84 print(testvec) 85 print() 82 86 83 87 print("distance to 1") … … 93 97 predictor.fit(vectors, files) 94 98 prediction = predictor.predict(np.array([testvec])) 99 print("Prediction:") 95 100 print(prediction) 101 print() 96 102 97 103 dictionary = read_dictionary(KEYWORDS_FILE) 104 98 105 start = time.time() 99 106 vectors, file_list = create_table(dictionary=dictionary) 100 107 end = time.time() 101 108 print(end - start, "seconds to create_table") 109 102 110 test_vec = count_occurances("extract.lib", dictionary) 103 111 predictor.fit(vectors, file_list) 112 104 113 start = time.time() 105 114 prediction = predictor.predict(np.array([test_vec])) 106 115 end = time.time() 116 print(end - start, "seconds to make prediction") 107 117 print(prediction) 108 print(end - start, "seconds to make prediction")109 118 110 119 if __name__ == '__main__':
Note: See TracChangeset
for help on using the changeset viewer.