1

I have written this code to append two numpy arrays:

td_XN = searchNegative(X,y,10)
td_XP = searchPosotive(X,y,10)
print(np.array(td_XN).shape, np.array(td_XP).shape)
print(type(td_XN), type(td_XP))
td_X = np.concatenate(td_XP, td_XN)
td_y = [1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0]
print(td_X.shape, len(td_y))

However, it produces this error:

TypeError: only length-1 arrays can be converted to Python scalars

On this line:

td_X = np.concatenate(td_XP, td_XN)
scutnex
  • 813
  • 1
  • 9
  • 19

1 Answers1

1

If you want to concatenate side by side (that is, create an array of 10 -by- 2544*2): you can do

td_X = np.concatenate([td_XP, td_XN],axis=1)

For example

td_X = np.concatenate([[[1,2,3,7],[4,5,6,8]],[[1,2,3],[4,5,6]]],axis=1)

gives

array([[1, 2, 3, 7, 1, 2, 3],
       [4, 5, 6, 8, 4, 5, 6]])

On the other hand, if you want to add td_XN below td_XP you can do

td_X = np.concatenate([td_XP, td_XN],axis=0)

For example,

td_X = np.concatenate([[[1,2,3],[4,5,6]],[[1,2,7],[4,5,8]]],axis=0)

gives

array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 7],
       [4, 5, 8]])
Miriam Farber
  • 18,986
  • 14
  • 61
  • 76