Exemplo n.º 1
0
def main():
    """ Fonction main """

    # Creating the model
    perceptron = Perceptron(max_iter=500, n_jobs=-1)

    # Perceptron du linear model sur les différents types de données
    fig_lm, ax_lm = plt.subplots(ncols=3, figsize=(10, 6))
    for dtype in range(3):
        linear_model(perceptron, data_type=dtype, epsilon=0.3, ax=ax_lm[dtype])

    # plt.savefig("linear_model_perceptron.png")

    # Application du perceptron de linear model sur les données usps
    comparaison_usps(perceptron)

    # Application des différents kernels sur l'ensemble des types de données
    kernels = ['linear', 'poly', 'rbf', 'sigmoid']
    fig_svm, ax_svm = plt.subplots(ncols=4,
                                   nrows=3,
                                   sharex=True,
                                   figsize=(25, 15))
    for dtype in range(3):
        for j, kernel in enumerate(kernels):
            SVM(data_type=dtype,
                epsilon=0,
                C=1,
                kernel=kernel,
                probability=False,
                max_iter=-1,
                ax=ax_svm[dtype][j])

    fig_svm.tight_layout()
    # plt.savefig("svm_data_kernel_rapport.png")
    plt.show()

    # ------------------------------- Grid Search

    # Données 2D artificiels
    trainx, trainy = gen_arti(nbex=1000, data_type=0, epsilon=0.3)
    testx, testy = gen_arti(nbex=1000, data_type=0, epsilon=0.3)
    # svm_gridSearch(trainx, trainy, testx, testy)

    # Données Usps
    trainux, trainuy = load_usps("USPS_train.txt")
    testux, testuy = load_usps("USPS_test.txt")
    # svm_gridSearch(trainux, trainuy, testux, testuy)

    # ------------------------------- Multi Class

    print(np.unique(testuy))
    multiClass(trainux, trainuy, testux, testuy)
Exemplo n.º 2
0
def SVM(data_type=0,
        epsilon=0.3,
        C=10,
        kernel='linear',
        probability=True,
        max_iter=100,
        ax=plt):
    """ Plot ls différents kernels appliqués sur les différents types de données

    :param data_type: Différents types de données: 0, 1, 2
    :param epsilon: Bruit dans les données
    :param C: pénalité
    :param kernel: kernel utilisé
    :param probability: Booléen permettant l'utilisation de la frontière ou nn.
    :param max_iter: Maximum d'itérations
    :param ax: axe sur lequel affiché le graphique.

    """

    trainx, trainy = gen_arti(nbex=1000, data_type=data_type, epsilon=epsilon)
    testx, testy = gen_arti(nbex=1000, data_type=data_type, epsilon=epsilon)

    s = svm.SVC(C=C, kernel=kernel, probability=probability, max_iter=max_iter)

    s.fit(trainx, trainy)

    err_train = round(1 - s.score(trainx, trainy), 3)
    err_test = round(1 - s.score(testx, testy), 3)

    print("Erreur : train %f, test %f\n" % (err_train, err_test))

    ax.set_title("SVM {} kernel \non data_type {}".format(kernel, data_type))

    if probability:

        def f(x):
            return s.predict_proba(x)[:, 0]
    else:

        def f(x):
            return s.decision_function(x)

    plotAll_in_subplots(testx, testy, f, ax)

    ax.legend(["err_train: {}\nerr_test: {}".format(err_train, err_test)],
              loc='lower left',
              bbox_to_anchor=(0, 1.02, 1, 0.2),
              ncol=1,
              fancybox=True,
              shadow=True)
Exemplo n.º 3
0
def linear_model(perceptron, data_type=0, epsilon=0.3, ax=plt):
    """ Utilise le perceptron passé en paramètre en l'occurence celui de
        sklearn.
    :param perceptron: Perceptron initialisé, soit pour les données
                       artificiels, soit, pour les données USPS.
    :param data_type: Différents types de données: 0, 1, 2
    :param epsilon: Bruit dans les données
    :param ax: axe sur lequel affiché le graphique.

    """

    trainx, trainy = gen_arti(nbex=1000, data_type=data_type, epsilon=epsilon)
    testx, testy = gen_arti(nbex=1000, data_type=data_type, epsilon=epsilon)

    # Learning
    perceptron.fit(trainx, trainy)

    # erreurs
    err_train = round(1 - perceptron.score(trainx, trainy), 3)
    err_test = round(1 - perceptron.score(testx, testy), 3)

    plotAll_in_subplots(testx, testy, perceptron.predict, ax)

    ax.set_title("linear_model_perceptron \non data_type {}".format(data_type))
Exemplo n.º 4
0
def main():

    
    # Création des données selon deux distributions gaussiennes
    plt.ion()
    trainx,trainy = gen_arti(nbex=1000, data_type=0, epsilon=1)
    testx,testy = gen_arti(nbex=1000, data_type=0, epsilon=1)
    # Base pour projection en distance gaussienne :
    base = trainx[np.random.randint(trainx.shape[0], size=100),:]
    
    # Tracé de l'isocontour de l'erreur pour MSE et HINGE
    plot_error(trainx,trainy,mse)
    plot_error(trainx,trainy,hinge)
    
    # Modèle standard avec MSE
    perceptron = Lineaire(mse,mse_g,max_iter=1000,eps=0.01)
    perceptron.fit(trainx,trainy)
    print("\nErreur mse : train %f, test %f"% (perceptron.score(trainx,trainy),perceptron.score(testx,testy)))
    plt.figure()
    plot_frontiere(trainx,perceptron.predict,200)
    plot_data(trainx,trainy)
    plot_trajectory(trainx,trainy,perceptron)
    plt.show()
    
    # Modèle standard Hinge
    perceptron = Lineaire(hinge,hinge_g,max_iter=1000,eps=0.1)
    perceptron.fit(trainx,trainy)
    print("\nErreur hinge : train %f, test %f"% (perceptron.score(trainx,trainy),perceptron.score(testx,testy)))
    plt.figure()
    plot_frontiere(trainx,perceptron.predict,200)
    plot_data(trainx,trainy)
    plot_trajectory(trainx,trainy,perceptron)
    plt.show()
    
    
    # Modèle MSE avec biais
    perceptron = Lineaire(mse,mse_g,max_iter=1000,eps=0.01, bias=True)
    perceptron.fit(trainx,trainy)
    print("\nErreur mse biais : train %f, test %f"\
          % (perceptron.score(trainx,trainy),perceptron.score(testx,testy)))
    plt.figure()
    plot_frontiere(trainx,perceptron.predict,200)
    plot_data(trainx,trainy)
    plot_trajectory(trainx,trainy,perceptron)
    plt.show()

    # Modèle hinge avec biais
    perceptron = Lineaire(hinge, hinge_g, max_iter=1000, eps=0.1, bias=True)
    perceptron.bias = True
    perceptron.fit(trainx,trainy)
    print("\nErreur hinge biais : train %f, test %f"\
          % (perceptron.score(trainx,trainy),perceptron.score(testx,testy)))
    plt.figure()
    plot_frontiere(trainx,perceptron.predict,200)
    plot_data(trainx,trainy)
    plot_trajectory(trainx,trainy,perceptron)
    plt.show()
    

    # Attention : l'affichage de la frontières avec projections sont très longues à générer
    """
    # Modèle hinge avec projection gaussienne
    perceptron = Lineaire(hinge, hinge_g, max_iter=1000, eps=0.5, bias=False, project="gauss")
    perceptron.base = base
    perceptron.fit(trainx,trainy)
    print("\nErreur hinge gauss : train %f, test %f"\
          %(perceptron.score(trainx,trainy),perceptron.score(testx,testy)))
    plt.figure()
    plot_frontiere(trainx,perceptron.predict,200)
    plot_data(trainx,trainy)
    plot_trajectory(trainx,trainy,perceptron)
    plt.show()

    # Modèle hinge avec projection polynomiale
    perceptron = Lineaire(hinge, hinge_g, max_iter=100, eps=0.1, bias=False, project="polynomial")
    perceptron.fit(trainx,trainy)
    print("\nErreur hinge polynomial : train %f, test %f"\
          %(perceptron.score(trainx,trainy),perceptron.score(testx,testy)))
    plt.figure()
    plot_frontiere(trainx,perceptron.predict,200)
    plot_data(trainx,trainy)
    plot_trajectory(trainx,trainy,perceptron)
    plt.show()
    """
    
    # Données USPS
    datax_train, datay_train = load_usps("USPS_test.txt")
    datax_test, datay_test = load_usps("USPS_train.txt")
    
    #6 vs 9    
    trainx = datax_train[np.where(np.logical_or(datay_train == 6,datay_train == 9))]
    trainy = datay_train[np.where(np.logical_or(datay_train == 6,datay_train == 9))]
    labely_train = np.sign(trainy - 7)
    testx = datax_test[np.where(np.logical_or(datay_test == 6,datay_test == 9))]
    testy = datay_test[np.where(np.logical_or(datay_test == 6,datay_test == 9))]
    labely_test = np.sign(testy - 7)

    perceptron = Lineaire(hinge, hinge_g, max_iter=1000, eps=0.1)
    perceptron.fit(trainx, labely_train)
    print("Erreur 2 classes 6/9: train %f, test %f"% (perceptron.score(trainx,labely_train),\
                                                      perceptron.score(testx,labely_test)))

    plot_vector(perceptron.w.reshape(16,16))
    plot_learning_curve(trainx, labely_train, testx, labely_test, start=0, stop=501, step=10)
        
    #6 vs all
    labely_train = 2 * (datay_train == 6) - 1
    labely_test = 2 * (datay_test == 6) - 1
    
    perceptron = Lineaire(hinge, hinge_g, max_iter=1000, eps=0.1)
    perceptron.fit(datax_train, labely_train)
    print("Erreur one vs all: train %f, test %f"% (perceptron.score(datax_train,labely_train),\
                                                   perceptron.score(datax_test,labely_test)))
    
    # Attention : la courbe d'apprentissage est très longue à générer
    """
from scipy.stats import uniform
import scipy


def load_usps(fn):
    with open(fn, "r") as f:
        f.readline()
        data = [[float(x) for x in l.split()] for l in f if len(l.split()) > 2]
    tmp = np.array(data)
    return tmp[:, 1:], tmp[:, 0].astype(int)


datax, datay = load_usps("USPS/USPS_train.txt")
datatestx, datatesty = load_usps("USPS/USPS_test.txt")

trainx, trainy = at.gen_arti(nbex=1000, data_type=0, epsilon=1)
testx, testy = at.gen_arti(nbex=1000, data_type=0, epsilon=1)

###############################################################################
# TEST AVEC SKLEARN
###############################################################################

# Perceptron
#num1=1
#num2=8

#X,Y=tme3.genere_Data(datax,datay,num1,num2)
#testX,testY=tme3.genere_Data(datatestx,datatesty,num1,num2)
#perceptron = Perceptron(tol=1e-3, random_state=0)
#perceptron = tme3.Lineaire(tme3.hinge,tme3.hinge_g,max_iter=1000,eps=0.1,biais=False)
#perceptron.fit(X,Y)
Exemplo n.º 6
0
def main():
    """ Tracer des isocourbes de l'erreur """

    # plt.ion
    trainx, trainy = gen_arti(nbex=1000, data_type=1, epsilon=0.3)
    testx, testy = gen_arti(nbex=1000, data_type=1, epsilon=0.3)
    grid, x1list, x2list = make_grid(xmin=-4, xmax=4, ymin=-4, ymax=4)

    # Plot error for test
    # plot_cost_erreur(testx, testy)

    # Batch gradient descent
    perceptron = Perceptron(loss=hinge,
                            loss_g=hinge_g,
                            max_iter=1000,
                            eps=0.1,
                            kernel=None)
    learn_plot_perceptron2D(perceptron,
                            trainx,
                            trainy,
                            testx,
                            testy,
                            gradient_descent="batch",
                            title="batch gradient descent")

    perceptron_poly = Perceptron(loss=hinge,
                                 loss_g=hinge_g,
                                 max_iter=100,
                                 eps=0.1,
                                 kernel="polynomial")
    learn_plot_perceptron2D(perceptron_poly,
                            trainx,
                            trainy,
                            testx,
                            testy,
                            gradient_descent="batch",
                            title="Batch gradient descent with polynomial "
                            "kernel")

    perceptron_gaussian = Perceptron(loss=hinge,
                                     loss_g=hinge_g,
                                     max_iter=100,
                                     eps=0.1,
                                     kernel="gaussian")
    learn_plot_perceptron2D(perceptron_gaussian,
                            trainx,
                            trainy,
                            testx,
                            testy,
                            gradient_descent="batch",
                            title="Batch gradient descent with gaussian "
                            "kernel")

    # Stochastic gradient descent
    perceptron_stochastic = Perceptron(loss=stochastic,
                                       loss_g=stochastic_g,
                                       max_iter=10,
                                       eps=0.1,
                                       kernel=None)
    learn_plot_perceptron2D(perceptron_stochastic,
                            trainx,
                            trainy,
                            testx,
                            testy,
                            gradient_descent="stochastic",
                            title="Stochastic gradient descent")

    # Mini-Batch gradient descent
    perceptron_minibash = Perceptron(loss=hinge,
                                     loss_g=hinge_g,
                                     max_iter=100,
                                     eps=0.1,
                                     kernel=None)
    learn_plot_perceptron2D(perceptron_minibash,
                            trainx,
                            trainy,
                            testx,
                            testy,
                            gradient_descent="minibatch",
                            title="Mini-Batch gradient descent")

    # Stochastic gradient descent Animation
    # perceptron_stoch_anim = Perceptron(loss=stochastic, loss_g=stochastic_g,
    #                                    max_iter=1, eps=0.1, kernel=None)
    # learn_plot_perceptron2D(perceptron_stoch_anim, trainx, trainy, testx,
    #                         testy, gradient_descent="stochastic_animation",
    #                         title="Stochastic gradient descent")

    ##########################################################################
    # ------------------------------------ USPS ---------------------------- #
    ##########################################################################

    perceptron_usps = Perceptron(loss=hinge,
                                 loss_g=hinge_g,
                                 max_iter=500,
                                 eps=0.1,
                                 kernel=None)

    # Matrice de poids 6 vs 9 and 1 vs 8
    fig, (ax1, ax2) = plt.subplots(ncols=2, sharex=True, sharey=True)
    plt.suptitle("Matrice de poids")
    weight_matrix(6, 9, fig, perceptron_usps, ax1)
    weight_matrix(1, 8, fig, perceptron_usps, ax2)
    # plt.savefig("weight_matrix_qqlexs")

    # Matrice de poids 6 vs All
    matrix_one_vs_all(6, perceptron_usps)

    # Courbes d'erreurs 6 vs All
    error_curves(6, "sklearn_perceptron")
Exemplo n.º 7
0
    #     ntestx = projection_polynomiale(testx)
    #
    #     perceptron = Lineaire(hinge, hinge_g, max_iter = 1000, eps = 0.05, bias = False)
    #     perceptron.fit(ntrainx,trainy)
    #
    #     print("Erreur : train %f, test %f"% (perceptron.score(ntrainx,trainy),perceptron.score(ntestx,testy)))
    #     plt.figure()
    #     plot_frontiere(trainx, lambda x: perceptron.predict(projection_polynomiale(x)), 200)
    #     plot_data(trainx,trainy)
    # =============================================================================

    #projection gaussienne

    plt.close("all")

    trainx, trainy = gen_arti(nbex=4000, data_type=2)
    testx, testy = gen_arti(nbex=4000, data_type=2)

    x1min = trainx[:, 0].min()
    x2min = trainx[:, 1].min()
    x1max = trainx[:, 0].max()
    x2max = trainx[:, 1].max()
    n1 = 40
    n2 = 40
    ntrainx = projection_gaussienne(trainx, x1min, x1max, x2min, x2max, n1, n2)
    ntestx = projection_gaussienne(testx, x1min, x1max, x2min, x2max, n1, n2)

    perceptron = Lineaire(hinge, hinge_g, max_iter=1000, eps=0.05)
    perceptron.fit(ntrainx, trainy)

    print("Erreur : train %f, test %f" %
Exemplo n.º 8
0
def main():

    # Affichage de f et df en fonction du nombre d'itérations
    plot_nb_iter(f1, df1, 5, 0.1, 30)
    plot_nb_iter(f2, df2, 5, 0.1, 30)
    plot_nb_iter(f3, df3, np.array([-1, -1]).reshape(1, 2), 0.001, 20)

    # Affichage 2D de f1, f2 et 3D de f3
    plot_2d(f1, df1, 5, 0.1, 30)
    plot_2d(f2, df2, 5, 0.1, 30)
    plot_3d(f3, df3, np.array([-1, -1]).reshape(1, 2), 0.001, 20)

    # Affichage des distances à l'optimum de l'historique
    plot_distance(f1, df1, 5, 0.1, 30)
    plot_distance(f2, df2, 5, 0.1, 30)
    plot_distance(f3, df3, np.array([-1, -1]).reshape(1, 2), 0.001, 50)

    # Regression logistique avec données USPS
    datax_train, datay_train = load_usps("USPS_test.txt")
    datax_test, datay_test = load_usps("USPS_train.txt")

    ## On test la reconnaissance 6 vs 9 (on isole puis transforme les données en -1 et 1)
    datax_train_2 = datax_train[np.where(
        np.logical_or(datay_train == 1, datay_train == 8))]
    datay_train_2 = datay_train[np.where(
        np.logical_or(datay_train == 1, datay_train == 8))]
    labely_train = np.sign(datay_train_2 - 2)
    datax_test_2 = datax_test[np.where(
        np.logical_or(datay_test == 1, datay_test == 8))]
    datay_test_2 = datay_test[np.where(
        np.logical_or(datay_test == 1, datay_test == 8))]
    labely_test = np.sign(datay_test_2 - 2)

    model = Learner(max_iter=1000, eps=0.05)
    model.fit(datax_train_2, labely_train)
    print("Erreur de classification 6/9: train %f, test %f"\
          % (model.score(datax_train_2,labely_train),model.score(datax_test_2,labely_test)))

    ## Affichage des poids
    weights = model.w
    plot_vector(weights.reshape(16, 16))

    ## Classification 1 versus toutes les autres
    model2 = Learner(max_iter=1000, eps=0.05)
    labely_train = 2 * (datay_train == 6) - 1
    labely_test = 2 * (datay_test == 6) - 1

    model2.fit(datax_train, labely_train)
    print("Erreur one vs all: train %f, test %f"\
          % (model2.score(datax_train,labely_train),model2.score(datax_test,labely_test)))

    ## Essai sur gen_arti pour tester les performances
    trainx, trainy = gen_arti(nbex=1000, data_type=0, epsilon=1)
    testx, testy = gen_arti(nbex=1000, data_type=0, epsilon=1)
    model1 = Learner(max_iter=100, eps=0.01)
    model1.fit(trainx, trainy)
    print("Erreur de classification gen_arti: train %f, test %f"\
          % (model1.score(trainx,trainy),model1.score(testx,testy)))
    plt.figure()
    plot_frontiere(trainx, model1.predict, 200)
    plot_data(trainx, trainy)