Combine two numpy arrays of different shapes into one array

I have two numpy a and b arrays of length 53 and 82 respectively. I would like to combine them into one array, because I want to use an array of length 53 + 82 = 135 to call it c for plotting.

I tried

c = a+b 

but I get a ValueError: form mismatch: objects cannot be transferred to the same form

Is it possible?

+4
source share
3 answers

You need to use numpy.concatenate instead of adding an array

c = numpy.concatenate((a, b))

Implementation

import numpy as np
a = np.arange(53)
b = np.arange(82)
c = np.concatenate((a, b))

Exit

c.shape
(135, )
+4
source

numpy.concatenate:

In [5]: import numpy as np

In [6]: a = np.arange(5)                                                                         

In [7]: b = np.arange(11)                                                                        

In [8]: np.concatenate((a, b))                                                                   
Out[8]: array([ 0,  1,  2,  3,  4,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

numpy.hstack:

In [9]: np.hstack((a, b))                                                                       
Out[9]: array([ 0,  1,  2,  3,  4,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10]
+3

, float, . ,

  • numpy
  • convert list to array

    part_1 = list(array1)
    part_2 = list(array2)
    combined_list = list(part_1 + part_2)
    combined_array = numpy.asarray(combined_list)
    
-1
source

Source: https://habr.com/ru/post/1523551/


All Articles