Python scientific computing library Numpy (1) - overview

Table of contents

一 Numpy(Numerical Python)

1. What is Numpy?

2. The main purpose of Numpy

Two Numpy arrays VS Python lists

Three Numpy data types and attributes

1. Data type

(1) Understand data types

(2) Code example

2. Array properties

(1) General array properties

(2) Code example

Quad Broadcasting and Vectoring

1. Adding arrays of different dimensions

 2. Multiplying arrays of different dimensions

Five conclusions

Six Reference Links


一 Numpy(Numerical Python)

1. What is Numpy?

Numpy is a basic extension package for scientific computing in the Python language. It supports a large number of dimensional arrays and matrix operations, and also provides a large number of mathematical function libraries for array operations. It is a very fast math library, mainly used for array calculations, including:

  • A powerful n-dimensional array object ndarray
  • broadcast function
  • Tools for integrating C/C++/Fortran code
  • Linear algebra, Fourier transform, random number generation and other functions

2. The main purpose of Numpy

Numpy is used in combination with third-party libraries such as Matplotlib ( Python drawing library Matplotlib ) to provide a powerful scientific and mathematical computing environment for us to learn data science or machine learning.

Two Numpy arrays VS Python lists

(1) Numpy is specially designed for array operations and calculations. The storage efficiency and input and output performance are far greater than the nested operations in the list. When the dimension of the array is larger, the advantages of Numpy are more obvious.

(2) Both can operate on multidimensional arrays. The ndarray in Numpy is a huge data container that can easily handle multidimensional arrays; lists can be nested to perform operations on multidimensional arrays.

(3) The array elements in Numpy must be homogeneous, and the element types in the list can be diverse, so the general performance of the list is stronger than that of Numpy, but the code of Numpy is simpler when performing scientific calculations.

Three Numpy data types and attributes

1. Data type

(1) Understand data types

The general data types in Numpy are shown in the following table:
 

Data types in Numpy
type of data describe
bool_ Boolean data type (True or False)
int_ default integer type
intc Same as C's int type, usually int32 or int64
intp Integer type used for indexing
int8/int16/int32/int64 signed integer
uint8/uint16/uint32/uint64 unsigned integer
float_ Shorthand for float64 type
float16 Half-precision type: 1 sign bit, 5 exponent bits, 10 mantissa bits
float32 Single precision type: 1 sign bit, 8 exponent bits, 23 mantissa bits
float64 Double type: 1 sign bit, 11 exponent bits, 52 mantissa bits
complex_ Shorthand for complex128, which is a 128-bit complex number
complex64 64-bit complex number, representing a double 32-bit floating point number (real and imaginary part)
complex128 128-bit complex number, representing a double 64-bit floating point number (real and imaginary part)

(2) Code example

Generally, the np.dtype() statement can be used to describe the type, size, byte order, etc. of the data:

"""
Author:XiaoMa
date:2021/12/30
"""
import numpy as np

dt = np.dtype(np.int32)#使用标量标签
print(dt)

dt = np.dtype('i1')#int8, int16, int32, int64 四种数据类型可以使用字符串 'i1', 'i2','i4','i8' 代替
print(dt)

dt = np.dtype('<i1')#字节顺序是通过对数据类型预先设定 < 或 > 来决定的。 < 意味着小端法(最小值存储在最小的地址,即低位组放在最前面)。> 意味着大端法(最重要的字节存储在最小的地址,即高位组放在最前面)。
print(dt)

#--------------------
#结构化数据类型的使用
#--------------------
dt = np.dtype([('age', 'i1')])#创建结构化数据类型
print(dt)

a = np.array([(10,), (20,), (30,)], dtype = dt)#将数据类型应用于数组对象
print(a)
print(a['age'])#类型字段名可用于存取实际的 age 列
#-----------------------------------------------------
#创建一个student结构,用来记录学生的姓名、成绩、年龄。
#-----------------------------------------------------
student = np.dtype([('name', 'S8'), ('age', 'i1'), ('marks', 'f')])
a = np.array([('XiaoMa', '21', '99.9'), ('XiaoYang', '20', '98.8'), ('XiaoWang', '21', '97.7')], dtype = student)
print(a)

The output is as follows:
 in python3, b' ' means that the string belongs to the bytes type. For a detailed introduction, please refer to the link at the bottom of the article. 

2. Array properties

(1) General array properties

Each linear dimension of the array in Numpy is called an axis (axis), that is, the axis represents a one-dimensional array. When we operate on axis = 0, we operate on each column element of the array; when we operate on axis = 1, we operate on each row element of the array.

The number of dimensions of an array in Numpy is called rank, that is, the number of axes in the array. For example, the rank of a one-dimensional array is 1, and the rank of a two-dimensional array is 2.

Some important object attributes in numpy.ndarray are as follows:

ndarray.ndim: rank, i.e. the number of dimensions or axes

ndarray.shape: The dimension of the array, for the matrix, n rows and m columns

ndarray.size: the number of elements in the array, ie n*m

ndarray.dtype: The element type of the array object

ndarray.itemsize: the size of each element in bytes

ndarray.flags: memory information of the array object

ndarray.real: the real part of the array object

ndarray.imag: the imaginary part of the array object

(2) Code example

"""
Author:XiaoMa
date:2021/12/30
"""
import numpy as np

#-------------------
#Numpy 中的数据属性
#-------------------
a = np.arange(24)
print(a.ndim)#打印a的秩,目前a的维度只有1

a = a.reshape(2, 4, 3)#调整a的结构形状
print(a.ndim)

a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape)#使用 .shape 打印数组的行数和列数

a.shape = (3, 2)#使用 .shape 来改变数组的1形状
print(a)

x = np.array([1, 2, 3, 4, 5], dtype = np.int8)#数组的数据类型为 int8(一个字节)
print(x.itemsize)#打印字节数,为1

y = np.array([1, 2, 3, 4, 5], dtype = np.float64)# 数组的数据类型为 float64(八个字节)
print(y.itemsize)#打印字节数,为8

print(x.flags)#打印数组元素的内存信息

The results obtained are as follows:

Quad Broadcasting and Vectoring

When using Numpy to perform numerical calculations on arrays of different shapes, the arithmetic calculations on the corresponding array elements are performed on the corresponding elements, which is called Numpy broadcasting, such as:

1. Adding arrays of different dimensions

a0 = np.array([[1, 2, 3], [4, 5, 6]])
a1 = np.array([[1, 0, 0]])
print(a0 + a1)

Get the output:
 add the one-dimensional array to each dimension of the multidimensional array

 2. Multiplying arrays of different dimensions

a0 = np.array([[1, 2, 3], [4, 5, 6]])
a1 = np.array([[1, 0, 0]])
print(a0*a1)

get the output:

 It can be seen that the operation form is the same as addition, and it will be expanded and multiplied by each dimension.

Five conclusions

This blog post mainly briefly introduces the data types, array attributes and vectorized operations in Numpy, the basic scientific computing library in the Python language, and implements codes to enhance understanding. 

Six Reference Links

Difference between python list and Numpy array

Rookie Tutorial

The meaning of r'', b'', u'', f'' in python

Bytes type

Guess you like

Origin blog.csdn.net/qq_52309640/article/details/122235404