欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python扩展库numpy示例快速理解

程序员文章站 2024-01-11 16:11:46
1. 对数组进行函数运算>>> x=np.arange(0,100,10,dtype=np.floating)>>> xarray([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90.])>>> index=np.random.randint(0,len(x),5)>>> indexarray([9, 0, 3, 0, 2])>>> x[index]arra...

1. 对数组进行函数运算

>>> x=np.arange(0,100,10,dtype=np.floating)
>>> x
array([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90.])
>>> index=np.random.randint(0,len(x),5)
>>> index
array([9, 0, 3, 0, 2])
>>> x[index]
array([90.,  0., 30.,  0., 20.])
>>> x[index]=[1,2,3,4,5]
>>> x
array([ 4., 10.,  5.,  3., 40., 50., 60., 70., 80.,  1.])
>>> x[[1,2,3]]
array([10.,  5.,  3.])
>>>
>>> x=np.arange(0,100,10,dtype=np.floating)
>>> np.sin(x)
array([ 0.        , -0.54402111,  0.91294525, -0.98803162,  0.74511316,
       -0.26237485, -0.30481062,  0.77389068, -0.99388865,  0.89399666])
>>> b=np.array(([1,2,3],[4,5,6],[7,8,9]))
>>> np.cos(b)
array([[ 0.54030231, -0.41614684, -0.9899925 ],
       [-0.65364362,  0.28366219,  0.96017029],
       [ 0.75390225, -0.14550003, -0.91113026]])
>>> np.round(_)
array([[ 1., -0., -1.],
       [-1.,  0.,  1.],
       [ 1., -0., -1.]])
>>> x=np.random.rand(10)
>>> x=x*10
>>> x
array([0.68242369, 1.39747941, 0.48375535, 5.34713392, 4.46608735,
       4.75378164, 3.5079992 , 6.54138794, 3.48111564, 0.22379024])
>>> np.floor(x)
array([0., 1., 0., 5., 4., 4., 3., 6., 3., 0.])
>>> np.ceil(x)
array([1., 2., 1., 6., 5., 5., 4., 7., 4., 1.])

2. 对矩阵不同维度上的元素进行计算

>>> x=np.arange(0,10).reshape(2,5)
>>> x
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> np.sum(x)
45
>>> np.sum(x,axis=0)
array([ 5,  7,  9, 11, 13])
>>> np.sum(x,axis=1)
array([10, 35])
>>> np.mean(x,axis=0)
array([2.5, 3.5, 4.5, 5.5, 6.5])
>>> weight=[0.3,0.7]
>>> np.average(x,axis=0,weight=weight)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 4, in average
TypeError: _average_dispatcher() got an unexpected keyword argument 'weight'
>>> np.average(x,axis=0,weights=weight)
array([3.5, 4.5, 5.5, 6.5, 7.5])
>>> np.max(x)
9
>>> np.min(x)
0
>>> x=np.random.randint(0,10,size=(3,3))
>>> x
array([[5, 2, 7],
       [0, 6, 4],
       [3, 1, 4]])
>>> np.std(x)
2.1659542988464366
>>> np.sort(x,axis=0)
array([[0, 1, 4],
       [3, 2, 4],
       [5, 6, 7]])

3. 改变数组大小

>>> a=np.arange(1,11,1)
>>> a
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
>>> a.shape=2,5
>>> a
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]])
>>> a.shape=5,-1
>>> a
array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10]])
>>> b=a.reshape(2,5)
>>> b
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]])

4.切片操作

>>> a=np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a[::-1]
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
>>> a[::2]
array([0, 2, 4, 6, 8])
>>> a[:5]
array([0, 1, 2, 3, 4])

5.布尔运算

>>> x=np.random.rand(10)
>>> x
array([0.99856181, 0.02720844, 0.9772333 , 0.89337884, 0.01307562,
       0.10111191, 0.33254189, 0.38107449, 0.04112068, 0.01870972])
>>> x>0.5
array([ True, False,  True,  True, False, False, False, False, False,
       False])
>>> x[x>0.5]
array([0.99856181, 0.9772333 , 0.89337884])
>>> a=np.array([1,2,3])
>>> b=np.array([3,2,1])
>>> a>b
array([False, False, False])
>>> a[a>b]
array([], dtype=int32)
>>> a=np.array([1,2,3])
>>> b=np.array([3,2,1])
>>> a>b
array([False, False,  True])
>>> a[a>b]
array([3])

本文地址:https://blog.csdn.net/weixin_46737224/article/details/109648628