1 | import numpy as np |
||
0 ignored issues
–
show
|
|||
2 | from Orange.util import deprecated |
||
3 | |||
4 | |||
5 | class DistMatrix(np.ndarray): |
||
6 | """ |
||
7 | Distance matrix. Extends ``numpy.ndarray``. |
||
8 | |||
9 | .. attribute:: row_items |
||
10 | |||
11 | Items corresponding to matrix rows. |
||
12 | |||
13 | .. attribute:: col_items |
||
14 | |||
15 | Items corresponding to matrix columns. |
||
16 | |||
17 | .. attribute:: axis |
||
18 | |||
19 | If axis=1 we calculate distances between rows, |
||
20 | if axis=0 we calculate distances between columns. |
||
21 | """ |
||
22 | def __new__(cls, data, row_items=None, col_items=None, axis=1): |
||
23 | """Construct a new distance matrix containing the given data. |
||
24 | |||
25 | :param data: Distance matrix |
||
26 | :type data: numpy array |
||
27 | :param row_items: Items in matrix rows |
||
28 | :type row_items: `Orange.data.Table` or `Orange.data.Instance` |
||
29 | :param col_items: Items in matrix columns |
||
30 | :type col_items: `Orange.data.Table` or `Orange.data.Instance` |
||
31 | :param axis: The axis along which the distances are calculated |
||
32 | :type axis: int |
||
33 | |||
34 | """ |
||
35 | obj = np.asarray(data).view(cls) |
||
36 | obj.row_items = row_items |
||
37 | obj.col_items = col_items |
||
38 | obj.axis = axis |
||
39 | return obj |
||
40 | |||
41 | def __array_finalize__(self, obj): |
||
42 | """See http://docs.scipy.org/doc/numpy/user/basics.subclassing.html""" |
||
43 | if obj is None: return |
||
44 | self.row_items = getattr(obj, 'row_items', None) |
||
0 ignored issues
–
show
|
|||
45 | self.col_items = getattr(obj, 'col_items', None) |
||
0 ignored issues
–
show
|
|||
46 | self.axis = getattr(obj, 'axis', 1) |
||
0 ignored issues
–
show
|
|||
47 | |||
48 | def __array_wrap__(self, out_arr, context=None): |
||
49 | if out_arr.ndim == 0: # a single scalar |
||
50 | return out_arr.item() |
||
51 | return np.ndarray.__array_wrap__(self, out_arr, context) |
||
52 | |||
53 | """ |
||
54 | __reduce__() and __setstate__() ensure DistMatrix is picklable. |
||
55 | """ |
||
0 ignored issues
–
show
|
|||
56 | def __reduce__(self): |
||
57 | state = super().__reduce__() |
||
58 | newstate = state[2] + (self.row_items, self.col_items, self.axis) |
||
59 | return state[0], state[1], newstate |
||
60 | |||
61 | def __setstate__(self, state): |
||
62 | self.row_items = state[-3] |
||
0 ignored issues
–
show
|
|||
63 | self.col_items = state[-2] |
||
0 ignored issues
–
show
|
|||
64 | self.axis = state[-1] |
||
0 ignored issues
–
show
|
|||
65 | super().__setstate__(state[0:-3]) |
||
66 | |||
67 | @property |
||
68 | @deprecated |
||
69 | def dim(self): |
||
70 | """Returns the single dimension of the symmetric square matrix.""" |
||
71 | return self.shape[0] |
||
72 | |||
73 | @property |
||
74 | @deprecated |
||
75 | def X(self): |
||
76 | return self |
||
77 | |||
78 | @property |
||
79 | def flat(self): |
||
80 | return self[np.triu_indices(self.shape[0], 1)] |
||
81 | |||
82 | def get_KNN(self, i, k): |
||
0 ignored issues
–
show
|
|||
83 | """Return k columns with the lowest value in the i-th row. |
||
84 | |||
85 | :param i: i-th row |
||
86 | :type i: int |
||
87 | :param k: number of neighbors |
||
88 | :type k: int |
||
89 | """ |
||
90 | idxs = np.argsort(self[i, :])[:] |
||
91 | return self[:, idxs] |
||
92 | |||
93 | def invert(self, typ): |
||
94 | """Invert values in the distance matrix. |
||
95 | |||
96 | :param type: 0 (-X), 1 (1 - X), 2 (max - X), 3 (1 / X) |
||
97 | :type type: int |
||
98 | """ |
||
99 | if typ == 0: |
||
100 | return -self |
||
101 | elif typ == 1: |
||
102 | return 1.-self |
||
103 | elif typ == 2: |
||
104 | return 1./self |
||
105 | else: |
||
106 | raise ValueError('Unknown option for typ of matrix inversion.') |
||
107 | |||
108 | def submatrix(self, row_items, col_items=None): |
||
109 | """Return a submatrix of self, describing only distances between items""" |
||
110 | if not col_items: |
||
111 | col_items = row_items |
||
112 | obj = self[np.ix_(row_items, col_items)] |
||
113 | if obj.row_items: |
||
114 | obj.row_items = self.row_items[row_items] |
||
115 | if obj.col_items: |
||
116 | obj.col_items = self.col_items[col_items] |
||
117 | return obj |
||
118 |
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.