Ejemplo n.º 1
0
    def predict(self, X: np.ndarray) -> np.ndarray:
        '''
			predict

			- params:
				- X : np.ndarray (S, N) : where S is the number of features,
											and N is the number of samples
			- return:
				- Y : np.ndarray (1, N) : output

		'''

        assert not np.any(self.params['centers'] == None),\
         "[Fuzzy C-Means][predict][ERROR] the model has not yet been trained!"

        u, u0, d, jm, p, fpc = cmeans_predict(
            test_data=X,
            cntr_trained=self.params['centers'],
            m=self.params['m'],
            error=self.params['tol'],
            maxiter=self.params['max_iter'],
            init=None)

        y = np.transpose(np.argmax(u, axis=0))

        self.results['u'] = u
        self.results['u0'] = u0
        self.results['d'] = d
        self.results['jm'] = jm
        self.results['p'] = p
        self.results['fpc'] = fpc

        return np.expand_dims(y, axis=1)
Ejemplo n.º 2
0
    def get_past_scores(self, test_fraud_prob, meta_features, num_anm=85):
        """
        This function gets the actual scores of the last 6 months using the saved cmeans cluster centers
        and saves them to csvs in outputs folder
        """

        print("GETTING ACTUAL PAST SCORES")
        u, _, _, _, _, _ = cmeans_predict(test_fraud_prob.T,
                                          self.cmeans_cntr,
                                          m=2,
                                          error=0.005,
                                          maxiter=1000)
        labels = u.T

        for i in range(len(meta_features)):
            window_num_patients = meta_features[i, :, 0]
            window_scores = labels[i * num_anm:(i + 1) * num_anm, 1]
            mask = []
            for j in range(num_anm):
                if window_num_patients[j] > 0:
                    mask.append(True)
                else:
                    mask.append(False)
            anm_mask = np.array(self.allANMdf['sub_center_id'])[mask]
            window_scores_mask = window_scores[mask]

            scores_df = pd.DataFrame({
                'sub_center_id': anm_mask,
                'scores': window_scores_mask
            })
            scores_df.to_csv('outputs/past_scores' + str(i) + '.csv')
Ejemplo n.º 3
0
 def update_rules(self, x: torch.Tensor, center):
     """
     todo: update rule object according to the given cluster center list
     :param x: the data where rules are generated
     :param center: the given cluster center list
     :return: None
     """
     self.center_list = center
     self.n_rules = center.shape[0]
     data_partition, _, _, _, _, _ = \
         cmeans_predict(x.t(), center, 2, error=0.005, maxiter=1000)
     self.data_partition = torch.tensor(data_partition).t()
     self.widths_list = self.get_widths_list(x)
Ejemplo n.º 4
0
    for i in range(10):
        c = fc[i]
        ax[i].matshow(c.reshape(8, 8) * 255.0, cmap='gray')
        ax[i].set_xticks([])
        ax[i].set_yticks([])

    plt.show()

    # Membership degrees of a sample representing the digit '7'
    print('Membership degrees: {}'.format(W[:, 7]))

    fig, ax = plt.subplots(figsize=(15, 8))

    ax.plot(np.arange(10), W[:, 7])
    ax.set_xlabel('Cluster index')
    ax.set_ylabel('Fuzzy membership')
    ax.grid()

    plt.show()

    # Perform a prediction
    new_sample = np.expand_dims(X_train[7], axis=1)
    Wn, _, _, _, _, _ = cmeans_predict(new_sample,
                                       cntr_trained=fc,
                                       m=1.25,
                                       error=1e-6,
                                       maxiter=10000,
                                       seed=1000)

    print('Membership degrees: {}'.format(Wn.T))
Ejemplo n.º 5
0
X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.4, random_state=0)

print(X_train.shape)
print(X_test.shape)
print(y_train)
print(y_test)

n_samples, n_features = data.shape
n_digits = len(np.unique(labels))
print("n_digits: %d, \t n_samples %d, \t n_features %d"
      % (n_digits, n_samples, n_features))

cntr, u, u0, d, jm, p, fpc = cmeans(data, 2, 2, error=0.005, maxiter=1000, init=None, seed=None)

print(cntr.shape)
# Predict
u,u0,d,jm,p,fpc = cmeans_predict(data, cntr, 2, error=0.005, maxiter=1000, init=None, seed=None)

# print('------ actual ----------')
# print(y_train.shape)
# print('------ predict ----------')
# print(u.shape)

# outputline = ','+ str(accuracy_score(y_train,u))+','+str(precision_score(y_train,u))+','+str(recall_score(y_train,u))+','+str(f1_score(y_train,u))

# f=open("out.csv", "a+")
# f.write(outputline)
# f.close()


Ejemplo n.º 6
0
 def predict(self, X):
     u, _, dists, _, _, fpc = cmeans_predict(X.T, self.centroids, self.exp,
                                             self.error, self.max_iter)
     return u.T
Ejemplo n.º 7
0
def perform_fuzzy_clustering(training: np.array, test: np.array,
                             clusters: int, m: int) -> tuple:
    center, train_labels = cmeans(training.T, clusters, m, 0.005, 1000)[0:2]
    test_labels = cmeans_predict(test.T, center, m, 0.005, 1000)[0]
    return *(np.argmax(label, 0) for label in [train_labels, test_labels]),