Ejemplo n.º 1
0
    def test_grafic_sensibility(self):
        INSTANCE: int = 74
        x, y = make_moons(n_samples=1500, noise=.4, random_state=17)
        clf = MLPClassifier()
        x_train, x_test, y_train, y_test = train_test_split(x,
                                                            y,
                                                            train_size=.8,
                                                            test_size=.2,
                                                            random_state=17)
        clf.fit(x_train, y_train)

        gpx = Gpx(clf.predict_proba,
                  x_train=x,
                  y_train=y,
                  random_state=42,
                  feature_names=['x', 'y'])
        gpx.explaining(x_test[INSTANCE, :])

        x, y = gpx.x_around[:, 0], gpx.x_around[:, 1]
        y_proba = gpx.proba_transform(gpx.y_around)

        resolution = 0.02
        x1_min, x1_max = x.min() - 1, x.max() + 1
        x2_min, x2_max = y.min() - 1, y.max() + 1
        xm1, xm2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                               np.arange(x2_min, x2_max, resolution))
        Z_bb = gpx.gp_prediction(np.array([xm1.ravel(), xm2.ravel()]).T)

        fig, ax = plt.subplots()
        ax.set_xlim(x1_min, x1_max)
        ax.set_xlim(x2_min, x2_max)
        scat = plt.scatter(x, y, y_proba)

        def func(data):
            k, j = data
            scat.set_offsets(k)
            scat.set_array(j)

        mmm = gpx.max_min_matrix(noise_range=10)

        gen = []
        for n in mmm[:, 0]:
            aux = gpx.x_around.copy()
            aux[:, 0] = n
            gen.append((aux.copy(), gpx.gp_prediction(aux.copy())))

        animation = ani.FuncAnimation(fig,
                                      func,
                                      gen,
                                      interval=200,
                                      save_count=200)

        plt.contourf(xm1, xm2, Z_bb.reshape(xm1.shape), alpha=0.4)
        plt.scatter(x, y, c=y_proba)

        plt.show()

        writergif = ani.PillowWriter(fps=5)
        animation.save('sens_x_2.gif', writer=writergif)

        sens_gpx = gpx.feature_sensitivity()
        print(sens_gpx)
Ejemplo n.º 2
0
    def test_multi_class(self):

        INSTANCE: int = 20
        iris = load_iris()
        X, y = load_iris(return_X_y=True)

        X_train, X_test, y_train, y_test = train_test_split(X,
                                                            y,
                                                            test_size=.3,
                                                            random_state=42)

        scaler = Normalizer()
        scaler.fit(X_train)
        X_train = scaler.transform(X_train)

        clf = RandomForestClassifier(random_state=42)
        clf.fit(X_train, y_train)

        gp_hyper_parameters = {
            'population_size':
            100,
            'generations':
            100,
            'stopping_criteria':
            0.0001,
            'p_crossover':
            0.7,
            'p_subtree_mutation':
            0.1,
            'p_hoist_mutation':
            0.05,
            'p_point_mutation':
            0.1,
            'const_range': (-1, 1),
            'parsimony_coefficient':
            0.0005,
            'init_depth': (3, 6),
            'n_jobs':
            -1,
            'random_state':
            42,
            'function_set': ('add', 'sub', 'mul', 'div', 'sqrt', 'log', 'abs',
                             'neg', 'inv', 'max', 'min', 'sin', 'cos', 'tan')
        }

        gpx = Gpx(clf.predict_proba,
                  gp_hyper_parameters=gp_hyper_parameters,
                  x_train=X_train,
                  y_train=y_train,
                  feature_names=iris.feature_names,
                  num_samples=500,
                  k_neighbor=50,
                  random_state=42)

        y_hat = gpx.explaining(
            scaler.transform(X_test[INSTANCE, :].reshape(-1, 1)))

        x_around = gpx.x_around

        gpx_y = gpx.gp_prediction(x_around)
        bb_y = clf.predict(x_around)

        gpx.logger.info('Multiclass gpx_y:{} / bb_y {}'.format(gpx_y, bb_y))
        gpx.logger.info('test_understand mult-class accuracy {}'.format(
            accuracy_score(gpx_y, bb_y)))