我对季节性数据实施MLPRegressor
有一些问题。首先,我从互联网上下载了一个数据集,以用下面已完成的代码进行测试。最终结果应显示图形,并在基于神经网络的预测数据与测试数据之间进行比较。在尝试使用以下代码测试数据集后,它显示错误:
如果数据具有单个特征,则使用array.reshape(-1,1)来重塑数据;如果包含单个样本,则使用array.reshape(1,-1)来重塑数据
如何在此代码中实现重塑方法并显示预测结果?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random
from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
df = pd.read_csv('car_sales_1960.csv', parse_dates=['Month'], index_col='Month')
df.index.freq = 'MS'
x_data = df.iloc[0:len(df), 0]
y_data = random.sample(list(x_data),108)
X_train, X_test, y_train, y_test = train_test_split(x_data.values.reshape(-1,1), np.asarray(y_data).reshape(-1,1), random_state=0)
y_train = np.asarray(y_train).reshape(-1,len(y_train))
x_model = MLPRegressor(hidden_layer_sizes = (100,), activation='logistic', solver='lbfgs',random_state = 0).fit(x_train,y_train)
x_predicted = x_model.predict(start=x_test.index[0], end=x_test.index[-1])
plt.plot(x_train.index, x_train, label='Train')
plt.plot(x_test.index, x_test, label='Test')
plt.plot(x_predicted.index, x_predicted, label='Neural Network')
plt.legend(loc='best')
plt.title('Neural Network Prediction')
plt.show()