【问题标题】:numpy array:tuple index out of rangenumpy数组:元组索引超出范围
【发布时间】:2019-11-20 09:22:41
【问题描述】:
#converting the lists into numpy arrays
x_train=np.array(x_train)
x_test=np.array(x_test)
y_train=np.array(y_train)
y_test=np.array(y_test)
x_train.shape,x_test.shape,y_train.shape,y_test.shape

问题从下面的代码开始

整形为 2d 以保存为 csv 格式

x_train_2d=np.reshape(x_train,(x_train.shape[0],x_train.shape[1]*x_train.shape[2]))
x_test_2d=np.reshape(x_test,(x_test.shape[0],x_test.shape[1]*x_test.shape[2]))
x_train_2d.shape,x_test_2d.shape

【问题讨论】:

  • 数组的原始形状是什么?
  • 也发布错误输出
  • numpy数组_train.shape,x_test.shape,y_train.shape,y_test.shape的形状是((7895, 40), (837, 40), (7895,), (837, ))
  • 形成的错误是:IndexError Traceback (最近一次调用最后一次) in () ----> 1 x_train_2d=np.reshape(x_train, (x_train.shape[0],x_train.shape[1]*x_train.shape[2])) 2 x_test_2d=np.reshape(x_test,(x_test.shape[0],x_test.shape[1]*x_test.shape [2])) 3 x_train_2d.shape,x_test_2d.shape IndexError: tuple index out of range
  • 输出形状应该是什么。

标签: python-3.x export-to-csv google-colaboratory


【解决方案1】:

您可以做的可能是以下。

创建一个包含 40 values from second dimension of X, and Y value 列的 csv 文件 行将首先是训练数据,然后是测试数据

data_train = np.c_[x_train, y_train]
data_test = np.c_[x_test, y_test]
data = np.r_[data_train, data_test]
print(data.shape)
np.savetxt('filename.csv', data, delimiter=',')

输出数据形状为

(8732, 41)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2021-03-02
    • 2018-11-16
    • 2016-04-04
    • 2020-06-08
    相关资源
    最近更新 更多