我目前正在尝试遵循教程,因为我刚刚开始学习机器学习。 我正在尝试预测股票价格。这是我的代码:
`
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as web
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Dropout
from tensorflow.python.keras.layers.recurrent import LSTM
company = 'TSLA'
start='2012-01-01'
end='2024-03-01'
data = web.download(company, start=start, end=end)
scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(data['Close'].values.reshape(-1,1))
prediction_days = 60
x_train = []
y_train = []
for x in range(prediction_days, len(scaled_data)):
x_train.append(scaled_data[x-prediction_days:x, 0])
y_train.append(scaled_data[x, 0])
model = Sequential()
model.add(LSTM(units = 50, return_sequences=True, input_shape=(x_train.shape[1], 1)))
model.add(Dropout(0.2))
model.add(LSTM(units = 50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units = 50))
model.add(Dropout(0.2))
`
我例外地没有输入任何内容,但是我收到了此错误: 但它说是一个错误, '
Traceback (most recent call last):
File
"c:\Users\User1\OneDrive\Documents\Desktop\python\projects\machine\stock_price_predictor.py",
line 32, in <module>
model.add(LSTM(units = 50, return_sequences=True, input_shape=(x_train.shape[1], 1)))
^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'shape'
'
你们有人知道如何解决这个问题吗?我尝试将其转换为 np.array 但没有任何效果。
错误的原因是正在尝试在 Python 列表上使用
shape
属性,而
shape
属性是 NumPy 数组的属性。需要将
x_train
列表转换为 NumPy 数组才能使用
shape
属性。
将以下代码:
x_train = []
y_train = []
for x in range(prediction_days, len(scaled_data)):
x_train.append(scaled_data[x-prediction_days:x, 0])
y_train.append(scaled_data[x, 0])
替换为:
x_train = np.array([scaled_data[x-prediction_days:x, 0] for x in range(prediction_days, len(scaled_data))])
y_train = np.array(scaled_data[prediction_days:, 0])
这段代码使用列表推导式创建
x_train
和
y_train
,并将它们直接转换为 NumPy 数组。这样就可以在
input_shape
参数中使用
x_train.shape
了。
以下是完整的代码:
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as web
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Dropout
from tensorflow.python.keras.layers.recurrent import LSTM
company = 'TSLA'
start='2012-01-01'
end='2024-03-01'
data = web.download(company, start=start, end=end)
scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(data['Close'].values.reshape(-1,1))
prediction_days = 60
# 使用列表推导式创建 NumPy 数组
x_train = np.array([scaled_data[x-prediction_days:x, 0] for x in range(prediction_days, len(scaled_data))])
y_train = np.array(scaled_data[prediction_days:, 0])
model = Sequential()
model.add(LSTM(units = 50, return_sequences=True, input_shape=(x_train.shape[1], 1)))
model.add(Dropout(0.2))
model.add(LSTM(units = 50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units = 50))
model.add(Dropout(0.2))
# ... 继续的代码 ...
标签:python,tensorflow,machine-learning
From: 78796342