NumPy array concatenation Simple example _python

Source: Internet
Author: User
This article mainly introduces a simple example of numpy array splicing, involving the introduction of the NumPy array, numpy array of properties and other content, with a certain reference value, the need for friends can refer to.

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

· The actual data

· Metadata that describes the data

Most operations are for metadata only, without altering the underlying actual data.

There are a few things you need to know about NumPy arrays:

· The subscript of the NumPy array starts with 0.

· The type of all elements in the same NumPy array must be the same.

NumPy Array Properties

Before the numpy array is described in detail. The basic properties of the next NumPy array are described in detail first. The dimensions of the NumPy array are called rank, the rank of the one-dimensional array is 1, the rank of the two-dimensional array is 2, and so on. In NumPy, each linear array is called an axis (axes), and the rank actually describes the number of axes. For example, a two-dimensional array is equivalent to two one-dimensional arrays, where each element in the first one-dimensional array is a one-dimensional array. So the one-dimensional array is the axis in NumPy (axes), the first axis is the underlying array, and the second axis is the array in the underlying array. and the number of axes--rank, is the dimension of the array.

The more important Ndarray object properties in the NumPy array are:

1.ndarray.ndim: The number of dimensions (that is, the number of array axes) of the array, equal to the rank. The most common are two-dimensional arrays (matrices).

2.ndarray.shape: The dimension of the array. As an integer tuple that represents the size of the array on each dimension. For example, in a two-dimensional array, the number of rows and columns of the array is represented. Ndarray.shape returns a tuple, the length of which is the number of dimensions, that is, the Ndim property.

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

4.ndarray.dtype: An object representing the element type in the array, which can be created or specified using the standard Python type. Dtype You can also use the data types provided by the NumPy described in the previous article.

5.ndarray.itemsize: The byte size of each element in the array. For example, an array with an element type of float64 Itemsiz has a value of 8 (float64 consumes 64 bits, each byte is 8, so 64/8, takes 8 bytes), and an array with an element type of complex32 has an item property of 4 (32/8).

6.ndarray.data: A buffer that contains an actual array element, which is generally not required to use because the element is generally obtained through an index of an array.

Array Stitching Method One

Idea: First, the array is converted into a list, and then the concatenation function append (), extend () and so on are used for splicing, and finally the list is converted into a group.

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, ten, 15]>> > A=np.array (a_list) >>> Aarray ([1, 2, 5, 10, 12, 15])


This method is only applicable to simple one-dimensional array splicing, because the conversion process is time consuming, for a large number of data splicing is generally not recommended.

Array Stitching Method Two

Idea: NumPy provides the numpy.append (Arr,values,axis=none) function. For parameter specification, either an array and a numeric value, or two arrays, not three or more directly append stitching. The APPEND function returns always a one-dimensional array.

Example 2:


>>> A=np.arange (5) >>> Aarray ([0, 1, 2, 3, 4]) >>> np.append (a,10) array ([0, 1, 2, 3, 4, ten]) > >> Aarray ([0, 1, 2, 3, 4]) >>> B=np.array ([11,22,33]) >>> Barray ([One, All,]) >>> Np.appe nd (A, b) array ([0, 1, 2, 3, 4, one,,]) >>> Aarray ([[1, 2, 3],    [4, 5, 6]]) >>> B=np.array ([[7,8,9]  , [10,11,12]]) >>> Barray ([[7, 8, 9],    [Ten, One,]]) >>> np.append (A, b) array ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])


The numpy array does not dynamically resize, and the Numpy.append () function re-allocates the entire array each time and copies the original array into the new array.

Array Stitching Method Three

Idea: NumPy provides numpy.concatenate ((A1,a2,...), axis=0) functions. Ability to stitch multiple arrays at once. Among them a1,a2,... is an array-type parameter

Example 3:


>>> A=np.array ([+]) >>> 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 an array ([1, 2, 3, one, one, one, one, and all]) #对于一维数组拼接, the value of axis does not affect the final result & Gt;>> A=np.array ([[1,2,3],[4,5,6]]) >>> B=np.array ([[11,21,31],[7,8,9]]) >>> np.concatenate (b), axis=0) Array ([[1, 2, 3], [    4, 5, 6], [    7, 8, 9]] >>> np.concatenate (b), axis= 1) #axis =1 represents an array of corresponding rows to be spliced array ([[1, 2, 3, one, 4, 5, 6, 7,    8]])


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

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-time128.2316728446> >> A=np.arange (9999) >>> B=np.arange (9999) >>> Time1=now () >>> c=np.concatenate ((A, B ), axis=0) >>> time2=now () >>> print time2-time120.3934997107


CONCATENATE () is more efficient and suitable for large-scale data stitching

Summarize

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.