添加输入到numpy 3d数组

时间:2018-03-10 17:44:05

标签: python numpy machine-learning

我有一个pandas DataFrame,我会像这样转换为numpy数组

  training_set = dataset_train.iloc[:, 1:2].values

  X_train = []
  for i in range(60, 1258):
      X_train.append(training_set[i-60:i, 0])
      y_train.append(training_set[i, 0])

  X1_train = np.array(X1_train)
  

X1_train.shape =(1198,60)

创建3d数组后

X1_train = np.reshape(X1_train, (X1_train.shape[0], X1_train.shape[1] , 1))
  

X2_train.shape =(1198,0)

现在尝试在我的数组中添加新输入x2_train.shape[0], x2_train.shape[1]并且我被阻止

我尝试了几件这样的事情

X1_train = np.reshape(X1_train, ([X1_train.shape[0], X1_train.shape[1], X2_train.shape[0], X2_train.shape[1]] , 1))

X1_train = np.reshape(X1_train, (X2_train.shape[0], X2_train.shape[1] , 1))

运行我的代码时出现此错误

File "/anaconda3/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 257, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)

  File "/anaconda3/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 62, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)

  File "/anaconda3/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 42, in _wrapit
    result = getattr(asarray(obj), method)(*args, **kwds)

TypeError: 'list' object cannot be interpreted as an integer

numpy doc不是很清楚

2 个答案:

答案 0 :(得分:1)

你的反应很好,但你不会忘记重塑阵列

你可以删除这个

  X1_train = np.reshape(X1_train, (X1_train.shape[0], X1_train.shape[1] , 1))

 X2_train = np.reshape(X2_train, (X2_train.shape[0], X2_train.shape[1] , 1))


new_train  = np.dstack((X1_train, X2_train))

只需像这样编写代码

new_train  = np.dstack((X1_train, X2_train))

答案 1 :(得分:0)

答案是替换

X1_train = np.reshape(X1_train, (X1_train.shape[0], X1_train.shape[1] , 1))

并替换为

X1_train = np.reshape(X1_train, (X1_train.shape[0], X1_train.shape[1] , 1))

 X2_train = np.reshape(X2_train, (X2_train.shape[0], X2_train.shape[1] , 1))


new_train  = np.dstack((X1_train, X2_train))

这是一个二维到3d数组

  

new_train.shape =(1198,60,2)