spielwiese
Last change
on this file since 1a034b was
1a034b,
checked in by Murray Heymann <heymann.murray@…>, 5 years ago
|
Write basic predictor
|
-
Property mode set to
100644
|
File size:
1.3 KB
|
Line | |
---|
1 | """ |
---|
2 | Define the predictor class for classifying according to help page. |
---|
3 | """ |
---|
4 | # Third party imports |
---|
5 | from sklearn.base import BaseEstimator, ClassifierMixin |
---|
6 | |
---|
7 | # Local imports |
---|
8 | from common.keyword_vector import vector_distance |
---|
9 | |
---|
10 | class HelpPagePredictor(BaseEstimator, ClassifierMixin): |
---|
11 | """ |
---|
12 | Classifier to assign the given input to a Singular helppage. |
---|
13 | """ |
---|
14 | def __init__(self): |
---|
15 | """ |
---|
16 | Define attributes |
---|
17 | """ |
---|
18 | self.vectors = None |
---|
19 | self.files = None |
---|
20 | |
---|
21 | def fit(self, X, y): # pylint: disable=invalid-name |
---|
22 | """ |
---|
23 | Setup the correspondence of vectors to help-files |
---|
24 | """ |
---|
25 | self.vectors = X |
---|
26 | self.files = y |
---|
27 | return self |
---|
28 | |
---|
29 | def predict(self, X): # pylint: disable=invalid-name |
---|
30 | """ |
---|
31 | Classify the input vectors |
---|
32 | """ |
---|
33 | ret_list = [] |
---|
34 | for x in X: # pylint: disable=invalid-name |
---|
35 | # find the closest vector |
---|
36 | |
---|
37 | min_val = float("inf") |
---|
38 | min_vec = None |
---|
39 | for vec in self.vectors: |
---|
40 | dist = vector_distance(x, vec) |
---|
41 | if dist < min_val: |
---|
42 | min_val = dist |
---|
43 | min_vec = vec |
---|
44 | |
---|
45 | # find corresponding filename |
---|
46 | index = self.vectors.index(min_vec) |
---|
47 | file = self.files[index] |
---|
48 | ret_list.append(file) |
---|
49 | return ret_list |
---|
Note: See
TracBrowser
for help on using the repository browser.