An in-depth understanding of numpy Concise Tutorial---array 1

Source: Internet
Author: User
This article mainly introduces the deep understanding NumPy Concise tutorial (two, array 1), NumPy array is a multidimensional array object, with a certain reference value, interested in small partners can refer to.

My job now is to introduce numpy into the Pyston (a Python compiler/interpreter implemented by Dropbox). In the course of work, I deeply contacted the NumPy source code, understand its implementation and submitted a PR fix numpy bug. In the process of dealing with NumPy source and numpy developers, I found that today's Chinese numpy tutorials are mostly translated or referenced in English, leading to a number of omissions. For example, the broadcast function in the NumPy array, almost all Chinese documents are translated as "broadcast". And one of the developers of NumPy, replied to "Broadcast is a compound--native 中文版 speakers can see that it's" broad "+" cast "=" cast (scatter, Distribute) broadly, I guess "cast (scatter, distribute) broadly" probably is closer to the meaning (meaning in numpy) ". In view of this, I intend to launch a project to write a series of tutorials for my knowledge of numpy use and the source level.

NumPy Array

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:

    • 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).

    • 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.

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

    • 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.

    • 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).

    • 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.

Create an array

First, we introduce the creation of arrays. There are many ways to create an array. If you can use the array function to create arrays from regular Python lists and tuples. The type of the array created is inferred from the element type in the original sequence.

>>> from numpy import * >>> a = array ([2,3,4]) >>> a   array ([2, 3, 4]) >>> A.dtype   Dtype (' int32 ') >>> B = Array ([1.2, 3.5, 5.1]) >>> b.dtype   dtype (' float64 ')

When you create using the array function, the parameter must be a list enclosed in square brackets, and you cannot call array with multiple numeric values as arguments.

>>> A = Array (1,2,3,4)  # error >>> a = array ([1,2,3,4]) # correct

You can use a double sequence to represent a two-dimensional array, a triple sequence that represents a three-dimensional array, and so on.

>>> B = Array ([(1.5,2,3), (4,5,6)]) >>> b   Array ([[[1.5, 2, 3.],   [4., 5., 6.]])

You can explicitly specify the type of elements in an array at creation time

>>> C = Array ([[], [3,4]], Dtype=complex) >>> C   Array ([[[1.+0.J, 2.+0.J],  [3.+0.J, 4. +0.J]])

Typically, the elements of an array are unknown at first, and the size of the array is known. As a result, NumPy provides some functions for creating arrays using placeholders. These functions help to satisfy the need for an array extension, while reducing the high computational overhead.

Using the function zeros can create an array that is all 0, using the function ones to create an array of all 1, the function empty creates an array of random content and relies on the memory state. The array type (DTYPE) created by default is float64.

You can naat d.dtype.itemsize to see the number of bytes occupied by the elements in the array.

>>> d = zeros ((3,4)) >>> d.dtype dtype (' float64 ') >>> d array ([[0., 0., 0., 0.],  [0., 0., 0., 0.],  [0., 0., 0., 0.]] >>> D.dtype.itemsize 8

You can also make your own type of elements in the array

>>> ones ((2,3,4), dtype=int16) #手动指定数组中元素类型    Array ([[[[[1], 1, 1, 1],    [1, 1, 1, 1],    [1, 1, 1, 1 ]],    [[1, 1, 1, 1],    [1, 1, 1, 1],    [1, 1, 1, 1]], dtype=int16) >>> empty ((2,3)) arr Ay ([[2.65565858e-316,  0.00000000e+000,  0.00000000e+000],    [0.00000000e+000,  0.00000000e+000,  0.00000000e+000])

NumPy provides a function similar to Arange to return an array in the form of a sequence:

>>> Arange (5),   array ([10, 15, 20, 25])

Starting at 10, the arithmetic progression with a difference of 5. The function accepts not only integers, but also floating-point parameters:

>>> Arange (0,2,0.5)   Array ([0., 0.5, 1., 1.5])

When Arange uses a floating-point parameter, the number of elements obtained is usually unpredictable because of the limited precision of the floating-point numbers. Therefore, it is better to use the function Linspace to receive the number of elements we want instead of using range to specify the step size. The Linespace usage is described in detail in the General functions section below.

>>> numpy.linspace ( -1, 0, 5)     Array ([-1.,-0.75,-0.5,-0.25, 0.])

The elements in the array are accessed by subscript, and you can access a single element in the array by enclosing a subscript in square brackets, or you can access multiple elements in the array as slices. About slicing access is described in the sections section.

Knowledge Points: Data types in NumPy

For scientific computing, the integral, floating-point, and complex types in Python are far from sufficient, so many data types are added to NumPy. As follows:

Basic data types in NumPy


basic data types in NumPy
Name Describe
bool Boolean type stored with one byte (True or FALSE)
Inti An integer whose size is determined by the platform on which it is located (typically int32 or Int64)
int8 One byte size, 128 to 127
Int16 Integers, 32768 to 32767
Int32 Integers,-2 * * 31 to 2 * * 32-1
Int64 Integers,-2 * * 63 to 2 * * 63-1
Uint8 unsigned integers, 0 to 255
UInt16 unsigned integers, 0 to 65535
UInt32 Unsigned integer, 0 to 2 * * 32-1
UInt64 Unsigned integer, 0 to 2 * * 64-1
Float16 Semi-precision floating-point number: 16-bit, sign 1-bit, exponent 5-bit, precision 10-bit
Float32 Single-precision floating-point number: 32-bit, positive and negative 1-bit, exponential 8-bit, precision 23-bit
Float64 or float Double-precision floating-point number: 64-bit, positive and negative 1-bit, exponential 11-bit, precision 52-bit
Complex64 Complex numbers, with two 32-bit floating-point numbers representing both real and imaginary parts
complex128 or complex Complex numbers, with two 64-bit floating-point numbers representing both real and imaginary parts

The NumPy type is converted as follows:

>>> float64 (42.0) >>> int8 (42.0)----------   True >>> bool ( 42.0)   True >>> Float (True)   1.0

The parameters of many functions can be specified in the parameter type, of course, this type parameter is optional. As follows:

>>> Arange (7, dtype=uint16)   Array ([0, 1, 2, 3, 4, 5, 6], dtype=uint16)

Output array

When an array is output, the NumPy is displayed in a specific layout in a similar nested list:

    • The first row is output from left to right

    • Each row is sequentially top-down output

    • Each slice is separated from the next by a blank line

    • One-dimensional arrays are printed in rows, two-dimensional numbers constitute matrices, and three-dimensional numbers constitute matrix lists.

>>> A = Arange (6)             # 1d array >>> print a   [0 1 2 3 4 5] >>> B = arange (a). Reshape (4 , 3)      # 2d array >>> print b   [[0 1 2]   [3 4 5]   [6 7 8]   [9]] >>> C = arange Reshape (2,3,4)     # 3d Array >>> print C [[[   0 1 2 3] [4 5   6 7] [   8 9 ten]]   [[12 1 3 [+] [   20 21 22 23]]

Reshape will describe in the next article

If an array is too long, NumPy automatically omits the middle part and prints only the data at both ends:

>>> Print Arange (10000) [  0  1  2 ..., 9997 9998 9999] >>> print Arange (10000). Resha  PE (100,100) [[  0  1  2 ...,  98  99] [100 101 102 ..., 197 198 199] [200 201 202 ..., 297 298 299] ..., [9700 9701 9702 ..., 9797 9798 9799] [9800 9801 9802 ..., 9897 9898 9899] [9900 99 01 9902 ..., 9997 9998 9999]]

You can disable this behavior for NumPy by setting the PrintOptions parameter and force the entire array to be printed.

Set_printoptions (threshold= ' nan ')

This way, all elements of the array are displayed when output.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

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.