An in-depth understanding of NumPy concise Tutorials---array 1_python

Source: Internet
Author: User
Tags arrays in python

My job now is to introduce numpy into the Pyston (a python compiler/interpreter for Dropbox implementations). In the process of working, 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 most of today's Chinese numpy tutorials are translations or references to English documents, thus leading to a lot of omissions. For example, the broadcast function in the NumPy array, almost all Chinese documents are translated as "broadcast." And one of NumPy's developers, replying 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) ". With that in mind, I'm going to start a project to write a series of tutorials on my knowledge of numpy usage and the source level.

NumPy Array

The NumPy array is a multidimensional array object called Ndarray. It is made up of two parts:

    • The actual data
    • The metadata that describes the data

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

There are several things you need to know about the NumPy array:

    • The subscript of the NumPy array starts at 0.
    • All elements in the same NumPy array must be of the same type.

NumPy Array Properties

Before you describe the NumPy array in more detail. First, the basic properties of the NumPy array are described in detail. The dimensions of the NumPy array are called rank (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 is actually the number that describes the axis. For example, a two-dimensional array is equivalent to two one-dimensional arrays, in which each element in the first one-dimensional array is a one-dimensional array. So the one-dimensional array is the axis in the NumPy (axes), the first axis is equivalent to the underlying array, and the second axis is the array in the underlying array. The number of axes-the rank-is the number of dimensions of the array.

NumPy arrays are more important Ndarray object properties are:

    • Ndarray.ndim: The number of dimensions of the array (that is, the number of array axes), equal to the rank. The most common is a two-dimensional array (matrix).
    • Ndarray.shape: The dimensions of the array. is an integer tuple that represents the size of an array on each dimension. In a two-dimensional array, for example, represents the number of rows and columns of an array. Ndarray.shape returns a tuple, the length of which is the number of dimensions, that is, the Ndim attribute.
    • Ndarray.size: The total number of array elements, equal to the product of the tuple elements in the Shape property.
    • Ndarray.dtype: An object that represents the element type in an array that can be created or specified using a 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 has a Itemsiz property value of 8 (Float64 occupies 64 bits, each byte length is 8, so 64/8, occupies 8 bytes), and the array item property of an element type of complex32 is 4 (32/8).
    • Ndarray.data: A buffer containing an actual array element, which is generally not required because it is typically fetched from an array's index.

Create an array

Let's start by creating an array. There are many ways to create arrays. You can use the array function to create arrays from regular Python lists and tuples. The array type created is derived 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 
  

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

>>> A = Array (1,2,3,4)  # Error 

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.],   

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

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

Typically, the elements of an array are unknown at first, and the size of the array is known. Therefore, NumPy provides a number of functions that create arrays using placeholders. These functions help meet the need for array expansion, while reducing the overhead of expensive operations.

Using function zeros to create an array of all 0, use the function ones to create an array of 1, function empty create a random and dependent array of memory states. The type of array created by default (Dtype) is float64.

You can yo Naat d.dtype.itemsize to see the number of bytes in the array that the element occupies.

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

You can also set the type of elements in an array yourself

>>> 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    ]]], dtype=int16 
>>> Empty ((2,3)) 
array ([[2.65565858e-316,  0.00000000e+000,  0.00000000e+000],    [0.00000000e+000 ,  0.00000000e+000,                       

NumPy provides an array of functions similar to arange that returns a sequence of numbers:

>>> Arange (10, 30, 5) 
  

Starting with 10, the difference is arithmetic progression of 5. This function accepts not only integers, but also floating-point arguments:

>>> Arange (0,2,0.5) 
  

When Arange uses floating-point parameters, the number of elements that are obtained is often unpredictable because of the limited precision of floating-point numbers. Therefore, it is best 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.

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

The elements in an array are accessed by subscript, which can be surrounded by brackets to access a single element in the array, or multiple elements in the array can be accessed as slices. For slicing access, you will see a section in slicing.

Knowledge Points: Data types in NumPy

For scientific computing, the integer, floating-point, and complex types that are brought in Python are far from enough, so many data types are added to the 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 (typically int32 or Int64) that determines its size from the platform in which it is located
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 integers, 0 to 2 * * 32-1
UInt64 unsigned integers, 0 to 2 * * 64-1
Float16 Semi-precision floating point number: 16 digits, positive and negative 1 digits, index 5 digits, precision 10 digits
Float32 Single-precision floating point number: 32 digits, positive and negative 1 digits, index 8 digits, precision 23 digits
Float64 or float Double-precision floating point number: 64 digits, positive and negative 1 digits, index 11 digits, precision 52 digits
Complex64 Plural, representing real and imaginary parts with two 32-bit floating-point numbers respectively
complex128 or complex Plural, representing real and imaginary parts with two 64-bit floating-point numbers respectively

NumPy type conversions are as follows:

>>> float64 (42.0) 
>>> int8 (42.0)- 
>>> bool (a) 
> >> bool (42.0) 
  True 
>>> float (true) 
  

You can specify the type of the parameter in many parameters of a function, of course, this type parameter is optional. As follows:

>>> Arange (7, dtype=uint16) 
  

Output array

When outputting an array, the NumPy is displayed in a particular layout in a similar way to a nested list:

    • The first line is output from left to right
    • Each row is output from top to bottom
    • Each slice is separated from the next by a blank line
    • The one-dimensional array is printed in rows, the two-dimensional number is composed of matrices, and the three-dimensional numbers form a matrix list.
>>> A = Arange (6)             # 1d array 
>>> print a 
  [0 1 2 3 4 5] 
>>> b = Arange (12). 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]] [[ 
  16 17 18 19]       
  

Reshape will introduce in the next article

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

>>> Print Arange (10000) 
[  0  1  2 ..., 9997 9998 9999] 
>>> print Arange  (10000). Reshape (100,100) 
[[  0  1  2 ...,  ] 
[100 101 102 ..., 197 198 199] 
[201 ..., 297 298 299] 
..., 
[9700 9701 9702 ..., 9797 9798 9799] 
[9800 9801 9802.., 9897 9898 9899]    

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

Set_printoptions (threshold= ' nan ') 

Thus, all the elements of an array are displayed when the output is made.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.