当前位置: 首页 > 知识库问答 >
问题:

Numpy:只有整数标量数组才能转换为标量索引

谯德元
2023-03-14

下面是代码:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import pandas_datareader as web
import datetime as dt 

from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM

# Load data
company = 'FB'

start = dt.datetime(2012,1,1)
end = dt.datetime(2020,1,1)

data = web.DataReader(company, 'yahoo', start, end)

#Prepare Data
scaler = MinMaxScaler(feature_range=(0,1))
scaled_date = scaler.fit_transform(data['Close'].values.reshape(-1,1))

prediction_days = 60

x_train = []
y_train = []

for x in range(prediction_days, len(scaled_date)):
    x_train.append(scaled_date[x-prediction_days:x, 0])
    y_train.append(scaled_date[x, 0])

x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0], x_train[1], 1)) #the error's here

我得到以下错误:

回溯(最后一次调用):文件“/Users/evgenypavlov/Documents/ml_tutorial_1/main.py”,第34行,在x_train=np中。重塑(x_列,(x_列.shape[0],x_列[1],1])文件“

据我所知,我已经把它转换成np了。数组,那么什么会导致此问题以及如何解决它?

共有1个答案

王俊楚
2023-03-14

元组的第二个元素中缺少形状

x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
 类似资料: