def ex_2_2(input1, target1, input2, target2):
    ## TODO
    scores = []
    scores_train = []
    classifiers = []
    for i in range(10):
        classifier = MLPClassifier(hidden_layer_sizes=(20, ),
                                   solver="adam",
                                   max_iter=1000,
                                   activation="tanh",
                                   random_state=i)
        classifier.fit(input1, target1[:, 0])
        scores.append(classifier.score(input2, target2[:, 0]))
        classifiers.append(classifier)
        scores_train.append(classifier.score(input1, target1[:, 0]))

    conf_mat = confusion_matrix(target2[:, 0],
                                classifiers[np.argmax(scores)].predict(input2))

    plot_histogram_of_acc(scores_train, scores)
    #plot_histogram_of_acc(classifiers[np.argmax(scores)], classifier.score(input2, target2[:, 0]))
    #plot_histogram_of_acc(classifier.score(input1, target1[:,0]), classifier.score(input2, target2[:,0]))
    predected_target = classifier.predict(input2)
    misclassified_images = []
    for i in range(len(target2[:, 0])):
        if target2[:, 0][i] != predected_target[i]:
            misclassified_images.append(input2[i])

    for i in range(len(misclassified_images)):
        plot_image(misclassified_images[i])
    pass
 def setUp(self):
     super(MLPClassifierTest, self).setUp()
     mdl = MLPClassifier(
         activation='identity', hidden_layer_sizes=50,
         max_iter=500, alpha=1e-4, solver='sgd', tol=1e-4,
         random_state=1, learning_rate_init=.1)
     self._port_model(mdl)
Example #3
0
def ex_2_1(X_train, y_train, X_test, y_test):
    """
    Solution for exercise 2.1
    :param X_train: Train set
    :param y_train: Targets for the train set
    :param X_test: Test set
    :param y_test: Targets for the test set
    :return:
    """

    # >>> from sklearn.neural_network import MLPClassifier
    # >>> from sklearn.datasets import make_classification
    # >>> from sklearn.model_selection import train_test_split
    # >>> X, y = make_classification(n_samples=100, random_state=1)
    # >>> X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y,
    # ...                                                     random_state=1)
    # >>> clf = MLPClassifier(random_state=1, max_iter=300).fit(X_train, y_train)
    # >>> clf.predict_proba(X_test[:1])
    # array([[0.038..., 0.961...]])
    # >>> clf.predict(X_test[:5, :])
    # array([1, 0, 1, 0, 1])
    # >>> clf.score(X_test, y_test)
    # 0.8...

    ## TODO
    train_accuracy = [0, 0, 0, 0, 0]
    test_accuracy = [0, 0, 0, 0, 0]
    clf = [0, 0, 0, 0, 0]
    for seed in range(1, 6):
        clf[seed - 1] = MLPClassifier(hidden_layer_sizes=(10, ),
                                      activation='tanh',
                                      max_iter=50,
                                      random_state=(seed * 3))
        clf[seed - 1].fit(X_train, y_train)
        train_accuracy[seed - 1] = clf[seed - 1].score(X_train, y_train)
        test_accuracy[seed - 1] = clf[seed - 1].score(X_test, y_test)

        print(train_accuracy[seed - 1])
        print(test_accuracy[seed - 1])
    plot_boxplot(train_accuracy, test_accuracy)

    y_test_pred = clf[4].predict(X_test)
    confusion = confusion_matrix(y_test, y_test_pred)

    # plot misclass
    misclass_coun = 0
    for ix, y in enumerate(y_test):
        if (y != y_test_pred[ix]):
            plot_image(X_test[ix])
            misclass_coun += 1
        if misclass_coun == 3:
            break

    # TODO: plot missclassified images based on confusion matrix
    print(confusion)

    # TODO: plot weights between input and hidden
    plot_hidden_layer_weights(clf[4].coefs_[0])

    pass
Example #4
0
def ex_2_1(input2, target2):
    """
    Solution for exercise 2.1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """

    #declaring variables used for MLPClassifier
    hidden_layers = 6
    solver_mode = 'adam'
    activation_mode = 'tanh'
    max_iter = 200

    cf = MLPClassifier(hidden_layer_sizes=(hidden_layers, ),
                       solver=solver_mode,
                       activation=activation_mode,
                       max_iter=max_iter)

    #training the classifier
    cf.fit(input2, target2[:, 1])

    #calculate y_predicted and y_true for confusion matrix calculation

    #printing confusion matrix
    print(confusion_matrix(target2[:, 1], cf.predict(input2)))

    #plotting the hidden layer weights
    plot_hidden_layer_weights(cf.coefs_[0])

    pass
Example #5
0
def main():
    X = [[0.,1.,0.,1.,0.,0.], [1.,0.,0., 1., 1., 0.]]
    y = [0,15]
    clf = MLPClassifier(alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1)
    clf.partial_fit(X, y)
    res = clf.predict([[1., 1., 0., 1., 1., 1.], [0, 0, 1 ,1, 1.,0]])
    print("res",res)
def ex_2_2(input1, target1, input2, target2):
    individualTarget1 = target1[:, 0]
    individualTarget2 = target2[:, 0]

    classifiers = []
    training_scores = []
    test_scores = []
    for i in range(10):
        classifiers.append(MLPClassifier(activation='tanh', hidden_layer_sizes=20, max_iter=1000, random_state=i))

    for mlp in classifiers:
        mlp.fit(input1, individualTarget1)
        training_scores.append(mlp.score(input1, individualTarget1))
        test_scores.append(mlp.score(input2, individualTarget2))

    plot_histogram_of_acc(training_scores, test_scores)
    best_index = test_scores.index(max(test_scores))
    best_prediction = classifiers[best_index].predict(input2)

    cnf_matrix = confusion_matrix(individualTarget2, best_prediction)
    print(cnf_matrix)

    misclassified_images = []
    for i, pred in enumerate(best_prediction):
        if pred != individualTarget2[i]:
            misclassified_images.append(i)

    plot_images(input2, misclassified_images)
Example #7
0
def trainModel():
    sss = []
    train_list = [["comp.speech/train/s1.wav", 0], ["comp.speech/train/s2.wav", 1], ["comp.speech/train/s3.wav", 2],
                  ["comp.speech/train/s4.wav", 3], ["comp.speech/train/s5.wav", 4], ["comp.speech/train/s6.wav", 5],
                  ["comp.speech/train/s7.wav", 6], ["comp.speech/train/s8.wav", 7]]

    for wav_name in train_list:
        add_wav_to_db(wav_name[0], wav_name[1], sss)

    data = []
    ans = []
    i = 0
    for index in xrange(len(sss)):
        for v in sss[index]:
            data.append(v[0])
            ans.append(v[1])

    clfNeural = MLPClassifier()
    clfNeural.fit(data, ans)

    clfForest = DecisionTreeClassifier(max_depth=250)
    clfForest.fit(data, ans)

    joblib.dump(clfNeural, 'model.pkl')
    joblib.dump(clfForest, 'forest.pkl')
def ex_2_1(input2, target2):
    """
    Solution for exercise 2.1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """
    # parse target2 2nd column
    pose2 = []
    for target in target2:
        pose2.append(target[1])

    mlp = MLPClassifier(activation='tanh', hidden_layer_sizes=6)
    print("===========fit started===========")
    mlp.fit(input2, pose2)
    print("===========fit finished===========")
    print("classes_: ", mlp.classes_)
    print("n_layers_: ", mlp.n_layers_)
    plot_hidden_layer_weights(mlp.coefs_[0])

    print("===========predict started===========")
    prediction = mlp.predict(input2)
    print("===========predict finished===========")
    cnf_matrix = confusion_matrix(pose2, prediction)
    print(cnf_matrix)
    return
Example #9
0
def ex_2_2(input1, target1, input2, target2):
    list = []
    train_acc = np.zeros(10)
    test_acc = np.zeros(10)
    for i in range(10):
        nn = MLPClassifier(hidden_layer_sizes=(20,),activation='tanh', max_iter=1000, random_state=None)
        list.append(nn)
        nn.fit(input1, target1[:,0]) 
        train_acc[i] = nn.score(input1, target1[:,0])
        test_acc[i] = nn.score(input2,target2[:,0])
        i_best = np.where(test_acc == test_acc.min())[0][0]
    import pdb
    pdb.set_trace() 
    y_pred = list[i_best].predict(input2)
    C = confusion_matrix(target2[:,0], y_pred, labels=None, sample_weight=None)
    """
    Solution for exercise 2.2
    :param input1: The input from dataset1
    :param target1: The target from dataset1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """
    ## TODO
    return train_acc, test_acc, y_pred, C
def ex_2_1(input2, target2):
    """
    Solution for exercise 2.1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """
    ## TODO - done

    hidden_units = 6
    nn = MLPClassifier(activation=ACTIVATION,
                       solver='adam',
                       hidden_layer_sizes=(hidden_units, ),
                       max_iter=200)
    pose = target2[:, 1]
    nn.fit(input2, pose)

    # using index 0 because of just one hidden layer
    hidden_layer_weights = nn.coefs_[0]

    y_pred = nn.predict(input2)
    matrix = confusion_matrix(pose, y_pred)

    print("The Confusion Matrix we obtained: \n" + str(matrix))

    plot_hidden_layer_weights(hidden_layer_weights)
Example #11
0
def ex_2_1(input2, target2):
    '''
    • Write code to train a feed-forward neural network with 1 hidden layers containing 6 hidden units
      for pose recognition. Use dataset2 for training after normalization, ‘adam’ as the training solver and
      train for 200 iterations.
    • Calculate the confusion matrix
    • Plot the weights between each input neuron and the hidden neurons to visualize what the network
      has learnt in the first layer.
      inote Use scikit-learn’s confusion_matrix function to to calculate the confusion matrix. Documentation
      for this can be found here
      inote You can use the coefs_ attribute of the model to read the weights. It is a list of length nlayers − 1
      where the ith element in the list represents the weight matrix corresponding to layer i.
      inote Use the plot_hidden_layer_weights in nn_classification_plot.py to plot the hidden weights.
    '''

    # dataset2 = normalize(input2) already done by main
    x_train = input2
    y_train = target2[:, 1]
    # print(y_train)
    nn = MLPClassifier(solver='adam',
                       activation='tanh',
                       max_iter=200,
                       hidden_layer_sizes=(6, ))
    nn.fit(x_train, y_train)
    cm = confusion_matrix(y_train, nn.predict(x_train))
    plot_hidden_layer_weights(nn.coefs_[0])
    print(cm)
    pass
def get_pipeline():
    clf = MLPClassifier(hidden_layer_sizes=(512, 512),
                        batch_size=100,
                        max_iter=3,
                        solver='adam',
                        early_stopping=True)

    return Pipeline([('clf', clf)])
 def setUp(self):
     super(MLPClassifierJSTest, self).setUp()
     self.mdl = MLPClassifier(activation='relu',
                              hidden_layer_sizes=15,
                              max_iter=500, alpha=1e-4,
                              solver='sgd', tol=1e-4,
                              learning_rate_init=.1,
                              random_state=1,)
def ex_2_2(input1, target1, input2, target2):
    """
    Solution for exercise 2.2
    :param input1: The input from dataset1
    :param target1: The target from dataset1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """
    train = input1
    test = input2
    target_train = target1[:, 1]
    target_test = target2[:, 1]


    ## TODO
    n_hidden_neurons = 20

    accu_list_train = np.zeros((10,1))
    accu_list_test = np.zeros((10, 1))

# Find the best seed
    for seed in range(10):
        nn = MLPClassifier(activation='tanh', solver='adam', max_iter=1000, hidden_layer_sizes=(n_hidden_neurons,), random_state=seed)
        nn.fit(train, target_train)
        accu_list_train[seed] = nn.score(train, target_train)
        accu_list_test[seed] = nn.score(test, target_test)

    print(accu_list_train)
    print(accu_list_test)
# Compute NN weights with the best seed
    best_seed = np.argmax(accu_list_train)
    best_nn = nn = MLPClassifier(activation='tanh', solver='adam', max_iter=1000, hidden_layer_sizes=(n_hidden_neurons,),random_state=best_seed)
    best_nn.fit(train, target_train)

# Evaluate the confusion matrix with best NN
    predictions = nn.predict(test)
    C = confusion_matrix(target_test, predictions)
    print(C)

# Plot results
    plot_histogram_of_acc(accu_list_train, accu_list_test)
    print(accu_list_test)
# Find misclassified images
    comp_array = target_test - predictions
    comp_vector2 = np.nonzero(comp_array)
Example #15
0
 def setUp(self):
     super(MLPClassifierTest, self).setUp()
     self.porter = Porter(language='js')
     clf = MLPClassifier(
         activation='identity', hidden_layer_sizes=50,
         max_iter=500, alpha=1e-4, solver='sgd', tol=1e-4,
         random_state=1, learning_rate_init=.1)
     self.set_classifier(clf)
def ex_2_2(input1, target1, input2, target2):
    """
    Solution for exercise 2.2
    :param input1: The input from dataset1
    :param target1: The target from dataset1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """
    ## TODO
    hidden_units = 20

    test_face = target2[:, 0]
    train_face = target1[:, 0]

    test_accuracy = np.zeros(10)
    train_accuracy = np.zeros(10)

    best_network = 0
    max_accuracy = 0
    nn = MLPClassifier(activation=ACTIVATION,
                       solver="adam",
                       hidden_layer_sizes=(hidden_units, ),
                       max_iter=1000)

    for i in range(0, 10):
        nn.random_state = i

        nn.fit(input1, train_face)
        train_accuracy[i] = nn.score(input1, train_face)
        test_accuracy[i] = nn.score(input2, test_face)

        if test_accuracy[i] > max_accuracy:
            best_network = nn
            max_accuracy = test_accuracy[i]

    plot_histogram_of_acc(train_accuracy, test_accuracy)

    # Use the best network to calculate the confusion matrix for the test set.
    y_pred = best_network.predict(input2)
    matrix = confusion_matrix(test_face, y_pred)

    print("The Confusion Matrix we obtained: \n" + str(matrix))

    # Plot a few misclassified images.
    annas_favorit_number = 177
    marcos_favorit_numer = 490
    strugers_favorit_number_aka_best_mirp = 13
    manfreds_favorit_number_is_a_emirp_a_lucky_fortunate_sexy_and_happy_prime = 79
    best_numbers_ever = [
        annas_favorit_number, strugers_favorit_number_aka_best_mirp,
        marcos_favorit_numer,
        manfreds_favorit_number_is_a_emirp_a_lucky_fortunate_sexy_and_happy_prime
    ]

    for _ in best_numbers_ever:
        misclassified = np.where(test_face != best_network.predict(input2))
        plot_random_images(input2[misclassified])
def ex_2_1(input2, target2):
    ## TODO
    classifier = MLPClassifier(hidden_layer_sizes=(6, ),
                               solver="adam",
                               max_iter=200,
                               activation="tanh")

    classifier.fit(input2, target2[:, 1])
    con_mat = confusion_matrix(target2[:, 1], classifier.predict(input2))
    plot_hidden_layer_weights(classifier.coefs_[0])
def get_models():
    models = dict()
    models['NN'] = MLPClassifier(solver='lbfgs',
                                 alpha=1e-5,
                                 hidden_layer_sizes=(40, 10),
                                 random_state=1)
    models['sgdc'] = SGDClassifier(loss='hinge',
                                   penalty='l2',
                                   alpha=1e-3,
                                   n_iter=5,
                                   random_state=42)
    return models
 def test_hidden_activation_function_identity(self):
     mdl = MLPClassifier(
         activation='identity', hidden_layer_sizes=50,
         learning_rate_init=.1)
     self._port_model(mdl)
     Y, Y_py = [], []
     min_vals = np.amin(self.X, axis=0)
     max_vals = np.amax(self.X, axis=0)
     for n in range(30):
         x = [random.uniform(min_vals[f], max_vals[f])
              for f in range(self.n_features)]
         Y.append(self.make_pred_in_custom(x))
         Y_py.append(self.make_pred_in_py(x))
     self.assertListEqual(Y, Y_py)
Example #20
0
def ex_2_2(input1, target1, input2, target2):
    """
    Solution for exercise 2.2
    :param input1: The input from dataset1
    :param target1: The target from dataset1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """

    #declaring variables used for MLPClassifier
    hidden_layers = 20
    solver_mode = 'adam'
    activation_mode = 'tanh'
    max_iter = 1000

    max_accuracy = 0.0

    train_accuracy = []
    test_accuracy = []
    cfn = []

    m = 0

    for m in range(10):
        cf = MLPClassifier(hidden_layer_sizes=(hidden_layers, ),
                           activation=activation_mode,
                           solver=solver_mode,
                           random_state=m,
                           max_iter=max_iter)
        cf.fit(input1, target1[:, 0])

        train_accuracy.append(cf.score(input1, target1[:, 0]))

        current_test_accuracy = cf.score(input2, target2[:, 0])

        test_accuracy.append(current_test_accuracy)

        plot_histogram_of_acc(train_accuracy[m], test_accuracy[m])

        if current_test_accuracy > max_accuracy:
            cfn = confusion_matrix(target2[:, 0], cf.predict(input2))
            max_accuracy = current_test_accuracy

    print(cfn)

    #plot_histogram_of_acc(train_accuracy, test_accuracy)
    #plot_random_images(input2)

    pass
 def test_hidden_activation_function_identity(self):
     clf = MLPClassifier(
         activation='identity', hidden_layer_sizes=50,
         learning_rate_init=.1)
     self._port_model(clf)
     java_preds, py_preds = [], []
     min_vals = np.amin(self.X, axis=0)
     max_vals = np.amax(self.X, axis=0)
     for n in range(30):
         x = [random.uniform(min_vals[f], max_vals[f])
              for f in range(self.n_features)]
         java_preds.append(self.make_pred_in_java(x))
         py_preds.append(self.make_pred_in_py(x))
     self.assertListEqual(py_preds, java_preds)
Example #22
0
def get_pipeline_models():
    pipelineModels = dict()
    pipelineModels['NN2'] = Pipeline([("vectorizer", TfidfVectorizer()),
                                      ("classifier",
                                       MLPClassifier(solver='lbfgs',
                                                     alpha=1e-5,
                                                     hidden_layer_sizes=(40,
                                                                         10),
                                                     random_state=1))])
    # pipelineModels['nb'] = Pipeline([("vectorizer", TfidfVectorizer()), ("classifier", MultinomialNB())])
    # pipelineModels['svm'] = Pipeline([("vectorizer", TfidfVectorizer()), ("classifier", SGDClassifier(loss='hinge', penalty='l2', alpha=1e-3, n_iter=5, random_state=42))])
    # # pipelineModels['svc'] = Pipeline([("vectorizer", TfidfVectorizer()), ("classifier", SVC(kernel="linear"))])
    # pipelineModels['logreg'] = Pipeline([("vectorizer", TfidfVectorizer()), ("classifier", LogisticRegression(n_jobs=1, C=1e5))])
    # pipelineModels['nc'] = Pipeline([("vectorizer", TfidfVectorizer()), ("classifier", NearestCentroid())])
    return pipelineModels
Example #23
0
def ex_2_1(input2, target2):
    """
    Solution for exercise 2.1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """
    ## TODO
    pose = target2[:,1]
    nn = MLPClassifier(hidden_layer_sizes=(6,) ,activation='tanh', max_iter=200)
    nn.fit(input2, pose) 
    y_pred = nn.predict(input2)
    C = confusion_matrix(pose, y_pred, labels=None, sample_weight=None)
    plot_hidden_layer_weights(nn.coefs_[0])
    return C
 def test_activation_fn_relu_w_binary_data(self):
     self.mdl = MLPClassifier(activation='relu',
                              hidden_layer_sizes=15,
                              learning_rate_init=.1)
     self.load_binary_data()
     self._port_model()
     amin = np.amin(self.X, axis=0)
     amax = np.amax(self.X, axis=0)
     preds, ground_truth = [], []
     for _ in range(self.N_RANDOM_FEATURE_SETS):
         x = np.random.uniform(amin, amax, self.n_features)
         preds.append(self.pred_in_custom(x))
         ground_truth.append(self.pred_in_py(x))
     self._clear_model()
     # noinspection PyUnresolvedReferences
     self.assertListEqual(preds, ground_truth)
 def test_activation_fn_identity_w_multi_layers_w_image_data(self):
     self.estimator = MLPClassifier(activation='identity',
                                    hidden_layer_sizes=[15, 10, 5],
                                    learning_rate_init=.1)
     self.load_digits_data()
     self._port_estimator()
     amin = np.amin(self.X, axis=0)
     amax = np.amax(self.X, axis=0)
     preds, ground_truth = [], []
     for _ in range(self.N_RANDOM_FEATURE_SETS):
         x = np.random.uniform(amin, amax, self.n_features)
         preds.append(self.pred_in_custom(x))
         ground_truth.append(self.pred_in_py(x))
     self._clear_estimator()
     # noinspection PyUnresolvedReferences
     self.assertListEqual(preds, ground_truth)
def ex_2_2(input1, target1, input2, target2):
    """
    Solution for exercise 2.2
    :param input1: The input from dataset1
    :param target1: The target from dataset1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """
    n = 10

    train_acc = np.zeros(n)
    test_acc = np.zeros(n)
    pred_test = np.zeros((n, 564))
    coefs = np.zeros((n, 960, 20))

    #print(min(target1[:,0]), max(target1[:,0]))
    # we have 20 person

    for i in range(n):
        classifier = MLPClassifier(hidden_layer_sizes=(20, ),
                                   activation='tanh',
                                   solver='adam',
                                   max_iter=5000,
                                   random_state=i)
        classifier.fit(input1, target1[:, 0])
        pred_test[i] = classifier.predict(input2)
        coefs[i] = classifier.coefs_[0]
        train_acc[i] = classifier.score(input1, target1[:, 0])
        test_acc[i] = classifier.score(input2, target2[:, 0])

    error = pred_test[1] - target2[:, 0]
    for j in range(len(error)):
        if (error[j] != 0):
            print(j)
    plot_random_images(np.row_stack((input2[175, :], input2[184, :])))
    plot_random_images(np.row_stack((input2[210, :], input2[134, :])))
    plot_random_images(np.row_stack((input2[223, :], input2[177, :])))
    plot_random_images(np.row_stack((input2[179, :], input2[186, :])))

    plot_histogram_of_acc(train_acc, test_acc)

    # best network with seed i=1
    confmat = confusion_matrix(target2[:, 0], pred_test[1])
    print(confmat)

    pass
def ex_2_1(input2, target2):
    """
    Solution for exercise 2.1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """
    ## TODO
    n_hidden_neurons = 6
    nn = MLPClassifier(activation='tanh', solver='adam', max_iter=200, hidden_layer_sizes=(n_hidden_neurons,))
    target = target2[:,2]
    ## Train the network
    nn.fit(input2, target)
    predictions = nn.predict(input2)
    C=confusion_matrix(target,predictions)
    hidden_layer_weights = nn.coefs_[0]
    plot_hidden_layer_weights(hidden_layer_weights)
    print(C)
def ex_2_1(input2, target2):
    """
    Solution for exercise 2.1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """

    classifier = MLPClassifier(hidden_layer_sizes=(6, ),
                               activation='tanh',
                               solver='adam',
                               max_iter=200)
    classifier.fit(input2, target2[:, 1])
    pred2 = classifier.predict(input2)
    confmat = confusion_matrix(target2[:, 1], pred2)
    coefs = classifier.coefs_
    print(confmat)
    plot_hidden_layer_weights(coefs[0])
    ## TODO
    pass
Example #29
0
def ex_2_1(input2, target2):

    target2 = np.transpose(target2)
    target2 = target2[1]
    nn = MLPClassifier(hidden_layer_sizes=(8, ),
                       activation='tanh',
                       solver='adam',
                       max_iter=200)

    model = nn.fit(input2, target2)

    y_predict = model.predict(input2)

    C = confusion_matrix(y_predict, target2)
    print(C)
    hidden_layer_weights = model.coefs_

    plot_hidden_layer_weights(hidden_layer_weights[0])

    pass
Example #30
0
    def compute(self):
        # Iterate Leave-One-Out Index over all vectors
        actual_matrix = self.get_actual_data_matrix()
        for params_list_index in range(len(self._params_list)):
            params = self._params_list[params_list_index]
            current_params_result = self._params_result_list[params_list_index]
            for loo_index in range(self.get_vector_count()):
                # Prepare data and labels for current leave one out
                train_data = [[
                    0 for x in range(self.get_actual_feature_count())
                ] for y in range(self.get_vector_count() - 1)]
                train_labels = [
                    0 for x in range(0,
                                     self.get_vector_count() - 1)
                ]
                test_data = [[
                    0 for x in range(0, self.get_actual_feature_count())
                ] for y in range(1)]
                test_labels = [0 for x in range(1)]
                y1 = 0
                for y in range(self.get_vector_count()):
                    if (y != loo_index):
                        for x in range(self.get_actual_feature_count()):
                            train_data[y1][x] = actual_matrix[y][x]
                        train_labels[y1] = self._labels[y]
                        y1 = y1 + 1
                for x in range(self.get_actual_feature_count()):
                    test_data[0][x] = actual_matrix[loo_index][x]
                    test_labels[0] = self._labels[loo_index]

                #clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1)
                clf = MLPClassifier(**params)
                clf.fit(train_data, train_labels)
                res = clf.predict(test_data)
                current_params_result.predicted_labels[loo_index] = res[0]
                #print(repr(self.get_labels()[loo_index])+"\t"+repr(res[0]))

            self._commit_params_computation(params_list_index)
        self._complete_computation()