2

header output:

array(['Subject_ID', 'tube_label', 'sample_#', 'Relabel', 
      'sample_ID','cortisol_value', 'Group'], dtype='<U14')

body output:

array([['STM002', '170714_STM002_1', 1, 1, 1, 1.98, 'HC'],
       ['STM002', '170714_STM002_2', 2, 2, 2, 2.44, 'HC'],], dtype=object)

testing = np.concatenate((header, body), axis=0)
ValueError                                Traceback (most recent call last) <ipython-input-302-efb002602b4b> in <module>()
      1 # Merge names and the rest of the data in np array
      2 
----> 3 testing = np.concatenate((header, body), axis=0)

ValueError: all the input arrays must have same number of dimensions

Might someone be able to troubleshoot this? I have tried different commands to merge the two (including stack) and am getting the same error. The dimensions (columns) do seem to be the same though.

kmario23
  • 57,311
  • 13
  • 161
  • 150
arkadiy
  • 746
  • 1
  • 10
  • 26
  • How do you want to combine these? They have completely different shapes. Numpy doesn't deal well with jagged arrays – user3483203 Jul 10 '18 at 17:38

3 Answers3

3

You're right in trying to use numpy.concatenate() but you've to promote the first array to 2D before concatenating. Here's a simple example:

In [1]: import numpy as np

In [2]: arr1 = np.array(['Subject_ID', 'tube_label', 'sample_#', 'Relabel', 
   ...:       'sample_ID','cortisol_value', 'Group'], dtype='<U14')
   ...:       

In [3]: arr2 = np.array([['STM002', '170714_STM002_1', 1, 1, 1, 1.98, 'HC'],
   ...:        ['STM002', '170714_STM002_2', 2, 2, 2, 2.44, 'HC'],], dtype=object)
   ...:        

In [4]: arr1.shape
Out[4]: (7,)

In [5]: arr2.shape
Out[5]: (2, 7)

In [8]: concatenated = np.concatenate((arr1[None, :], arr2), axis=0)

In [9]: concatenated.shape
Out[9]: (3, 7)

And the resultant concatenated array would look like:

In [10]: concatenated
Out[10]: 
array([['Subject_ID', 'tube_label', 'sample_#', 'Relabel', 'sample_ID',
        'cortisol_value', 'Group'],
       ['STM002', '170714_STM002_1', 1, 1, 1, 1.98, 'HC'],
       ['STM002', '170714_STM002_2', 2, 2, 2, 2.44, 'HC']], dtype=object)

Explanation:

The reason you were getting the ValueError is because one of the arrays is 1D while the other is 2D. But, numpy.concatenate expects the arrays to be of same dimension in this case. That's why we promoted the array dimension of arr1 using None. But, you can also use numpy.newaxis in place of None

kmario23
  • 57,311
  • 13
  • 161
  • 150
0

You need to align array dimensions first. You are currently trying to combine 1-dimensional and 2-dimensional arrays. After alignment, you can use numpy.vstack.

Note np.array([A]).shape returns (1, 7), while B.shape returns (2, 7). A more efficient alternative would be to use A[None, :].

Also note your array will become of dtype object, as this will accept arbitrary / mixed types.

A = np.array(['Subject_ID', 'tube_label', 'sample_#', 'Relabel', 
              'sample_ID','cortisol_value', 'Group'], dtype='<U14')

B = np.array([['STM002', '170714_STM002_1', 1, 1, 1, 1.98, 'HC'],
              ['STM002', '170714_STM002_2', 2, 2, 2, 2.44, 'HC'],], dtype=object)

res = np.vstack((np.array([A]), B))

print(res)

array([['Subject_ID', 'tube_label', 'sample_#', 'Relabel', 'sample_ID',
        'cortisol_value', 'Group'],
       ['STM002', '170714_STM002_1', 1, 1, 1, 1.98, 'HC'],
       ['STM002', '170714_STM002_2', 2, 2, 2, 2.44, 'HC']], dtype=object)
jpp
  • 159,742
  • 34
  • 281
  • 339
0

Look at numpy.vstack and hstack, as well as the axis argument in np.append. Here it looks like you want vstack (i.e. the output array will have 3 columns, each with the same number of rows). You can also look into numpy.reshape, to change the shape of the input arrays so you can concatenate them.

JoshuaF
  • 1,124
  • 2
  • 9
  • 23
  • Thank you for the info! I tried vstack, and am getting a similar error. However, it looks like both have the same number of colums (7). Does it look like there is an issue in there? thank you so much, – arkadiy Jul 10 '18 at 17:51
  • What are the shapes of each array? You can check with (e.g. for a numpy array `bar`) `bar.shape`. If you want to stack, every dimension should be the same except the one in which you're stacking. – JoshuaF Jul 10 '18 at 17:55
  • Thank you for suggesting the bar.shape- very helpful in reshaping and checking the result. I got it to work. – arkadiy Jul 10 '18 at 18:52