Simple example of numpy array concatenation

  • 2020-06-19 10:34:38
  • OfStack

The NumPy array is a multidimensional array object called ndarray. It consists of two parts:

· Actual data

· Metadata that describes the data

Most operations are only for metadata and do not change the underlying actual data.

A few things to know about NumPy arrays:

· The index of the NumPy array starts at 0.

· All elements in the same NumPy array must have the same type.

NumPy array properties

Before I go into more detail about the NumPy array. Let's start with the basic properties of the NumPy array. The dimension of an NumPy array is called the rank (rank), the rank of a 1-dimensional array is 1, the rank of a 2-dimensional array is 2, and so on. In NumPy, each linear array is called an axis (axes), and the rank is actually the number of axes described. For example, a 2-dimensional array is equivalent to two 1-dimensional arrays, where each element in the first 1-dimensional array is a 1-dimensional array. So a 1-dimensional array is the axis in NumPy (axes), the first axis is the bottom array, and the second axis is the array in the bottom array. And the number of axes, the rank, is the dimension of the array.

The more important ndarray object attributes in the NumPy array are:

ndarray.ndim: The dimension of the array (that is, the number of axis of the array), equal to the rank. The most common is a 2-dimensional array (matrix).

ndarray. shape: The dimension of an array. Is 1 integer tuple representing the size of the array on each dimension. For example, in a 2-dimensional array, the "number of rows" and "number of columns" represent the array. ndarray.shape returns 1 tuple whose length is the number of dimensions, the ndim attribute.

3. ndarray. size: The total number of array elements, equal to the product of tuple elements in the shape attribute.

ndarray. dtype: Represents an object of the element type in an array, which can be created or specified using the standard Python type. You can also use the data types provided by NumPy as described in the previous article.

ndarray. itemsize: The size in bytes of each element in the array. For example, an array of itemsiz of element type float64 has an attribute value of 8(float64 takes up 64 bits, each byte length is 8, so 64/8 takes up 8 bytes), or an array of item of element type complex32 has an attribute value of 4 (32/8).

6. ndarray. data: A buffer containing the actual array elements. Since elements are usually fetched by the index of an array, this attribute is usually not needed.

Array concatenation method 1

Idea: first turn the array into a list, then use the splicing functions of the list append(), extend() for splicing processing, and finally turn 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 1 - d array mosaics.

Array concatenation method 2

numpy provides numpy.append (arr,values,axis=None) function. For parameters, either 1 array and 1 value; Either two arrays, can not be 3 or more arrays directly append mosaics. The append function always returns a 1-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's array does not dynamically resize; each time the numpy.append () function reallocates the entire array and copies the original array into the new array.

Array concatenation method 3

numpy provides numpy.concatenate((a1,a2...) , axis = 0) function. Able to concatenate multiple arrays at one time. The a1, a2,... Is an argument of type array

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 1 Dimensional array assembly, axis The value of 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 Represents the array of corresponding rows to be spliced 
array([[ 1, 2, 3, 11, 21, 31],
    [ 4, 5, 6, 7, 8, 9]])

Compare the running time of 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 Mosaic

conclusion

That's it for this article's simple example of numpy array concatenation. Those who are interested can continue to see this site:

Some sorting methods of numpy array are discussed

Python programs an example of adding a column method to the numpy matrix

Python numpy generates matrix, series matrix code sharing

If there is any deficiency, please let me know. Thank you for your support!


Related articles: