How do I use the MLP score function? Error: shapes (295,1) and (7,450) not aligned: 1 (dim 1) != 7 (dim 0)

How do I use the MLP score function? Error: shapes (295,1) and (7,450) not aligned: 1 (dim 1) != 7 (dim 0)

我最近开始在 Python 中编写深度神经网络,我已经被这个问题困扰了好几个星期。我查了其他类似的问题,没能搞定。

我有一个前馈神经网络,我正在尝试为我的模型获取 R^2 值。我有一个 1031 行乘 9 列的数据框。我将前 7 列(风暴特征)与第 8 列或第 9 列中给出的水位高度相匹配。这里 'h' 是我适合前 7 列的列标题。

this link 查看 csv 数据。

### Load dependencies
import pandas as pd
import numpy as np
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import accuracy_score, mean_squared_error
from sklearn.model_selection import cross_val_score
from sklearn.metrics import r2_score

data = pd.read_csv (r'C:\Users\SM\Desktop\dataframe.csv')

### Define MLP Hyper-parameters

n = 20 # ----------- Number of layers
p = 450 # ---------- Number of perceptrons in each layer
i = 1 # ------------ Initializing iteration count
MLPsize = []

### While loop to create tuple for MLP hyperparameter input
while i <= n:
    MLPsize.append(p)
    i += 1

Model = MLPRegressor(hidden_layer_sizes=tuple(MLPsize), 
                     activation='logistic', solver='adam', alpha=0.0001, 
                     batch_size='auto', learning_rate='adaptive', learning_rate_init=0.001, 
                     power_t=0.5, max_iter=5000, shuffle=False, random_state=None, tol=0.0001, 
                     verbose=False, warm_start=False, momentum=0.9, nesterovs_momentum=True, 
                     early_stopping=False, validation_fraction=0.5,
                     beta_1=0.9, beta_2=0.999, epsilon=1e-08, n_iter_no_change=10)

### Define function to run train-test simulation

msk = np.random.rand(len(data)) < 0.7
Train = data[msk]
Test = data[~msk]

Train = Train.reset_index(drop=True)
Test = Test.reset_index(drop=True)

def RunModel (h):
    global Train_pred
    global Test_pred
    global Train_true
    global Test_true
    global Train_error
    global Test_error
    Train_error = []
    Test_error = []

    Model.fit(Train.iloc[:,:7], Train[h])
    Train_pred = Model.predict(Train.iloc[:,:7])
    Train_error.append(mean_squared_error(Train[h], Train_pred))
    Train_true = np.array(Train[h]).reshape(-1, 1)
    Train_pred = Train_pred.reshape(-1, 1)

    Test_pred = Model.predict(Test.iloc[:,:7])
    Test_error.append(mean_squared_error(Test[h], Test_pred))
    Test_true = np.array(Test[h]).reshape(-1, 1)
    Test_pred = Test_pred.reshape(-1, 1)

    print(mean_squared_error(Train_pred, Train_true))

    print(Model.score(Test_pred, Test_true))

RunModel('11939')

我得到一个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-01acc869ac62> in <module>
----> 1 Model.score(Test_pred, Test_true)

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py in score(self, X, y, sample_weight)
    406         from .metrics import r2_score
    407         from .metrics.regression import _check_reg_targets
--> 408         y_pred = self.predict(X)
    409         # XXX: Remove the check in 0.23
    410         y_type, _, _, _ = _check_reg_targets(y, y_pred, None)

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\extmath.py in safe_sparse_dot(a, b, dense_output)
    140         return ret
    141     else:
--> 142         return np.dot(a, b)
    143 
    144 

**ValueError: shapes (295,1) and (7,450) not aligned: 1 (dim 1) != 7 (dim 0)**

请帮忙。任何详细的解释将不胜感激。

引发错误的行如下:

print(Model.score(Test_pred, Test_true))

这是无效的,因为 MLPRegressor.score() 方法将 (X,y) 作为输入。在这里阅读更多:https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPRegressor.html#sklearn.neural_network.MLPRegressor.score

所以,如果你想估计模型的 R2 系数,你需要传递 Xtestytest,在你的情况下是 Test.iloc[:,:7], Test_true.

### Load dependencies
import pandas as pd
import numpy as np
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import accuracy_score, mean_squared_error
from sklearn.model_selection import cross_val_score
from sklearn.metrics import r2_score

data = pd.read_csv (r'C:\Users\SM\Desktop\dataframe.csv')

### Define MLP Hyper-parameters

n = 20 # ----------- Number of layers
p = 450 # ---------- Number of perceptrons in each layer
i = 1 # ------------ Initializing iteration count
MLPsize = []

### While loop to create tuple for MLP hyperparameter input
while i <= n:
    MLPsize.append(p)
    i += 1

Model = MLPRegressor(hidden_layer_sizes=tuple(MLPsize), 
                     activation='logistic', solver='adam', alpha=0.0001, 
                     batch_size='auto', learning_rate='adaptive', learning_rate_init=0.001, 
                     power_t=0.5, max_iter=5000, shuffle=False, random_state=None, tol=0.0001, 
                     verbose=False, warm_start=False, momentum=0.9, nesterovs_momentum=True, 
                     early_stopping=False, validation_fraction=0.5,
                     beta_1=0.9, beta_2=0.999, epsilon=1e-08, n_iter_no_change=10)

### Define function to run train-test simulation

msk = np.random.rand(len(data)) < 0.7
Train = data[msk]
Test = data[~msk]

Train = Train.reset_index(drop=True)
Test = Test.reset_index(drop=True)

def RunModel (h):
    global Train_pred
    global Test_pred
    global Train_true
    global Test_true
    global Train_error
    global Test_error
    Train_error = []
    Test_error = []

    Model.fit(Train.iloc[:,:7], Train[h])
    Train_pred = Model.predict(Train.iloc[:,:7])
    Train_error.append(mean_squared_error(Train[h], Train_pred))
    Train_true = np.array(Train[h]).reshape(-1, 1)
    Train_pred = Train_pred.reshape(-1, 1)

    Test_pred = Model.predict(Test.iloc[:,:7])
    Test_error.append(mean_squared_error(Test[h], Test_pred))
    Test_true = np.array(Test[h]).reshape(-1, 1)
    Test_pred = Test_pred.reshape(-1, 1)

    print(mean_squared_error(Train_pred, Train_true))

    print(Model.score(Test.iloc[:,:7], Test_true))

RunModel('11939')

打印:

0.5131277608869395
-0.02165748764016695