Use of numpy Library of Python Foundation

  • 2021-11-02 01:52:12
  • OfStack

Overview of numpy Library

The most basic data type that the numpy library deals with is multidimensional arrays of the same elements, referred to simply as "arrays."

Characteristics of arrays:

All elements in the array must be of the same type Elements in an array can be indexed by integers Serial number starts from 0

The dimension of ndarray type is called axis, and the number of axes is called rank

Analysis of numpy Library

Because there are many functions in numpy library and it is easy to be confused with common names, it is recommended to use the following methods to reference numpy library


import numpy as np

Commonly used Array Creation Functions in numpy Library

函数 描述
np.array([x,y,z],dtype=int) 从Python列表和元组中创建数组
np.arange(x,y,i) 创建1个由x到y,以i为步长的数组
np.linspace(x,y,n) 创建1个由x到y,等分成n个元素的数组
np.indices((m,n)) 创建1个m行n列的矩阵
np.random.rand(m,n) 创建1个m行n列的随机数组
np.ones((m,n),dtype) 创建1个m行n列全1的数组,dtype是数据类型
np.empty((m,n),dtype) 创建1个m行n列全0的数组,dtype是数据类型

import numpy as np
a1 = np.array([1,2,3,4,5,6])
a2 = np.arange(1,10,3)
a3 = np.linspace(1,10,3)
a4 = np.indices((3,4))
a5 = np.random.rand(3,4)
a6 = np.ones((3,4),int)
a7 = np.empty((3,4),int)
print(a1)
print("===========================================================")
print(a2)
print("===========================================================")
print(a3)
print("===========================================================")
print(a4)
print("===========================================================")
print(a5)
print("===========================================================")
print(a6)
print("===========================================================")
print(a7)
=================================================================================
[1 2 3 4 5 6]
===========================================================
[1 4 7]
===========================================================
[ 1.   5.5 10. ]
===========================================================
[[[0 0 0 0]
  [1 1 1 1]
  [2 2 2 2]]

 [[0 1 2 3]
  [0 1 2 3]
  [0 1 2 3]]]
===========================================================
[[0.00948155 0.7145306  0.50490391 0.69827703]
 [0.18164292 0.78440752 0.75091258 0.31184394]
 [0.17199081 0.3789     0.69886588 0.0476422 ]]
===========================================================
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
===========================================================
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

After creating a simple array, you can view the attributes of the array

属性 描述
ndarray.ndim 数组轴的个数,也被称为秩
ndarray.shape 数组在每个维度上大小的整数元组
ndarray.size 数组元素的总个数
ndarray.dtype 数组元素的数据类型,dtype类型可以用于创建数组
ndarray.itemsize 数组中每个元素的字节大小
ndarray.data 包含实际数组元素的缓冲区地址
ndarray.flat 数组元素的迭代器

import numpy as np
a6 = np.ones((3,4),int)
print(a6)
print("=========================================")
print(a6.ndim)
print("=========================================")
print(a6.shape)
print("=========================================")
print(a6.size)
print("=========================================")
print(a6.dtype)
print("=========================================")
print(a6.itemsize)
print("=========================================")
print(a6.data)
print("=========================================")
print(a6.flat)
=================================================================================
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
=========================================
2
=========================================
(3, 4)
=========================================
12
=========================================
int32
=========================================
4
=========================================
<memory at 0x0000020D79545908>
=========================================
<numpy.flatiter object at 0x0000020D103B1180>

Arrays are treated as objects in numpy and can be used < a > . < b > ().

Morphological manipulation of ndarray class

方法 描述
ndarray.reshape(n,m) 不改变数组ndarray,返回1个维度为(n,m)的数组
ndarray.resize(new_shape) 与reshape()作用相同,直接修改数组ndarray
ndarray.swapaxes(ax1,ax2) 将数组n个维度中任意两个维度进行调换
ndarray.flatten() 对数组进行降维,返回1个折叠后的1维数组
ndarray.ravel() 作用同np.flatten(),但返回的是1个视图

Indexing and Slicing Methods of ndarray Class

方法 描述
x[i] 索引第i个元素
x[-i] 从后向前索引第i个元素
x[n:m] 默认步长为1,从前向后索引,不包含m
x[-m:-n] 默认步长为1,从前向后索引,结束位置为n
x[n: m :i] 指定i步长的由n到m的索引

In addition to ndarray type methods, numpy library provides a single operation function

函数 描述
np.add(x1,x2[,y]) y = x1 + x2
np.subtract(x1,x2[,y]) y = x1 -x2
np.multiply(x1,x2[,y]) y = x1 * x2
np.divide(x1,x2[,y]) y = x1 /x2
np floor_divide(x1,x2[,y]) y = x1 // x2
np.negative(x[,y]) y = -x
np.power(x1,x2[,y]) y = x1 ** x2
np.remainder(x1,x2[,y]) y = x1 % x2

Comparison Operation Function of numpy Library

函数 符号描述
np.equal(x1,x2[,y]) y = x1 == x2
np.not_equal(x1,x2[,y]) y = x1 != x2
np.less(x1,x2,[,y]) y = x1 < x2
np.less_equal(x1,x2,[,y]) y = x1 < = x2
np.greater(x1,x2,[,y]) y = x1 > x2
np.greater_equal(x1,x2,[,y]) y >= x1 >= x2
np.where(condition[x,y]) 根据条件判断是输出x还是y

Other Operational Functions of numpy Library

函数 描述
np.abs(x) 计算济源元素的整形、浮点、或复数的绝对值
np.sqrt(x) 计算每个元素的平方根
np.squre(x) 计算每个元素的平方
np.sign(x) 计算每个元素的符号1(+),0,-1(-)
np.ceil(x) 计算大于或等于每个元素的最小值
np.floor(x) 计算小于或等于每个元素的最大值
np.rint(x[,out]) 圆整,取每个元素为最近的整数,保留数据类型
np.exp(x[,out]) 计算每个元素的指数值
np.log(x),np.log10(x),np.log2(x) 计算自然对数(e),基于10,,2的对数,log(1+x)

Related articles: