Example #1
0
class TestFCM(unittest.TestCase):
    def setUp(self):
        self.df = pd.read_csv("iris-setosa.csv")
        self.X = self.df.iloc[:, 1:5].values
        self.y = self.df.iloc[:, 0].values
        self.model = FCM()
        self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
            self.X, self.y)

    def test_isScoreValid(
            self):  # Controlla che l'accuratezza sia un valore valido
        n_clusters = 2
        train_membership, centers = self.model.fuzzy_train(
            self.X_train, n_clusters, 2)
        test_membership = self.model.fuzzy_predict(self.X_test, n_clusters,
                                                   centers, 2)
        #warnings.filterwarnings("ignore")
        acc = self.model.accuracy(test_membership, self.y_test)
        self.assertGreaterEqual(acc, 0)
        self.assertLessEqual(acc, 1)

    def test_errorThreshold(
        self
    ):  # Controlla che l'errore RMSE delle membership sia minore di una certa soglia
        threshold = 0.4
        n_clusters = 2
        train_membership, centers = self.model.fuzzy_train(
            self.X_train, n_clusters, 2)
        test_membership = self.model.fuzzy_predict(self.X_test, n_clusters,
                                                   centers, 2)
        #warnings.filterwarnings("ignore")
        err = self.model.RMSE_membership(test_membership, self.y_test)
        self.assertLessEqual(err, threshold)
Example #2
0
def fcm_script(data):

    #Da utilizzare se so a priori i nomi delle colonne
    #feat1=sys.argv[2]
    #feat2=sys.argv[3]
    #labels=sys.argv[4]

    dataset = pd.read_csv(data)

    # extract features and labels
    #X = dataset[[feat1, feat2]].values
    #y = dataset[labels].values

    X = dataset.iloc[:, 1:]
    y = dataset.iloc[:, 0]
    X = np.asarray(X)
    y = np.asarray(y)

    model = FCM()

    N_SPLITS = 5
    N_CLUSTER = 2
    error = []
    score = []

    #cross validation
    skf = StratifiedKFold(n_splits=N_SPLITS, shuffle=True, random_state=1)
    for train_index, test_index in skf.split(X, y):
        #print("TRAIN:", train_index, "\nTEST:", test_index)
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]

        #training
        train_membership, centers = model.fuzzy_train(X_train, N_CLUSTER, 2)

        #test
        test_membership = model.fuzzy_predict(X_test, N_CLUSTER, centers, 2)

        if (N_CLUSTER == 2):
            error.append(model.RMSE_membership(test_membership, y_test))

        else:
            error.append(model.RMSE(test_membership, y_test))

        score.append(model.accuracy(test_membership, y_test))

    return model, score, error