def test_cross_product_kernel_linear(self): val=1*5*0.3*0.7 + 1*6*0.3*0.8 + 2*5 * 0.4 *0.7 +2*6*0.4*0.8 X = FuzzySet([1, 2], membership_degrees= [0.3, 0.4]) Y = FuzzySet([5, 6], membership_degrees= [0.7, 0.8]) self.assertAlmostEqual(kernels.cross_product_kernel(X, Y, kernels.linear_kernel, '', kernels.linear_kernel, ''), val, places=3, msg="Should be equal" ) self.assertAlmostEqual(kernels.cross_product_kernel(X, Y, pairwise.linear_kernel, '', pairwise.linear_kernel, ''), val, places=3, msg="Should be equal" )
def quantile_fuzzification_classification(self): ''' Algorithm 1 from https://hal.archives-ouvertes.fr/hal-01438607/document ''' grouped = self._data.groupby([self._target]) self._epistemic_values = grouped.transform(lambda x: np.exp(-np.square(x - x.quantile(0.5)) / (np.abs(x.quantile(0.75) - x.quantile(0.25)) / ( 2 * np.sqrt(2 * np.log(2)))) ** 2 )) # join data and epistemistic values num_rows = self._epistemic_values.shape[0] num_cols = self._epistemic_values.shape[1] self._fuzzydata = np.asarray([[FuzzySet(elements=self._data.iloc[j, i], membership_degrees=self._epistemic_values.iloc[j, i]) for i in range(num_cols)] for j in range(num_rows)])
def transform(self,X,y=None): num_rows = X.shape[0] num_cols = X.shape[1] return np.asarray([[FuzzySet(membership_function_params=[X[j, i], self.std_proportion]) for i in range(num_cols)] for j in range(num_rows)])
def createToyFuzzyDataSet(num_rows, num_cols): [[ FuzzySet(elements=np.random.uniform(0, 100, 2), mf=gaussmf, params=[ np.mean(np.random.uniform(0, 100, 2)), np.std(np.random.uniform(0, 100, 2)) ]) for i in range(num_rows) ] for j in range(num_cols)]
def cross_product_kernel( X: FuzzySet, Y: FuzzySet, kernel_on_elements: Callable, params_kernel_on_elements: List, kernel_on_membership_degrees: Callable, params_kernel_on_membership_degrees: List) -> np.ndarray: """ Calculates the Cross Product kernel between two fuzzy sets X and Y Input: X: (Type: FuzzySet) Y: (Type: FuzzySet) kernel_elements: (Type: Callable) params_kernel_elements: (Type: List) kernel_degrees: (Type: Callable) params_kernel_degrees: (Type: List) Output: (Type: numpy.ndarray) kernel result """ # create cross-product map x = X.get_pair() y = Y.get_pair() cross_product_map = list(itertools.product(*list([x, y]))) # iterate over the cross-product map x = [ kernel_on_elements( *input_validation(val[0][0], val[1][0], params_kernel_on_elements)) for val in cross_product_map ] y = [ kernel_on_membership_degrees(*input_validation( val[0][1], val[1][1], params_kernel_on_membership_degrees)) for val in cross_product_map ] x = np.asarray([float(i) for i in x]) y = np.asarray([float(i) for i in y]) return np.dot(x, y)
def nonsingleton_gaussian_kernel(X: FuzzySet, Y: FuzzySet, gamma: float) -> np.ndarray: """ Calculates the non-singleton Gaussian kernel between two fuzzy sets X and Y Input: X: (Type: FuzzySet) Y: (Type: FuzzySet) gamma: (Type: float) kernel parameter Output: (Type: numpy.ndarray) kernel result """ # get Gaussian parameters mean_x, sigma_x = X.get_membership_function_params() mean_y, sigma_y = Y.get_membership_function_params() return np.exp(-0.5 * gamma * (mean_x - mean_y)**2 / (sigma_x**2 + sigma_y**2))
def get_rule_antecedents(clf, fuzzy_data, gamma): rules = fuzzy_data.copy() rules = rules[clf.support_, :] # updatate sigmas of MFs of rule antecedents with gamma parameter num_rows = rules.shape[0] num_cols = rules.shape[1] return np.asarray([[FuzzySet(membership_function_params=[rules[j, i].get_membership_function_params()[0], rules[j, i].get_membership_function_params()[1] * np.sqrt(1/gamma)]) for i in range(num_cols)] for j in range(num_rows)])
def create_toy_fuzzy_dataset(num_rows=10, num_cols=2, parametric=False): ''' Creates a matrix of fuzzy datasets, each row represent a tuple of fuzzy sets each column is a variable. Each fuzzy set is a fuzzy set with gaussian membership function ''' # returns, elements, membership degrees based on gaussmf if not parametric: return np.asarray([[FuzzySet(elements=np.random.uniform(0, 100, 2), membership_function=gaussmf, membership_function_params=[np.mean(np.random.uniform(0, 100, 2)), np.std(np.random.uniform(0, 100, 2))]) for i in range(num_cols)] for j in range(num_rows)]) # returns fuzzy sets characterized by gaussian MF with two parameters, mean and std if parametric: return np.asarray([[FuzzySet(membership_function_params=[np.mean(np.random.uniform(0, 100, 2)), np.std(np.random.uniform(0, 100, 2))]) for i in range(num_cols)] for j in range(num_rows)])
def create_toy_fuzzy_dataset(num_rows=10, num_cols=2): ''' Creates a matrix of fuzzy datasets, each row represent a tuple of fuzzy sets each column is a variable. Each fuzzy set is a fuzzy set with gaussian membership function ''' return np.asarray([[ FuzzySet(elements=np.random.uniform(0, 100, 2), mf=gaussmf, params=[ np.mean(np.random.uniform(0, 100, 2)), np.std(np.random.uniform(0, 100, 2)) ]) for i in range(num_cols) ] for j in range(num_rows)])
def non_singleton_fuzzification_classification(self, constant_std=True, std_proportion=5): grouped = self._data.groupby([self._target]) if constant_std: self._std_values = grouped.transform(lambda x: std_proportion) else: self._std_values = grouped.transform(lambda x: np.random.normal(np.random.uniform(low=np.std(x)/10, high=np.std(x)/2, size=len(x)), (std_proportion / 100) * np.std(x), len(x))) num_rows = self._std_values.shape[0] num_cols = self._std_values.shape[1] self._fuzzydata = np.asarray([[FuzzySet(membership_function_params=[self._data.iloc[j, i], self._std_values.iloc[j, i]]) for i in range(num_cols)] for j in range(num_rows)])