当前位置: 首页>>代码示例>>Python>>正文


Python MLPRegressor.fit方法代码示例

本文整理汇总了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)
开发者ID:KaterinaKou,项目名称:M4-methods,代码行数:27,代码来源:ML_benchmarks.py

示例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
开发者ID:JessMcintosh,项目名称:SonoGestures,代码行数:9,代码来源:NNRegCross.py

示例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])
开发者ID:fritjofwolf,项目名称:RL2048,代码行数:9,代码来源:deepneuroevolution_bot.py

示例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
开发者ID:fritjofwolf,项目名称:RL2048,代码行数:9,代码来源:deepneuroevolution_bot.py

示例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)
开发者ID:AntonKorobkov,项目名称:HW_3,代码行数:37,代码来源:homework_3_Korobkov.py

示例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)
开发者ID:aniryou,项目名称:scikit-learn,代码行数:9,代码来源:test_mlp.py

示例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)
开发者ID:0664j35t3r,项目名称:scikit-learn,代码行数:12,代码来源:test_mlp.py

示例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
开发者ID:leolorenzoii,项目名称:Development-Codes,代码行数:55,代码来源:SubsurfacePredictionANN.py

示例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
开发者ID:licheng5625,项目名称:coder,代码行数:15,代码来源:NNsklean_mult.py

示例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)
开发者ID:aniryou,项目名称:scikit-learn,代码行数:16,代码来源:test_mlp.py

示例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])
开发者ID:chrisfilo,项目名称:scikit-learn,代码行数:27,代码来源:test_mlp.py

示例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
开发者ID:minhitbk,项目名称:data-science,代码行数:18,代码来源:ETL_Modeling.py

示例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)
开发者ID:aniryou,项目名称:scikit-learn,代码行数:26,代码来源:test_mlp.py

示例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)
开发者ID:ithallojunior,项目名称:NN_compare,代码行数:24,代码来源:xor_reg.py

示例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)
开发者ID:lazyprogrammer,项目名称:machine_learning_examples,代码行数:33,代码来源:sk_mlp.py


注:本文中的sklearn.neural_network.MLPRegressor.fit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。