Numpy Tips Series 01 - Introduction

Numpy Tips Series 01 - Introduction

Official website portal:
http://www.numpy.org/

NumPy (Numerical Python) is an extended program library of the Python language that supports a large number of dimensional arrays and matrix operations, and also provides a large number of mathematical function libraries for array operations.

NumPy's predecessor, Numeric, was first developed by Jim Hugunin and other collaborators. In 2005, Travis Oliphant combined the features of another program library of the same nature, Numarray, in Numeric, and added other extensions to develop NumPy. NumPy is open source and developed by many collaborators.

basic terms

NumPy's main object is a homogeneous multidimensional array. It is a table of elements (usually numbers) all of the same type, indexed by a tuple of non-negative integers. In NumPy, dimensions are called axes.
For example, the coordinates [1,2,1] of a point in 3D space have an axis. This axis has 3 elements, so let's say it has length 3. In the example below, the array has two axes. The length of the first axis is 2 and the length of the second axis is 3.

[[ 1., 3., 0.],
 [ 0., 1., 2.]]

NumPy's array class is called ndarray. Alias ​​alias array. Note that numpy arrays are not the same as standard Python library class arrays. The Python library array which only deals with one-dimensional arrays and provides less functionality.
The more important attributes of ndarray objects are:

1)
The number of axes (dimensions) of the ndarray.ndim array.

2)
The dimension of the ndarray.shape array. This is a tuple of integers denoting the size of the array in each dimension. For a matrix with n rows and m columns, the shape will be (n,m). Thus, the length of the shape tuple is the number of axes ndim.

3)
The total number of elements in the ndarray.size array. It is equal to the product of shape elements.

4) ndarray.dtype
describes the object of the element type in the array. A dtype can be created or specified using standard Python types. Additionally, NumPy provides its own types. For example: numpy.int32, numpy.int16, and numpy.float64.

5)
The byte size of each element in the ndarray.itemsize array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while an array of elements of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.type.itemsize.
For example:

>>> import numpy as np
>>> a = np.arange(15).reshape(3, 5)
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> a.shape
(3, 5)
>>> a.ndim
2
>>> a.dtype.name
'int64'
>>> a.itemsize
8
>>> a.size
15
>>> type(a)
<type 'numpy.ndarray'>
>>> b = np.array([6, 7, 8])
>>> b
array([6, 7, 8])
>>> type(b)

Guess you like

Origin blog.csdn.net/weixin_45582028/article/details/103840590