예제 #1
0
def do_MLP_regressor(X, Y):
    Y.ravel()
    """
    Call the MLP Regressor,
    Fit the weight on the training set

    Input
    ------
    X: dataframe, n*m, n is number of data points,
        m is number of features
    y: experimental electrical conductivity

    Returns
    ------
    mlp_regr : the MLP object with the best parameters
    """
    alphas = np.array([0.1, 0.01, 0.001, 0.0001])
    mlp_regr = MLPRegressor(hidden_layer_sizes=(100, ),
                            activation='tanh',
                            solver='sgd',
                            alpha=0.0001,
                            max_iter=5000,
                            random_state=None,
                            learning_rate_init=0.01)
    grid_search = GridSearchCV(mlp_regr, param_grid=dict(alpha=alphas))
    grid_search.fit(X, Y)

    #print(grid_search.best_params_)
    mlp_regr.alpha_ = grid_search.best_params_['alpha']
    mlp_regr.fit(X, Y)

    return mlp_regr
예제 #2
0
def test_do_MLP_regressor():
    """
    Test calling the MLP Regressor, 
    Fit the weight on the training set
    
    Input
    ------
    X: dataframe, n*m, n is number of data points, 
        m is number of features
    y: experimental electrical conductivity
    
    Returns
    ------
    mlp_regr : the MLP object with the best parameters

    Checking:
    1. The X and y has the name datatype
    2. The X and y has the name length
    3. The input matrix has enough data points to be splitted in the regressor
    """
    X = [[
        0, 6, 234.321, 5, 0, 1, 1, 0, 1, 18.000, 1, 0, 0, 0, 0, 298.15, 101,
        0.004
    ],
         [
             1, 6, 234.321, 5, 0, 1, 1, 0, 1, 18.000, 1, 0, 0, 0, 0, 300.15,
             101, 0.005
         ],
         [
             2, 6, 234.321, 5, 0, 1, 1, 0, 1, 18.000, 1, 0, 0, 0, 0, 302.15,
             101, 0.006
         ],
         [
             3, 6, 234.321, 5, 0, 1, 1, 0, 1, 18.000, 1, 0, 0, 0, 0, 304.15,
             101, 0.007
         ],
         [
             4, 6, 234.321, 5, 0, 1, 1, 0, 1, 18.000, 1, 0, 0, 0, 0, 306.15,
             101, 0.005
         ]]
    y = [0.02, 0.03, 0.03, 0.04, 0.05]

    assert isinstance(X, type(y)), "The two input should has the same datatype"
    assert len(X) == len(y), "Dimension mismatch between two input matrix"
    assert len(X) >= 1, "Need more data points in the input data"

    alphas = np.array([5, 2, 5, 1.5, 1, 0.1, 0.01, 0.001, 0.0001, 0])
    mlp_regr = MLPRegressor(hidden_layer_sizes=(100, ),
                            activation='relu',
                            solver='adam',
                            alpha=0.0001,
                            max_iter=5000,
                            random_state=None,
                            learning_rate_init=0.01)
    grid_search = GridSearchCV(mlp_regr, param_grid=dict(alpha=alphas))
    grid_search.fit(X, y)

    #print(grid_search.best_params_)
    mlp_regr.alpha_ = grid_search.best_params_['alpha']
    mlp_regr.fit(X, y)

    return mlp_regr