【问题标题】:how do I solve this error in python (code in Jupyter notebook)如何在 python 中解决这个错误(Jupyter notebook 中的代码)
【发布时间】:2021-05-20 16:22:39
【问题描述】:

我在三月份创建了这个程序,当时它运行良好,但现在出现错误,我不知道为什么。

这是当前不工作的代码(我在 Jupiter 笔记本上编码)

import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime
import seaborn
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.linear_model import LinearRegression
pd.options.mode.chained_assignment = None  # default='warn'


df = yf.download("spy")
df.to_csv('spy.csv')
df = df[['Adj Close']]
plt.plot(df)

df['Adj Close'].plot(figsize=(15,6), color = 'g')
plt.legend(loc='upper left')
plt.show()


forecast = 70
df['Prediction'] = df[['Adj Close']].shift(-forecast)
X = np.array(df.drop(['Prediction'], 1))
X = preprocessing.scale(X)            
X_forecast = X[-forecast:]
X = X[:-forecast]
y = np.array(df['Prediction'])
y = y[:-forecast]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
clf = LinearRegression()
clf.fit(X_train, y_train)
confidence = clf.score(X_test, y_test)
confidence
forecast_predicted = clf.predict(X_forecast)
print(forecast_predicted)

plt.plot(X, y)

dates = pd.date_range(start="2021-05-21", end= "2021-06-19")
plt.plot(dates, forecast_predicted, color='b')
df['Adj Close'].plot(color='g')
plt.xlim(xmin = datetime.date(2020,5,1))
plt.xlim(xmax = datetime.date(2021,7,1))

我知道错误出现在代码的最后一部分。 这是代码的最后一部分在 3 月 15 日工作时的样子。

dates = pd.date_range(start="2021-03-16", end= "2021-04-14")
plt.plot(dates, forecast_predicted, color='b')
df['Adj Close'].plot(color='g')
plt.xlim(xmin = datetime.date(2020,3,1))
plt.xlim(xmax = datetime.date(2021,5,1))

【问题讨论】:

    标签: python codex codexl


    【解决方案1】:

    在错误输出中有解释:你的 x 和 y 第一个维度不匹配。问题是您正在预测 70 天 (forecast=70) 并尝试将其绘制到 30 天期间。

    您可以尝试更改预测日期:

    forecast=30
    

    或时间段匹配 70 天,如下所示:

    dates = pd.date_range(start="2021-05-21", end= "2021-07-29")
    

    【讨论】:

      猜你喜欢
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 2022-01-10
      • 1970-01-01
      • 2020-06-20
      • 2019-02-27
      • 2022-07-12
      相关资源
      最近更新 更多