FRsūtil̲s
A fuzzy-rough reasoning utilities library
 
Loading...
Searching...
No Matches
owafrs.py
Go to the documentation of this file.
1# frutil/models/owafrs.py
2"""
3OWAFRS implementation.
4"""
5import sys
6import os
7
8sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
9
10import owa_weights as owa_weights
11from approximations import FuzzyRoughModel
12import numpy as np
13
15 def __init__(self, similarity_matrix: np.ndarray,
16 labels: np.ndarray,
17 tnorm,
18 implicator,
19 lower_app_weights_method : str,
20 upper_app_weights_method : str):
21 super().__init__(similarity_matrix, labels)
22 self.tnorm = tnorm
23 self.implicator = np.vectorize(implicator)
24
25 # sum_vals = np.sum(lower_approximation_weights) + np.sum(upper_approximation_weights)
26 # assert np.isclose(sum_vals, 2.0), "lower & upper weights, each must sum up to 1.0"
27 # assert not np.all((lower_approximation_weights >= 0) & (lower_approximation_weights <= 1))
28 # assert not np.all((upper_approximation_weights >= 0) & (upper_approximation_weights <= 1))
29 # assert not len(lower_approximation_weights) == len(upper_approximation_weights)
30
31 if upper_app_weights_method not in ['sup_weights_linear']:
32 raise ValueError(f"Unsupported weight type: {upper_app_weights_method}")
33 if lower_app_weights_method not in ['inf_weights_linear']:
34 raise ValueError(f"Unsupported weight type: {lower_app_weights_method}")
35
36 n = len(labels)
37 # We generate one less element regarding weights, because in calculations of
38 # OWA, the same instance will be excluded from calculations. Therefore,
39 # lower and upper approximations have slightly higher values which are
40 # more realistic
41 if(upper_app_weights_method == 'sup_weights_linear'):
42 self.upper_approximation_weights = owa_weights._owa_suprimum_weights_linear(n - 1)
43 if(lower_app_weights_method == 'inf_weights_linear'):
44 self.lower_approximation_weights = owa_weights._owa_infimum_weights_linear(n - 1)
45
46
47
49 label_mask = (self.labels[:, None] == self.labels[None, :]).astype(float)
50 implication_vals = self.implicator(self.similarity_matrix, label_mask)
51
52 # Since for the calculations of lower approximation,
53 # we use OWA operator which is a essentailly a product,
54 # to exclude the same instance from calculations we set
55 # the diagonal to 0.0 which
56 # is ignored by the product operator.
57
58 np.fill_diagonal(implication_vals, 0.0)
59 sorted_matrix = np.sort(implication_vals, axis=1)
60 sorted_matrix = sorted_matrix[:, ::-1]
61 sorted_matrix = sorted_matrix[:, :-1]
62
63 result = np.matmul(sorted_matrix, self.lower_approximation_weights)
64 return result
65
67 label_mask = (self.labels[:, None] == self.labels[None, :]).astype(float)
68 implication_vals = self.implicator(self.similarity_matrix, label_mask)
69
70 # Since for the calculations of upper approximation,
71 # we use OWA operator which is a essentailly a product,
72 # to exclude the same instance from calculations we set
73 # the diagonal to 0.0 which
74 # is ignored by the product operator.
75
76 np.fill_diagonal(implication_vals, 0.0)
77 sorted_matrix = np.sort(implication_vals, axis=1)
78 sorted_matrix = sorted_matrix[:, ::-1]
79 sorted_matrix = sorted_matrix[:, :-1]
80
81 result = np.matmul(sorted_matrix, self.upper_approximation_weights)
82 return result
__init__(self, np.ndarray similarity_matrix, np.ndarray labels, tnorm, implicator, str lower_app_weights_method, str upper_app_weights_method)
Definition owafrs.py:20