import pandas as pd
import numpy as np
from sklearn.neural_network import MLPRegressor
import matplotlib.pyplot as plt
data = pd.read_excel("idx.xlsx")
data
date = data.iloc[2:242,1]
train = data.iloc[2:242,2:]
train
# 构建数据集
x_train = []
y_train = []
for i in range(220):
x_train.append(np.array(train.values[i:i+20].tolist()).reshape(1,-1)[0])
y_train.append(np.array(train.values[i+20,-1]))
clf = MLPRegressor(random_state=420)
clf.fit(x_train[:180], y_train[:180])
p = clf.predict(x_train[180:190])
score = clf.score(X = x_train[180:190], y = y_train[180:190])
score
output:-0.159274
画个图看看:
然而,换线性回归:
t = pd.DataFrame(x_train[:180])
t_p = pd.DataFrame(y_train[:180])
model = LinearRegression().fit(t,t_p)
pp = model.predict(x_train[180:190])
sco = model.score(x_train[180:190],y_train[180:190])
sco
output:0.402906
对比:都是裸跑,没有调参,很明显,线性回归拟合效果比神经网络好。
缺少业务知识,构建数据集可能不科学,也算模型精度的原因之一。
thank you!