【问题标题】:IndexError: tuple index out of range when running python 3.9.1IndexError:运行python 3.9.1时元组索引超出范围
【发布时间】:2023-04-05 05:16:02
【问题描述】:

运行我的代码时出错

dataset_total = pd.concat((dataset['Open'], dataset_test['Open']), axis = 0)
inputs = dataset_total[len(dataset_total) - len(dataset_test) - 60:].values
inputs = inputs.reshape(-1,1)
inputs = sc.transform(inputs)
X_test = []
for i in range(60, 80):
   X_test.append(inputs[i-60:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
predicted_forex_price = regressor.predict(X_test)
predicted_forex_price = sc.inverse_transform(predicted_forex_price)

结果是:

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:8:
VisibleDeprecationWarning:从不规则的嵌套创建 ndarray
序列(这是列表或元组或 ndarrays 的列表或元组
不推荐使用不同长度或形状的)。如果你打算这样做
这个,你必须在创建 ndarray 时指定 'dtype=object'

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-110-0e4e370b525c> in <module>()
      7 X_test.append(inputs[i-60:i, 0])
      8 X_test = np.array(X_test)
----> 9 X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
     10 predicted_forex_price = regressor.predict(X_test)
     11 predicted_forex_price = sc.inverse_transform(predicted_forex_price)

IndexError: tuple index out of range

【问题讨论】:

  • 您可以打印 X_test 并将其添加到上面的问题中吗?
  • 我该怎么做?

标签:
python
arrays
numpy
reshape