开始更新numpy相关的文章,本文介绍numpy中的25个小案例,主要内容是如何利用numpy来生成向量(一维数组),矩阵和高维数组等
NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
NumPy 是一个运行速度非常快的数学库,主要用于数组计算,包含:
import numpy as npprint(np.version)<module 'numpy.version' from '/Applications/downloads/anaconda/anaconda3/lib/python3.7/site-packages/numpy/version.py'>print(np.show_config)<function show at 0x1060cc560># np.info(np.abs)np.zeros(10)array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])np.zeros((5,2))array([[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]])np.zeros((2,2,3))array([[[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.]]])z = np.zeros((3,4))
zarray([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])z[2,3] = 1
z[1,1] = 2
zarray([[0., 0., 0., 0.],
[0., 2., 0., 0.],
[0., 0., 0., 1.]])np.nonzero(z)(array([1, 2]), array([1, 3]))
np.ones(6)array([1., 1., 1., 1., 1., 1.])np.ones((3,2))array([[1., 1.],
[1., 1.],
[1., 1.]])np.ones([2,3,2])array([[[1., 1.],
[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.],
[1., 1.]]])np.eye(4)array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])np.eye(4,dtype=int)array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
np.ones([2,3]) # 默认是浮点数array([[1., 1., 1.],
[1., 1., 1.]])np.ones([2,3],dtype=int) # 指定为int类型array([[1, 1, 1],
[1, 1, 1]])lst = [1,2,3,4]
np.array(lst)array([1, 2, 3, 4])# 指定数组类型
lst = [1,2,3,4]
np.array(lst, dtype=float)array([1., 2., 3., 4.])lst1 = [[1,2,3],[4,5,6]]
np.array(lst1)array([[1, 2, 3],
[4, 5, 6]])# 指定数据类型
lst1 = [[1,2,3],[4,5,6]]
np.array(lst1, dtype=float)array([[1., 2., 3.],
[4., 5., 6.]])t1 = (9,8,7)
np.array(t1)array([9, 8, 7])t2 = ((9,8,7),(6,5,4))
np.array(t2)array([[9, 8, 7],
[6, 5, 4]])lt = [(1,2,3),(7,8,9)]
np.array(lt)array([[1, 2, 3],
[7, 8, 9]])range_number = range(3,8)
np.array(range_number)array([3, 4, 5, 6, 7])# 指定类型
range_number = range(3, 8)
np.array(range_number, dtype=float)array([3., 4., 5., 6., 7.])边界值为1,其他为0
b = np.ones([6,6])
barray([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]])b[1:-1,1:-1] = 0
barray([[1., 1., 1., 1., 1., 1.],
[1., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 1.],
[1., 1., 1., 1., 1., 1.]])用0填充矩阵的边界
c = np.ones((6,6))
carray([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]])np.pad(c,pad_width=1,mode="constant",constant_values=0)array([[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 1., 1., 1., 1., 1., 0.],
[0., 1., 1., 1., 1., 1., 1., 0.],
[0., 1., 1., 1., 1., 1., 1., 0.],
[0., 1., 1., 1., 1., 1., 1., 0.],
[0., 1., 1., 1., 1., 1., 1., 0.],
[0., 1., 1., 1., 1., 1., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]])6*6的矩阵,对角线下方的值为1,2,3,4,5
np.diag(1 + np.arange(5), k=-1)array([[0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0],
[0, 2, 0, 0, 0, 0],
[0, 0, 3, 0, 0, 0],
[0, 0, 0, 4, 0, 0],
[0, 0, 0, 0, 5, 0]])numpy 包中的使用 arange 函数创建数值范围并返回 ndarray 对象,函数使用方法为:
numpy.arange(start, stop, step, dtype)np.arange(10)array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])# 指定步长
np.arange(0,10,2)array([0, 2, 4, 6, 8])# 指定类型
np.arange(0,10,2,dtype=float)array([0., 2., 4., 6., 8.])np.random.random((2,3,2))array([[[0.56045087, 0.15566786],
[0.34963774, 0.51837142],
[0.68895046, 0.04980068]],
[[0.98352437, 0.47189043],
[0.30430488, 0.49057744],
[0.20020709, 0.90466043]]])import pandas as pd
s = pd.Series([1,2,3,4])
np.array(s)array([1, 2, 3, 4])d = pd.DataFrame([[1,2,3,4],[9,8,7,6]])
np.array(d)array([[1, 2, 3, 4],
[9, 8, 7, 6]])ten = np.arange(10)
tenarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])ten[::-1]array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])主要是用来改变数组的形状
arr = np.arange(16)
arrarray([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])arr.shape(16,)arr.reshape((4,4))array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])arr.reshape((2,8))array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15]])arr.reshape((8,2))array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11],
[12, 13],
[14, 15]])arr.reshape((1,16))array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]])我们需要特别注意-1的使用,numpy会自动生成相应的shape值
arr.reshape((8,-1))array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11],
[12, 13],
[14, 15]])arr.reshape((-1,8))array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15]])用于构建一个等差数列的数组,使用方法为:
np.linspace(
start, # 起始值
stop, # 终止值,如果endpoint为true,该值包含于数列中
num=50, # 生成的样本量,默认为50
endpoint=True, #是否包含末尾的值;默认为True
retstep=False, # 为True时,生成的数组中会显示间距,反之不
dtype=None # 数据类型
)np.linspace(1,10,5)array([ 1. , 3.25, 5.5 , 7.75, 10. ])可以不包含末尾的数值:
np.linspace(1,10,5,endpoint=False)array([1. , 2.8, 4.6, 6.4, 8.2])全部是1的等差数列:
# 全部是1的等差数列
np.linspace(1,1,10)array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])加上restep参数,则会显示步长:
np.linspace(1,10,5,retstep=True)(array([ 1. , 3.25, 5.5 , 7.75, 10. ]), 2.25)主要是用于生成等比数列,使用方法为:
np.logspace(
start, # base ** start(指数)
stop, # base ** stop;如果endpoint为true,该值包含于数列中
num=50,
endpoint=True,
base=10.0, # 默认底数为10
dtype=None
)np.logspace(1,5,num=10)array([1.00000000e+01, 2.78255940e+01, 7.74263683e+01, 2.15443469e+02,
5.99484250e+02, 1.66810054e+03, 4.64158883e+03, 1.29154967e+04,
3.59381366e+04, 1.00000000e+05])指定不同的底数;第一个数为2的0次方,为1:
np.logspace(0,8,num=10,base=2)array([ 1. , 1.85174942, 3.42897593, 6.34960421,
11.75787594, 21.77264 , 40.3174736 , 74.65785853,
138.24764658, 256. ])