def main(): np.random.seed(0) random.seed(0) # Load dataset data = load_iris() # Organize our data X = data["data"] y = data["target"] # y.shape is (150,) y = create_one_hot_from_array(y) # y.shape is now (150,3) # Split our data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) # Declare the fitness function we want to use def fit(y_true, y_pred): # y_pred are floats in [0, n_classes-1]. To use accuracy metric we need # to binarize the output using round_to_cls() # Warning /!\ here since it has been one-hot-encoded we need to set # n_classes=2 instead n_classes=N_CLASSES because each consequent # is a binary class y_pred_bin = round_to_cls(y_pred, n_classes=2) return accuracy_score(y_true, y_pred_bin) # Initialize our classifier clf = TrefleClassifier( n_rules=3, # here we need to increase the number of rule to 3 # # because we need at least 1 rule per class in the case # # of a one-hot-encoded problem n_classes_per_cons=[2, 2, 2], # there are 3 consequents with 2 classes # # each. n_labels_per_mf=4, # use 4 labels LOW, MEDIUM, HIGH, VERY HIGH default_cons=[0, 0, 1], # default rule yield the class 2 n_max_vars_per_rule=4, # let's use the 4 iris variables (PL, PW, SL, SW) n_generations=30, fitness_function=fit, verbose=True, ) # Train our classifier clf.fit(X_train, y_train) # Make predictions # y_pred = clf.predict_classes(X_test) y_pred_raw = clf.predict(X_test) y_pred = round_to_cls(y_pred_raw, n_classes=2) clf.print_best_fuzzy_system() # Evaluate accuracy # Important /!\ the fitness can be different than the scoring function score = accuracy_score(y_test, y_pred) print("Score on test set: {:.3f}".format(score))
def run(): import numpy as np import random np.random.seed(6) random.seed(6) # Load dataset data = load_breast_cancer() # data = load_iris() # Organize our data y_names = data["target_names"] print("target names", y_names) y = data["target"] y = y.reshape(-1, 1) X_names = data["feature_names"] print("features names", X_names) X = data["data"] # X, y = make_classification( # n_samples=1000, n_features=10, n_informative=5, n_classes=2 # ) # y = y.reshape(-1, 1) # multi_class_y_col = np.random.randint(0, 4, size=y.shape) # regr_y_col = np.random.random(size=y.shape) * 100 + 20 # y = np.hstack((y, multi_class_y_col, regr_y_col)) # print(y) # Split our data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) def fit(y_true, y_pred): y_pred_thresholded = round_to_cls(y_pred, n_classes=2) fitness_val = accuracy_score(y_true, y_pred_thresholded) return fitness_val # Initialize our classifier clf = TrefleClassifier( n_rules=3, n_classes_per_cons=[2], default_cons=[1], n_max_vars_per_rule=3, n_generations=10, pop_size=100, n_labels_per_mf=3, verbose=True, dc_weight=1, # p_positions_per_lv=16, n_lv_per_ind_sp1=40, fitness_function=fit, ) # Train our classifier model = clf.fit(X_train, y_train) # Make predictions y_pred = clf.predict(X_test) clf.print_best_fuzzy_system() tff_str = clf.get_best_fuzzy_system_as_tff() print(tff_str) yolo = TrefleFIS.from_tff(tff_str) yolo.describe() # fis = clf.get_best_fuzzy_system() # print("best fis is ", end="") # print(fis) # FISViewer(fis).show() # Evaluate accuracy print("Simple run score: ") y_pred_thresholded = round_to_cls(y_pred, n_classes=2) print("acc", accuracy_score(y_test, y_pred_thresholded)) print(classification_report(y_test, y_pred_thresholded))