numpy array concatenation

Reprinted from: https://blog.csdn.net/zyl1042635242/article/details/43162031

Array concatenation method one

First convert the array into a list, then use the list splicing functions append(), extend(), etc. to splicing, and finally convert the list into an array.

Example 1:

>>> import numpy as np
>>> a=np.array([1,2,5])
>>> b=np.array([10,12,15])
>>> a_list=list(a)
>>> b_list=list(b)

>>> a_list.extend(b_list)

>>> a_list
[1, 2, 5, 10, 12, 15]
>>> a=np.array(a_list)
>>> a
array([ 1,  2,  5, 10, 12, 15])

This method is only suitable for simple one-dimensional array splicing. Since the conversion process is time-consuming, it is generally not recommended for splicing large amounts of data.

 

Array concatenation method 2

Idea: numpy provides the numpy.append(arr, values, axis=None) function. For parameter specification, either one array and one value; or two arrays, three or more arrays cannot be directly appended and spliced. The append function returns always a one-dimensional array.

Example 2:

>>> a=np.arange(5)
>>> a
array([0, 1, 2, 3, 4])
>>> np.append(a,10)
array([ 0,  1,  2,  3,  4, 10])
>>> a
array([0, 1, 2, 3, 4])

 

>>> b=np.array([11,22,33])
>>> b
array([11, 22, 33])
>>> np.append(a,b)
array([ 0,  1,  2,  3,  4, 11, 22, 33])

 

>>> a
array([[1, 2, 3],
       [4, 5, 6]])
>>> b=np.array([[7,8,9],[10,11,12]])
>>> b
array([[ 7,  8,  9],
       [10, 11, 12]])
>>> np.append(a,b)
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

Numpy arrays do not have the ability to dynamically change the size. The numpy.append() function will reallocate the entire array every time and copy the original array to the new array.

 

Array splicing method three

Idea: numpy provides the numpy.concatenate((a1,a2,...), axis=0) function. Able to complete the concatenation of multiple arrays at one time. where a1,a2,... are parameters of array type

Example 3:

>>> a=np.array([1,2,3])
>>> b=np.array([11,22,33])
>>> c=np.array([44,55,66] )
>>> np.concatenate((a,b,c),axis=0) # By default, axis=0 can not write
array([ 1, 2, 3, 11, 22, 33, 44, 55, 66]) #For one-dimensional array concatenation, the value of axis does not affect the final result

 

>>> a=np.array([[1,2,3],[4,5,6]])
>>> b=np.array([[11,21,31],[7,8,9]])
>>> np.concatenate((a,b),axis=0)
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [11, 21, 31],
       [ 7,  8,  9]])

>>> np.concatenate((a,b),axis=1) #axis=1 means that the array of the corresponding row is concatenated
array([[ 1, 2, 3, 11, 21, 31],
       [ 4, 5, 6, 7, 8, 9]])

 

Compare the running times of the two functions numpy.append() and numpy.concatenate()

Example 4:

>>> from time import clock as now
>>> a=np.arange(9999)
>>> b=np.arange(9999)
>>> time1=now()
>>> c=np.append(a,b)
>>> time2=now()
>>> print time2-time1
28.2316728446
>>> a=np.arange(9999)
>>> b=np.arange(9999)
>>> time1=now()
>>> c=np.concatenate((a,b),axis=0)
>>> time2=now()
>>> print time2-time1
20.3934997107

It can be seen that concatenate() is more efficient and suitable for large-scale data splicing

  

 

Strange, why am I different from his

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325269625&siteId=291194637