Ejemplo n.º 1
0
def Neural(X, y):
    starttime = time.time()
    x1_train, x1_test, y1_train, y1_test = cv.train_test_split(X,
                                                               y,
                                                               test_size=0.33,
                                                               random_state=0)
    clf = mlpc(max_iter=300, random_state=1).fit(x1_train, y1_train)
    trnscore = clf.score(x1_train, y1_train)
    tstscore = clf.score(x1_test, y1_test)
    trntime = time.time() - starttime
    return (trnscore, tstscore, trntime)
Ejemplo n.º 2
0
# # XOR Model | Pickle

import numpy as np
import sklearn
import pickle
import os
from sklearn.neural_network import MLPClassifier as mlpc

os.getcwd()
os.listdir()

xs = np.array([0, 0, 0, 1, 1, 0, 1, 1]).reshape(4, 2)
ys = np.array([0, 1, 1, 0]).reshape(4, )

mdl = mlpc(activation='relu', max_iter=10000, hidden_layer_sizes=(4, 2))
mdl.fit(xs, ys)

filename = 'XOR_model.pkl'

pickle.dump(mdl, open(filename, 'wb'))

os.listdir()

# #### 'XOR_model.pkl'
#
# Model has been saved into a 'pkl' file.

# help('modules')

filename = 'XOR_model.pkl'
Ejemplo n.º 3
0
scaler = StandardScaler()

data_partial = data[data['Sex'] == sex].drop('Sex', axis=1)
# corr_matrix_f, corr_matrix_m = data_f.corr(), data_m.corr()
# plot_corr_matrices(corr_matrix_f, corr_matrix_m)

y = data_partial['EmoState']
X = scaler.fit_transform(data_partial.drop('EmoState', axis=1))
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.3,
                                                    random_state=71)

models = (('DTC', dtc()), ('SVM', svc(C=10)), ('KNN', knc(n_neighbors=10)),
          ('SGDC', sgdc()), ('GNBC', gnbc()), ('MLPC',
                                               mlpc(max_iter=1000,
                                                    learning_rate='adaptive')))
results = []
names = []
seed = 13
scoring = 'accuracy'

for name, model in models:
    kfold = model_selection.KFold(n_splits=10, random_state=seed, shuffle=True)
    cv_results = model_selection.cross_val_score(model,
                                                 X_train,
                                                 y_train,
                                                 cv=kfold,
                                                 scoring=scoring)
    results.append(cv_results)
    names.append(name)
    print('{}: {} ({})'.format(name, round(cv_results.mean(), 2),
Ejemplo n.º 4
0
        test_labels = []
        for i in train:
            train_set.append(dataset[i])
            train_labels.append(target[i])
        for i in test:
            test_set.append(dataset[i])
            test_labels.append(target[i])
        '''
            uncomment the classifier you want to use. Comment out the others
        '''
        # classifier = KNeighborsClassifier()
        # classifier = nb.GaussianNB()
        # classifier = nb.MultinomialNB()
        # classifier = svm.SVC()
        classifier = mlpc(solver='lbfgs',
                          hidden_layer_sizes=(5, 15),
                          max_iter=200)

        predicted = classifier.fit(train_set, train_labels).predict(test_set)

        score = f1_score(test_labels, predicted, average='weighted')
        scores.append(score)
        incorrect = (test_labels != predicted).sum()
        accuracy = (len(test_set) - incorrect) / len(test_set) * 100.
        accuracies.append(accuracy)
    print("Maximum accuracy attained ", max(accuracies))
    print("f1score  ", scores[np.argmax(accuracies)])
    print('\n')

print("Maximum accuracy attained ", max(accuracies))
print("f1score  ", scores[np.argmax(accuracies)])
from time import time
from split import data_split

X_train, X_test, Y_train, Y_test = data_split()

from sklearn.neural_network import MLPClassifier as mlpc

t0 = time()

clf = mlpc(alpha=1e-5, hidden_layer_sizes=(5, 5), random_state=1)
clf.fit(X_train, Y_train)

print("Training Time: " + str(round(time() - t0, 3)) + "s")

t1 = time()

pred = clf.predict(X_test)

print("Testing Time: " + str(round(time() - t1, 3)) + "s")

from sklearn.metrics import accuracy_score

print(accuracy_score(Y_test, pred))
 def __init__(self, pathToData):
     self.dataFilePath = pathToData
     self.algoname = 'ANN'
     self.datasetName = 'Letter'
     self.classifier = mlpc(early_stopping=True, learning_rate='adaptive')
     self.cv = 5