本文整理汇总了Python中sklearn.neural_network.MLPClassifier.predict_proba方法的典型用法代码示例。如果您正苦于以下问题:Python MLPClassifier.predict_proba方法的具体用法?Python MLPClassifier.predict_proba怎么用?Python MLPClassifier.predict_proba使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.neural_network.MLPClassifier
的用法示例。
在下文中一共展示了MLPClassifier.predict_proba方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: naviBayes
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
def naviBayes(train_X, train_y, test_X, test_y):
# print train_y
# print test_y
# model = tfMultyPerceptron(train_X, train_y, test_X, test_y)
# model.run()
time_start = time.time()
model = MLPClassifier(hidden_layer_sizes=(128, 32, 32, 128), max_iter=100, early_stopping=False, learning_rate_init=0.001,
verbose=True)
# model = MultinomialNB()
# model = BernoulliNB()
# model = KNeighborsClassifier()
# model = DecisionTreeClassifier(max_depth=20, min_samples_leaf=0.01)
# model = LinearSVC(random_state=0)
# model.fit(X, y)
model.fit(train_X, train_y)
# model_1.fit(train_X, train_y)
# model_2.fit(train_X, train_y)
# model_3.fit(train_X, train_y)
# model_4.fit(train_X, train_y)
# model_5.fit(train_X, train_y)
# All_model = [model, model_1, model_2, model_3, model_4, model_5]
# train_pre = predct_all(All_model, train_X, train_y)
# test_pre = predct_all(All_model, test_X, test_y)
time_end = time.time()
print "perceptron training cost time:{}".format(time_end - time_start)
# model = OneVsRestClassifier(SVC(kernel='linear'))
# model.fit(train_X, train_y)
# save
with open(config.BTMData + 'BayesModel/BTM_perceptron.model', 'wb') as fp:
cPickle.dump(model, fp)
# load model
# model = None
# with open(config.BTMData + 'BayesModel/bayes_BTM.model', 'rb') as fp:
# model = cPickle.load(fp)
# print 'train data set size:', len(train_y)
# result = metrics.accuracy_score(train_pre, train_y)
# 返回各自文本的所被分配到的类索引
# print"Predicting random boost train result: ", result
# print 'train data set size:', len(train_y)
# result = metrics.accuracy_score(test_pre, test_y)
# 返回各自文本的所被分配到的类索引
# print "Predicting random boost test result:", result
print 'train data set size:', len(train_y)
result = model.score(train_X, train_y)
# 返回各自文本的所被分配到的类索引
print"Predicting train result: ", result
test_result = model.score(test_X, test_y)
print "Predicting test set result: ", test_result
top_train_result = model.predict_proba(train_X)
print "top 3 predict train data accuracy rate: {}".format(cal_topThreeScore(model, top_train_result, train_y))
top_test_result = model.predict_proba(test_X)
print "top 3 predict test data accuracy rate: {}".format(cal_topThreeScore(model, top_test_result, test_y))
示例2: test2
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
def test2():
X = [[0., 0.], [1., 1.]]
y = [0, 1]
clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(3), random_state=1, activation='relu')
clf.fit(X,y)
test_sample = [[2., 2.], [-1., -2.]]
print clf.predict(test_sample)
print clf.predict_proba(test_sample)
output_mlp(clf)
示例3: main
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
def main():
enc = OneHotEncoder(n_values=[7,7,7,7,7,7])
burgers = pandas.read_hdf('../../../machine/data.h5', 'df')
X = burgers.drop(['output'], axis=1)
y = burgers['output']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)
clf = MLPClassifier(solver='adam', activation='relu',
hidden_layer_sizes=64,
verbose=False,
max_iter=10000,
tol=1e-9,
random_state=1)
classes = numpy.unique(y)
i = 0
while True:
burgers = X_train[y_train == 1]
notburgers = X_train[y_train == 0]
# Pull 32 samples from training data,
# where half the samples come from each class
sample = burgers.sample(16).join(y_train)
sample = sample.append(notburgers.sample(16).join(y_train))
sample_X_train = sample.drop(['output'], axis=1)
sample_y_train = sample['output']
sample_X_train_categoricals = sample_X_train[column_names]
tX_sample_train_categoricals = enc.fit_transform(sample_X_train_categoricals)
clf.partial_fit(tX_sample_train_categoricals, sample_y_train.as_matrix().astype(int), classes=classes)
if (i % 5) == 0:
print(i)
X_test_categoricals = X_test[column_names]
tX_test_categoricals = enc.fit_transform(X_test_categoricals)
prediction = clf.predict(tX_test_categoricals)
print_eval(y_test, prediction)
print(classification_report(y_test, prediction))
i += 1
X_train_categoricals = X_train[column_names]
tX_train_categoricals = enc.fit_transform(X_train_categoricals)
probs = clf.predict_proba(tX_train_categoricals)
# Store the probabilities
X_train_copy = X_train.copy()
X_train_copy['prob_notburger'] = probs[:,0]
X_train_copy['prob_burger'] = probs[:,1]
X_train_categoricals = X_train_copy[column_names]
tX_train_categoricals = enc.fit_transform(X_train_categoricals)
prediction = clf.predict(tX_train_categoricals)
pickle.dump(clf, open("clf.pkl.tmp", "wb"))
os.rename("clf.pkl.tmp", "clf.pkl")
示例4: predictNN
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
def predictNN(self):
print("Start Predict")
df = pd.read_csv("./samples/regInput.csv")
cols = df.columns.tolist()
y = df[cols[0]] # variable to predict
le = LabelEncoder()
y = le.fit_transform(y)
X = df[cols[1:]]
y = np.ravel(y)
train_X = X.as_matrix()
#22,2 -> 0.763
#23,2 -> 0.786
#24,2 -> 0.783
clf = MLPClassifier(activation="logistic", solver='lbfgs', alpha=1e-3, hidden_layer_sizes=(5, 5, 2), random_state=1)
clf.fit(X, y)
cpt = 0
for fpTest in self.testSet:
print(cpt)
cpt += 1
f = open("./samples/regInputPred.csv", 'w')
f.write("sameUser,browser,os,browserVersion,httpLanguages,acceptHttp,encodingHttp,timezoneJs,pluginsSubset,resolutionJs,adblock,plugins,canvasJs,platformJs,dntJs,cookiesJs,localJs,flashBlockedExt,fontsSubset,fonts,platformFlash,resolutionFlash\n")
for fpTrain in self.trainSet:
resultComparaison = self.computeSimilirarity(fpTest, fpTrain)
f.write(resultComparaison + "\n")
f.close()
dfPredict = pd.read_csv("./samples/regInputPred.csv")
cols = dfPredict.columns.tolist()
yp = dfPredict[cols[0]]
Xp = dfPredict[cols[1:]]
predicted = clf.predict_proba(Xp.as_matrix())
nearest = (-predicted[:, 0]).argsort()[:30]
#0.93 original
if predicted[nearest[0], 0] > 0.90:
self.predictions[fpTest.counter] = self.trainSet[nearest[0]].id
else:
self.predictions[fpTest.counter] = None
res = fpTest.id == self.trainSet[nearest[0]].id
print("Prediction : " + str(fpTest.counter) + " ," + str(self.predictions[fpTest.counter]) + ", " + str(res))
示例5: test_predict_proba_multi
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
def test_predict_proba_multi():
# Test that predict_proba works as expected for multi class."""
X = X_digits_multi[:10]
y = y_digits_multi[:10]
clf = MLPClassifier(hidden_layer_sizes=5)
clf.fit(X, y)
y_proba = clf.predict_proba(X)
y_log_proba = clf.predict_log_proba(X)
(n_samples, n_classes) = y.shape[0], np.unique(y).size
proba_max = y_proba.argmax(axis=1)
proba_log_max = y_log_proba.argmax(axis=1)
assert_equal(y_proba.shape, (n_samples, n_classes))
assert_array_equal(proba_max, proba_log_max)
assert_array_equal(y_log_proba, np.log(y_proba))
示例6: learn_and_predict_mlp
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
def learn_and_predict_mlp(self, dataset='train'):
#predictors = ["Pclass", "Sex", "Age", "Fare", "Embarked", "FamilySize", "FamilyId"]
predictors = self.PREDICTORS
if dataset == 'train':
param_dist = {
'algorithm': ['l-bfgs'],
'learning_rate': ['constant'],
'learning_rate_init': [0.05, 0.01, 0.005, 0.001], # what is this
'hidden_layer_sizes': [(8,), (9,), (10,), (11,), (12,), (13,), (14,), (15,)],
'activation': ['logistic', 'tanh', 'relu'],
'alpha':[0.00001, 0.0001, 0.001, 0.01, 0.1, 1.],
'verbose': [False],
'max_iter': [200],
}
clf = MLPClassifier()
'''
random_search = RandomizedSearchCV(clf, param_distributions=param_dist, n_iter=400, cv=3)
random_search.fit(self.mlp_train_df[predictors], self.mlp_train_df['Survived'])
report(random_search.grid_scores_)
'''
elif dataset == 'test':
clf = MLPClassifier(learning_rate='constant', learning_rate_init=0.01, verbose=False, hidden_layer_sizes=(8,), max_iter=200, alpha=0.01, activation='relu', random_state=1)
clf.fit(self.mlp_train_df[predictors], self.mlp_train_df['Survived'])
predictions = clf.predict(self.mlp_test_df[predictors])
predictions_proba = clf.predict_proba(self.mlp_test_df[predictors])
predictions_proba = [f[1] for f in predictions_proba]
submission = pd.DataFrame({
'PassengerId': self.test_df['PassengerId'],
'Survived': predictions,
})
submission.to_csv('./mlpclassifier_1222.csv', index=False)
submission_proba = pd.DataFrame({
'PassengerId': self.test_df['PassengerId'],
'Survived': predictions_proba,
})
submission_proba.to_csv('./mlpclassifier_1222_soft.csv', index=False)
示例7: test_predict_proba_binary
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
def test_predict_proba_binary():
# Test that predict_proba works as expected for binary class."""
X = X_digits_binary[:50]
y = y_digits_binary[:50]
clf = MLPClassifier(hidden_layer_sizes=5)
clf.fit(X, y)
y_proba = clf.predict_proba(X)
y_log_proba = clf.predict_log_proba(X)
(n_samples, n_classes) = y.shape[0], 2
proba_max = y_proba.argmax(axis=1)
proba_log_max = y_log_proba.argmax(axis=1)
assert_equal(y_proba.shape, (n_samples, n_classes))
assert_array_equal(proba_max, proba_log_max)
assert_array_equal(y_log_proba, np.log(y_proba))
assert_equal(roc_auc_score(y, y_proba[:, 1]), 1.0)
示例8: test_predict_proba_binary
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
def test_predict_proba_binary():
# Test that predict_proba works as expected for binary class.
X = X_digits_binary[:50]
y = y_digits_binary[:50]
clf = MLPClassifier(hidden_layer_sizes=5, activation='logistic',
random_state=1)
with ignore_warnings(category=ConvergenceWarning):
clf.fit(X, y)
y_proba = clf.predict_proba(X)
y_log_proba = clf.predict_log_proba(X)
(n_samples, n_classes) = y.shape[0], 2
proba_max = y_proba.argmax(axis=1)
proba_log_max = y_log_proba.argmax(axis=1)
assert_equal(y_proba.shape, (n_samples, n_classes))
assert_array_equal(proba_max, proba_log_max)
assert_array_equal(y_log_proba, np.log(y_proba))
assert_equal(roc_auc_score(y, y_proba[:, 1]), 1.0)
示例9: test_predict_proba_multilabel
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
def test_predict_proba_multilabel():
# Test that predict_proba works as expected for multilabel.
# Multilabel should not use softmax which makes probabilities sum to 1
X, Y = make_multilabel_classification(n_samples=50, random_state=0,
return_indicator=True)
n_samples, n_classes = Y.shape
clf = MLPClassifier(solver='lbfgs', hidden_layer_sizes=30,
random_state=0)
clf.fit(X, Y)
y_proba = clf.predict_proba(X)
assert_equal(y_proba.shape, (n_samples, n_classes))
assert_array_equal(y_proba > 0.5, Y)
y_log_proba = clf.predict_log_proba(X)
proba_max = y_proba.argmax(axis=1)
proba_log_max = y_log_proba.argmax(axis=1)
assert_greater((y_proba.sum(1) - 1).dot(y_proba.sum(1) - 1), 1e-10)
assert_array_equal(proba_max, proba_log_max)
assert_array_equal(y_log_proba, np.log(y_proba))
示例10: main
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
def main():
train_X, train_Y = import_training_data('train.csv')
test_X = import_testing_data('test.csv')
print("Preprocessing Training Data...")
train_X = preprocess(train_X)
print("Preprocessing Testng Data...")
test_X = preprocess(test_X)
print("Generate grid")
train_X, test_X = generate_grid(train_X, test_X, no_grid=100)
print("Fitting model")
model = MLPClassifier(verbose=True, hidden_layer_sizes=(100, 20,), activation='logistic', shuffle=True,
algorithm='adam', random_state=0)
model.fit(train_X, train_Y[train_X.index])
print(np.sum(model.predict(train_X) == train_Y[train_X.index])*10000/len(train_X.index)/100)
test_Y = pd.DataFrame(model.predict_proba(test_X), index=test_X.index, columns=model.classes_)
print(test_Y[:5])
write_result(test_Y, 'MLP_logistic')
示例11: print
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
X_test[rownum][colnum]=row[colnum]
colnum += 1
rownum += 1
'''
#### Now train a simple SVM classifier ####
'''
print('X_train: ', X_train[0:1,0:1],np.shape(X_train))
print('X_test: ', X_test[0:1,0:1],np.shape(X_test))
clf = MLPClassifier(algorithm='sgd', alpha=1e-5, learning_rate='adaptive', hidden_layer_sizes=(100,))
clf.fit(X_train,y)
#clf.fit(X_train[0:5000,:],y[0:5000])
pred = clf.predict_proba(X_test)
pred = pred[:,1]
print(pred[0:10])
output = np.zeros(3401,dtype=object)
with open('SampleSubmission.csv', 'rb') as csvfile:
trial_reader = csv.reader(csvfile, delimiter=',', quotechar='|')
rownum = 0
for row in trial_reader:
#print(row[0])
if rownum>0:
output[rownum]=row[0]
rownum += 1
示例12: runns
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
def runns(resp_var, size_of_test_data,dataset,positive_class,n_estimators,important_features,dealing_with_nulls):
dataset = pd.read_csv('raw_data.csv', low_memory=False) # For testing purposes
#----DATA PREPROCESSING
#-------dealing with NULL values in the data
#----------remove the rows in which the response is null
dataset=dataset.dropna(subset=[resp_var])
#----------dealing with nulls
dataset=deal_with_nulls(dealing_with_nulls,dataset)
#----FEATURE SELECTION
#-------get predictors important in predicting the response
#-----------transform categorical predictors to dummy variables
predictors=dataset.drop(resp_var,axis=1,inplace=False)
predictors=pd.get_dummies(predictors)
#-----------balance the classes in the response var
ros = RandomOverSampler(random_state=0)
resp=dataset[resp_var]
prds, resp = ros.fit_sample(predictors, resp)
#-----------fit the random forest classifier to give us the important predictors
rf_clf = RandomForestClassifier(n_estimators=n_estimators)
rf_clf.fit(prds,resp)
#-------get the important predictors
feature_imp = pd.Series(rf_clf.feature_importances_,
index=list(predictors.iloc[:,0:])).sort_values(ascending=False)
#-------names of the important predictors
important_predictor_names = feature_imp.index[0:important_features]
#-------subset the data to get only the important predictors and the response
resp=pd.DataFrame(data=resp,columns=[resp_var])
predictors=pd.DataFrame(prds,columns=list(predictors))
dataset=pd.concat([resp,predictors],axis=1)
#---------------------------------------------------------
#----MODEL TRAINING
#--------Remove the response variables from the features variables - axis 1 refers to the columns
m_data= dataset.drop(resp_var, axis = 1,inplace=False)
# Response variables are the values we want to predict
resp_var = np.array(dataset[resp_var])
dataset = pd.get_dummies(m_data)
# Saving feature names for later use
feature_list = list(m_data.columns)
# Convert to numpy array
dataset = np.array(dataset)
# Split the data into training and testing sets
train_features, test_features, train_labels, test_labels = train_test_split(dataset, resp_var, test_size = size_of_test_data, random_state = 402)
# Instantiate model with n_estimators decision trees
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5, 2), random_state=13563)
# Train the model on training data
clf.fit(train_features, train_labels)
# evaluation
predicted = clf.predict(test_features)
pred_prob = clf.predict_proba(test_features)
accuracy = accuracy_score(test_labels, predicted)
#confusion matrix
cnf = (confusion_matrix(test_labels,predicted))
#precision score
precision = precision_score(test_labels,predicted,pos_label=positive_class)
#avg pres
avg_precision = average_precision_score(test_labels,pred_prob[:,[1]])
#recall score
rec = recall_score(test_labels,predicted,pos_label=positive_class)
#f1 scorea
fscore = f1_score(test_labels,predicted,pos_label=positive_class)
#fbeta score
fbeta = fbeta_score(test_labels,predicted,beta=0.5)
#hamming_loss
hamming = hamming_loss(test_labels,predicted)
#jaccard similarity score
jaccard = jaccard_similarity_score(test_labels,predicted)
#logloss
logloss = log_loss(test_labels,predicted)
#zero-oneloss
zero_one = zero_one_loss(test_labels,predicted)
#auc roc
area_under_roc = roc_auc_score(test_labels,pred_prob[:,[1]])
#cohen_score
cohen = cohen_kappa_score(test_labels,predicted)
#mathews corr
mathews = matthews_corrcoef(test_labels,predicted)
# Variable importances from the important features selection stage
variable_importance_list = list(zip(prds, feature_imp))
output={"accuracy":accuracy,"precision":precision,"average precision":avg_precision,"recall":rec,"fscore":fscore,"fbeta":fbeta,"hamming":hamming,"jaccard":jaccard,"logloss":logloss,"zero_one":zero_one,"area_under_roc":area_under_roc,"cohen":cohen,"mathews":mathews}
output=json.dumps(output)
return jsonify({"Prediction": output})
示例13: MLPClassifier
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
clf = MLPClassifier(
alpha=1e-3, hidden_layer_sizes=(5), max_iter=5000, verbose=True, random_state=np.random.RandomState(1)
)
clf.fit(training_data[:, 1:], training_data[:, 0])
#matrix
print('Hidden Weights:')
for l in clf.coefs_[0].T:
print('-', l.tolist())
print('Hidden Bias')
print(clf.intercepts_[0].tolist())
print('Output Weights:')
for l in clf.coefs_[1].T[0]:
print('-', str([l]))
print('Output Bias')
print(clf.intercepts_[1])
probs = clf.predict_proba(test_data[:, 1:])
out = np.hstack([(probs[:, 1] > .5).reshape(-1, 1), probs])
# Write out our validation output
with open('validation_prediction', 'w') as f:
f.write('labels 0 1\n')
for d in out:
f.write(str(list(d)).strip('[]').replace(',', '') + '\n')
with open('validation_ground_truth', 'w') as f:
for d in test_data:
f.write('{}\n'.format(d[0]))
示例14: test_fit
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
def test_fit():
# Test that the algorithm solution is equal to a worked out example.
X = np.array([[0.6, 0.8, 0.7]])
y = np.array([0])
mlp = MLPClassifier(solver='sgd', learning_rate_init=0.1, alpha=0.1,
activation='logistic', random_state=1, max_iter=1,
hidden_layer_sizes=2, momentum=0)
# set weights
mlp.coefs_ = [0] * 2
mlp.intercepts_ = [0] * 2
mlp.n_outputs_ = 1
mlp.coefs_[0] = np.array([[0.1, 0.2], [0.3, 0.1], [0.5, 0]])
mlp.coefs_[1] = np.array([[0.1], [0.2]])
mlp.intercepts_[0] = np.array([0.1, 0.1])
mlp.intercepts_[1] = np.array([1.0])
mlp._coef_grads = [] * 2
mlp._intercept_grads = [] * 2
# Initialize parameters
mlp.n_iter_ = 0
mlp.learning_rate_ = 0.1
# Compute the number of layers
mlp.n_layers_ = 3
# Pre-allocate gradient matrices
mlp._coef_grads = [0] * (mlp.n_layers_ - 1)
mlp._intercept_grads = [0] * (mlp.n_layers_ - 1)
mlp.out_activation_ = 'logistic'
mlp.t_ = 0
mlp.best_loss_ = np.inf
mlp.loss_curve_ = []
mlp._no_improvement_count = 0
mlp._intercept_velocity = [np.zeros_like(intercepts) for
intercepts in
mlp.intercepts_]
mlp._coef_velocity = [np.zeros_like(coefs) for coefs in
mlp.coefs_]
mlp.partial_fit(X, y, classes=[0, 1])
# Manually worked out example
# h1 = g(X1 * W_i1 + b11) = g(0.6 * 0.1 + 0.8 * 0.3 + 0.7 * 0.5 + 0.1)
# = 0.679178699175393
# h2 = g(X2 * W_i2 + b12) = g(0.6 * 0.2 + 0.8 * 0.1 + 0.7 * 0 + 0.1)
# = 0.574442516811659
# o1 = g(h * W2 + b21) = g(0.679 * 0.1 + 0.574 * 0.2 + 1)
# = 0.7654329236196236
# d21 = -(0 - 0.765) = 0.765
# d11 = (1 - 0.679) * 0.679 * 0.765 * 0.1 = 0.01667
# d12 = (1 - 0.574) * 0.574 * 0.765 * 0.2 = 0.0374
# W1grad11 = X1 * d11 + alpha * W11 = 0.6 * 0.01667 + 0.1 * 0.1 = 0.0200
# W1grad11 = X1 * d12 + alpha * W12 = 0.6 * 0.0374 + 0.1 * 0.2 = 0.04244
# W1grad21 = X2 * d11 + alpha * W13 = 0.8 * 0.01667 + 0.1 * 0.3 = 0.043336
# W1grad22 = X2 * d12 + alpha * W14 = 0.8 * 0.0374 + 0.1 * 0.1 = 0.03992
# W1grad31 = X3 * d11 + alpha * W15 = 0.6 * 0.01667 + 0.1 * 0.5 = 0.060002
# W1grad32 = X3 * d12 + alpha * W16 = 0.6 * 0.0374 + 0.1 * 0 = 0.02244
# W2grad1 = h1 * d21 + alpha * W21 = 0.679 * 0.765 + 0.1 * 0.1 = 0.5294
# W2grad2 = h2 * d21 + alpha * W22 = 0.574 * 0.765 + 0.1 * 0.2 = 0.45911
# b1grad1 = d11 = 0.01667
# b1grad2 = d12 = 0.0374
# b2grad = d21 = 0.765
# W1 = W1 - eta * [W1grad11, .., W1grad32] = [[0.1, 0.2], [0.3, 0.1],
# [0.5, 0]] - 0.1 * [[0.0200, 0.04244], [0.043336, 0.03992],
# [0.060002, 0.02244]] = [[0.098, 0.195756], [0.2956664,
# 0.096008], [0.4939998, -0.002244]]
# W2 = W2 - eta * [W2grad1, W2grad2] = [[0.1], [0.2]] - 0.1 *
# [[0.5294], [0.45911]] = [[0.04706], [0.154089]]
# b1 = b1 - eta * [b1grad1, b1grad2] = 0.1 - 0.1 * [0.01667, 0.0374]
# = [0.098333, 0.09626]
# b2 = b2 - eta * b2grad = 1.0 - 0.1 * 0.765 = 0.9235
assert_almost_equal(mlp.coefs_[0], np.array([[0.098, 0.195756],
[0.2956664, 0.096008],
[0.4939998, -0.002244]]),
decimal=3)
assert_almost_equal(mlp.coefs_[1], np.array([[0.04706], [0.154089]]),
decimal=3)
assert_almost_equal(mlp.intercepts_[0],
np.array([0.098333, 0.09626]), decimal=3)
assert_almost_equal(mlp.intercepts_[1], np.array(0.9235), decimal=3)
# Testing output
# h1 = g(X1 * W_i1 + b11) = g(0.6 * 0.098 + 0.8 * 0.2956664 +
# 0.7 * 0.4939998 + 0.098333) = 0.677
# h2 = g(X2 * W_i2 + b12) = g(0.6 * 0.195756 + 0.8 * 0.096008 +
# 0.7 * -0.002244 + 0.09626) = 0.572
# o1 = h * W2 + b21 = 0.677 * 0.04706 +
# 0.572 * 0.154089 + 0.9235 = 1.043
# prob = sigmoid(o1) = 0.739
assert_almost_equal(mlp.predict_proba(X)[0, 1], 0.739, decimal=3)
示例15: P
# 需要导入模块: from sklearn.neural_network import MLPClassifier [as 别名]
# 或者: from sklearn.neural_network.MLPClassifier import predict_proba [as 别名]
# MLP can fit a non-linear model to the training data.
# clf.coefs_ contains the weight matrices that constitute the model parameters:
[coef.shape for coef in clf.coefs_]
### PROBABILITIES
# To get the raw values before applying the output activation function, run the following command,
clf.decision_function([[2., 2.], [1., 2.]])
"""
MLP trains using Backpropagation. More precisely, it trains using
some form of gradient descent and the gradients are calculated using Backpropagation.
For classification, it minimizes the Cross-Entropy loss function,
giving a vector of probability estimates P(y|x) per sample x.
"""
clf.predict_proba([[2., 2.], [1., 2.]])
# The algorithm supports multi-label classification in which a sample can belong to more than one class.
# For each class, the output of MLPClassifier.decision_function passes through the logistic function.
# Values larger or equal to 0.5 are rounded to 1, otherwise to 0.
X = [[0., 0.], [1., 1.]]
y = [[0, 1], [1, 1]]
clf = MLPClassifier(algorithm='l-bfgs', alpha=1e-5, hidden_layer_sizes=(15,), random_state=1)
clf.fit(X, y)
clf.predict([1., 2.])
clf.predict([0., 0.])
###############################################################################