简体   繁体   English

如何解决 python 中的此错误(Jupyter 笔记本中的代码)

[英]how do I solve this error in python (code in Jupyter notebook)

I created this program in march and it worked fine then, but now it has an error and I can't figure out why.我在三月份创建了这个程序,当时它运行良好,但现在它有一个错误,我不知道为什么。 它显示的错误它工作时的样子

here is the current non working code (I coded this on Jupiter notebook)这是当前的非工作代码(我在 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))

I know the error is in the last part of the code.我知道错误在代码的最后一部分。 here is how the last part of the code looked when it was working on march 15.这是代码的最后一部分在 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))

It is explained in the error output: yours x and y first dimensions don't match.错误 output 中对此进行了解释:您的 x 和 y 第一个尺寸不匹配。 The problem is you are forecasting for 70 days (forecast=70) and trying to plot that onto 30 days period.问题是您预测 70 天(预测 = 70)并尝试将 plot 预测为 30 天。

You can either try changing forecast days:您可以尝试更改预测日期:

forecast=30

Or the time period so it matches 70 days, something like this:或时间段匹配 70 天,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Jupyter Notebook 上编写代码时如何解决 Python3 中的 FileNotFound 错误? - How do I solve the FileNotFound error in Python3 while writing code on Jupyter Notebook? 如何解决Jupyter Notebook中的错误? - How can I solve error in Jupyter notebook? 如何在 Jupyter 笔记本中运行 Python 异步代码? - How do I run Python asyncio code in a Jupyter notebook? 如何解决安装 jupyter notebook 的错误? - How to solve error installing jupyter notebook? 我如何使用 jupyter 笔记本的 python 代码从网站下载 csv 文件 - how do i download the csv file from a website using python code for my jupyter notebook 如何在Jupyter Notebook中验证python代码? - How can I validate python code in a Jupyter Notebook? 如何帮助 Python 找到 Jupyter 命令“jupyter-nbconvert”,将 Jupyter Notebook 导出为 HTML? - How do I help Python find Jupyter command 'jupyter-nbconvert', to export Jupyter Notebook to HTML? 如何在我的jupyter笔记本中添加python3内核? - How do I add a python3 kernel to my jupyter notebook? 如何将输出集中在 Python Jupyter 笔记本上? - How do I center the outputs on a Python Jupyter notebook? 如何使用 jupyter notebook 修复 python 中的“语法错误”? - how can i fix the 'syntax error' in python using jupyter notebook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM