本文整理匯總了Python中sklearn.neural_network.MLPRegressor.fit方法的典型用法代碼示例。如果您正苦於以下問題:Python MLPRegressor.fit方法的具體用法?Python MLPRegressor.fit怎麽用?Python MLPRegressor.fit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.neural_network.MLPRegressor
的用法示例。
在下文中一共展示了MLPRegressor.fit方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: mlp_bench
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def mlp_bench(x_train, y_train, x_test, fh):
"""
Forecasts using a simple MLP which 6 nodes in the hidden layer
:param x_train: train input data
:param y_train: target values for training
:param x_test: test data
:param fh: forecasting horizon
:return:
"""
y_hat_test = []
model = MLPRegressor(hidden_layer_sizes=6, activation='identity', solver='adam',
max_iter=100, learning_rate='adaptive', learning_rate_init=0.001,
random_state=42)
model.fit(x_train, y_train)
last_prediction = model.predict(x_test)[0]
for i in range(0, fh):
y_hat_test.append(last_prediction)
x_test[0] = np.roll(x_test[0], -1)
x_test[0, (len(x_test[0]) - 1)] = last_prediction
last_prediction = model.predict(x_test)[0]
return np.asarray(y_hat_test)
示例2: regression
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def regression(N, P):
assert len(N) == len(P)
clf = MLPRegressor(hidden_layer_sizes=(15, ), activation='relu', algorithm='adam', alpha=0.0001)
clf.fit (N, P)
return clf
示例3: _create_first_population
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def _create_first_population(self):
self._current_population = []
for _ in range(self._n_individuals):
mlp = MLPRegressor(hidden_layer_sizes = self._nn_architecture, alpha=10**-10, max_iter=1)
mlp.fit([np.random.randn(self._n_features)], [np.random.randn(self._n_actions)])
mlp.out_activation_ = 'softmax'
self._current_population.append([mlp,0])
示例4: _create_new_nn
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def _create_new_nn(self, weights, biases):
mlp = MLPRegressor(hidden_layer_sizes = self._nn_architecture, alpha=10**-10, max_iter=1)
mlp.fit([np.random.randn(self._n_features)], [np.random.randn(self._n_actions)])
mlp.coefs_ = weights
mlp.intercepts_ = biases
mlp.out_activation_ = 'softmax'
return mlp
示例5: construct_train
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def construct_train(train_length, **kwargs):
"""
Train and test model with given input
window and number of neurons in layer
"""
start_cur_postion = 0
steps, steplen = observations.size/(2 * train_length), train_length
if 'hidden_layer' in kwargs:
network = MLPRegressor(hidden_layer_sizes=kwargs['hidden_layer'])
else:
network = MLPRegressor()
quality = []
# fit model - configure parameters
network.fit(observations[start_cur_postion:train_length][:, 1].reshape(1, train_length),
observations[:, 1][start_cur_postion:train_length].reshape(1, train_length))
parts = []
# calculate predicted values
# for each step add all predicted values to a list
# TODO: add some parallelism here
for i in xrange(0, steps):
parts.append(network.predict(observations[start_cur_postion:train_length][:, 1]))
start_cur_postion += steplen
train_length += steplen
# estimate model quality using
result = np.array(parts).flatten().tolist()
for valnum, value in enumerate(result):
quality.append((value - observations[valnum][1])**2)
return sum(quality)/len(quality)
示例6: test_multioutput_regression
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def test_multioutput_regression():
# Test that multi-output regression works as expected
X, y = make_regression(n_samples=200, n_targets=5)
mlp = MLPRegressor(solver='lbfgs', hidden_layer_sizes=50, max_iter=200,
random_state=1)
mlp.fit(X, y)
assert_greater(mlp.score(X, y), 0.9)
示例7: test_lbfgs_regression
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def test_lbfgs_regression():
# Test lbfgs on the boston dataset, a regression problems."""
X = Xboston
y = yboston
for activation in ACTIVATION_TYPES:
mlp = MLPRegressor(algorithm='l-bfgs', hidden_layer_sizes=50,
max_iter=150, shuffle=True, random_state=1,
activation=activation)
mlp.fit(X, y)
assert_greater(mlp.score(X, y), 0.95)
示例8: GetOptimalCLF2
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def GetOptimalCLF2(train_x,train_y,rand_starts = 8):
'''
Gets the optimal CLF function based on fixed settings
Parameters
------------------------
train_x - np.array
Training feature vectors
train_y - np.array
Training label vectors
rand_starts - int
Number of random starts to do
Default - 8 for 95% confidence and best 30%
Returns
------------------------
max_clf - sklearn function
Optimal trained artificial neuron network
'''
#### Get number of feature inputs of training vector
n_input = train_x.shape[1]
#### Set initial loss value
min_loss = 1e10
#### Perform number of trainings according to random start set
for i in range(rand_starts):
#### Print current status
print "Iteration number {}".format(i+1)
#### Initialize ANN network
clf = MLPRegressor(hidden_layer_sizes = (int(round(2*np.sqrt(n_input),0)),1), activation = 'logistic',solver = 'sgd',
learning_rate = 'adaptive', max_iter = 100000000,tol = 1e-10,
early_stopping = True, validation_fraction = 1/3.)
#### Fit data
clf.fit(train_x,train_y)
#### Get current loss
cur_loss = clf.loss_
#### Save current clf if loss is minimum
if cur_loss < min_loss:
#### Set min_loss to a new value
min_loss = cur_loss
#### Set max_clf to new value
max_clf = clf
return max_clf
示例9: MLP_Regressor
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def MLP_Regressor(train_x, train_y):
clf = MLPRegressor( alpha=1e-05,
batch_size='auto', beta_1=0.9, beta_2=0.999, early_stopping=False,
epsilon=1e-08, hidden_layer_sizes=([8,8]), learning_rate='constant',
learning_rate_init=0.01, max_iter=500, momentum=0.9,
nesterovs_momentum=True, power_t=0.5, random_state=1, shuffle=True,
tol=0.0001, validation_fraction=0.1, verbose=False,
warm_start=False)
clf.fit(train_x, train_y)
#score = metrics.accuracy_score(clf.predict((train_x)), (train_y))
#print(score)
return clf
示例10: test_lbfgs_regression
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def test_lbfgs_regression():
# Test lbfgs on the boston dataset, a regression problems.
X = Xboston
y = yboston
for activation in ACTIVATION_TYPES:
mlp = MLPRegressor(solver='lbfgs', hidden_layer_sizes=50,
max_iter=150, shuffle=True, random_state=1,
activation=activation)
mlp.fit(X, y)
if activation == 'identity':
assert_greater(mlp.score(X, y), 0.84)
else:
# Non linear models perform much better than linear bottleneck:
assert_greater(mlp.score(X, y), 0.95)
示例11: test_shuffle
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def test_shuffle():
# Test that the shuffle parameter affects the training process (it should)
X, y = make_regression(n_samples=50, n_features=5, n_targets=1,
random_state=0)
# The coefficients will be identical if both do or do not shuffle
for shuffle in [True, False]:
mlp1 = MLPRegressor(hidden_layer_sizes=1, max_iter=1, batch_size=1,
random_state=0, shuffle=shuffle)
mlp2 = MLPRegressor(hidden_layer_sizes=1, max_iter=1, batch_size=1,
random_state=0, shuffle=shuffle)
mlp1.fit(X, y)
mlp2.fit(X, y)
assert np.array_equal(mlp1.coefs_[0], mlp2.coefs_[0])
# The coefficients will be slightly different if shuffle=True
mlp1 = MLPRegressor(hidden_layer_sizes=1, max_iter=1, batch_size=1,
random_state=0, shuffle=True)
mlp2 = MLPRegressor(hidden_layer_sizes=1, max_iter=1, batch_size=1,
random_state=0, shuffle=False)
mlp1.fit(X, y)
mlp2.fit(X, y)
assert not np.array_equal(mlp1.coefs_[0], mlp2.coefs_[0])
示例12: train_model
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def train_model(x_train, y_train, alpha=1e-3, hid_layers=[512], max_iter=100):
"""
Train model on training data.
:param x_train: training examples
:param y_train: target variables
:param alpha: L2 regularization coefficient
:param hid_layers: hidden layer sizes
:param max_iter: maximum number of iterations in L-BFGS optimization
:return a model trained with neuron network
"""
nn_model = MLPRegressor(solver='lbgfs', hidden_layer_sizes=hid_layers,
alpha=alpha, max_iter=max_iter,
activation="relu", random_state=1)
nn_model.fit(x_train, y_train)
return nn_model
示例13: test_partial_fit_regression
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
def test_partial_fit_regression():
# Test partial_fit on regression.
# `partial_fit` should yield the same results as 'fit' for regression.
X = Xboston
y = yboston
for momentum in [0, .9]:
mlp = MLPRegressor(solver='sgd', max_iter=100, activation='relu',
random_state=1, learning_rate_init=0.01,
batch_size=X.shape[0], momentum=momentum)
with warnings.catch_warnings(record=True):
# catch convergence warning
mlp.fit(X, y)
pred1 = mlp.predict(X)
mlp = MLPRegressor(solver='sgd', activation='relu',
learning_rate_init=0.01, random_state=1,
batch_size=X.shape[0], momentum=momentum)
for i in range(100):
mlp.partial_fit(X, y)
pred2 = mlp.predict(X)
assert_almost_equal(pred1, pred2, decimal=2)
score = mlp.score(X, y)
assert_greater(score, 0.75)
示例14: MLPRegressor
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
#Example with a Regressor using the scikit-learn library
# example for the XOr gate
from sklearn.neural_network import MLPRegressor
X = [[0., 0.],[0., 1.], [1., 0.], [1., 1.]] # each one of the entries 00 01 10 11
y = [0, 1, 1, 0] # outputs for each one of the entries
# check http://scikit-learn.org/dev/modules/generated/sklearn.neural_network.MLPRegressor.html#sklearn.neural_network.MLPRegressor
#for more details
reg = MLPRegressor(hidden_layer_sizes=(5),activation='tanh', algorithm='sgd', alpha=0.001, learning_rate='constant',
max_iter=10000, random_state=None, verbose=False, warm_start=False, momentum=0.8, tol=10e-8, shuffle=False)
reg.fit(X,y)
outp = reg.predict([[0., 0.],[0., 1.], [1., 0.], [1., 1.]])
print'Results:'
print '0 0 0:', outp[0]
print '0 1 1:', outp[1]
print '1 0 1:', outp[2]
print '1 1 0:', outp[0]
print'Score:', reg.score(X, y)
示例15: getKaggleMNIST
# 需要導入模塊: from sklearn.neural_network import MLPRegressor [as 別名]
# 或者: from sklearn.neural_network.MLPRegressor import fit [as 別名]
from __future__ import print_function, division
from future.utils import iteritems
from builtins import range, input
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
from sklearn.neural_network import MLPRegressor
from util import getKaggleMNIST
# get data
X, _, Xt, _ = getKaggleMNIST()
# create the model and train it
model = MLPRegressor()
model.fit(X, X)
# test the model
print("Train R^2:", model.score(X, X))
print("Test R^2:", model.score(Xt, Xt))
Xhat = model.predict(X)
mse = ((Xhat - X)**2).mean()
print("Train MSE:", mse)
Xhat = model.predict(Xt)
mse = ((Xhat - Xt)**2).mean()
print("Test MSE:", mse)