class MLPClassifierImpl(): def __init__(self, hidden_layer_sizes=(100,), activation='relu', solver='adam', alpha=0.0001, batch_size='auto', learning_rate='constant', learning_rate_init=0.001, power_t=0.5, max_iter=200, shuffle=True, random_state=None, tol=0.0001, verbose=False, warm_start=False, momentum=0.9, nesterovs_momentum=True, early_stopping=False, validation_fraction=0.1, beta_1=0.9, beta_2=0.999, epsilon=1e-08, n_iter_no_change=10): self._hyperparams = { 'hidden_layer_sizes': hidden_layer_sizes, 'activation': activation, 'solver': solver, 'alpha': alpha, 'batch_size': batch_size, 'learning_rate': learning_rate, 'learning_rate_init': learning_rate_init, 'power_t': power_t, 'max_iter': max_iter, 'shuffle': shuffle, 'random_state': random_state, 'tol': tol, 'verbose': verbose, 'warm_start': warm_start, 'momentum': momentum, 'nesterovs_momentum': nesterovs_momentum, 'early_stopping': early_stopping, 'validation_fraction': validation_fraction, 'beta_1': beta_1, 'beta_2': beta_2, 'epsilon': epsilon, 'n_iter_no_change': n_iter_no_change} def fit(self, X, y=None): self._sklearn_model = SKLModel(**self._hyperparams) if (y is not None): self._sklearn_model.fit(X, y) else: self._sklearn_model.fit(X) return self def predict(self, X): return self._sklearn_model.predict(X) def predict_proba(self, X): return self._sklearn_model.predict_proba(X)
StandardScaler(copy=True, with_mean=True, with_std=True) #now apply the transformations to the data: x_train_nn = scaler.transform(X) x_test_nn = scaler.transform(X_test) nn = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1) print(nn.fit(x_train_nn, y)) print('Neural network model:') nn_pred_test = nn.predict(x_test_nn) #compute confusion matrix from sklearn import metrics #pred_obj = np.where(predictions==predictions[0],'N','Y') #print(pred_obj) cnf_matrix = metrics.confusion_matrix(y_test, nn_pred_test) print(cnf_matrix) #compute roc cureve import matplotlib.pyplot as plt y_pred_proba = nn.predict_proba(X_test)[::, 1] y_binary = np.where(y == 'N', 0, 1) fpr, tpr, _ = metrics.roc_curve(y_binary, y_pred_proba) auc = metrics.roc_auc_score(y_binary, y_pred_proba) plt.plot(fpr, tpr, label="data 1, auc=" + str(auc)) plt.legend(loc=4) plt.show()