NumPy是Python中的一个运算速度非常快的一个数学库,是Python中的数据科学和机器学习。
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a=np.array([0,1,2,3,4])
>>> b=np.array((0,1,2,3,4))
>>> c=np.arange(5)
>>> d=np.linspace(0,2*np.pi,5)
>>> print(a)
[0 1 2 3 4]
>>> print(b)
[0 1 2 3 4]
>>> print(c)
[0 1 2 3 4]
>>> print(d)
[0. 1.57079633 3.14159265 4.71238898 6.28318531]
>>> print(a[3])
3
>>> a=np.array([[11,12,13,14,15],
... [16,17,18,19,20],
... [21,22,23,24,25],
... [26,27,28,29,30],
... [31,32,33,34,35]])
>>> print(a[2,4])
25
>>>
矢量、矩阵(暂时没明白)
>>> print(a[0,1:4])
[12 13 14]
>>> print(a[1:4,0])
[16 21 26]
>>> print(a[::2,::2])
[[11 13 15]
[21 23 25]
[31 33 35]]
>>> print(a[:,1])
[12 17 22 27 32]
>>>
>>> print(type(a))
>>> print(a.dtype)
int32
>>> print(a.size)
25
>>> print(a.shape)
(5, 5)
>>> print(a.itemsize)
4
>>> print(a.ndim)
2
>>> print(a.nbytes)
100
>>>
itemsize:每个项占用的字节数。
ndim:数组的维数。
nbytes: 数组中的所有数据消耗掉的字节数。(这句子没看懂)
>>> a=np.arange(25)
>>> a=a.reshape((5,5))
>>> b=np.array([10,62,1,14,2,56,79,2,1,45,
... 4,92,5,55,63,43,35,6,53,24,
... 56,3,56,44,78])
>>> b=b.reshape((5,5))
>>> print(a)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
>>> print(b)
[[10 62 1 14 2]
[56 79 2 1 45]
[ 4 92 5 55 63]
[43 35 6 53 24]
[56 3 56 44 78]]
>>> print(a+b)
[[ 10 63 3 17 6]
[ 61 85 9 9 54]
[ 14 103 17 68 77]
[ 58 51 23 71 43]
[ 76 24 78 67 102]]
>>> print(a-b)
[[-10 -61 1 -11 2]
[-51 -73 5 7 -36]
[ 6 -81 7 -42 -49]
[-28 -19 11 -35 -5]
[-36 18 -34 -21 -54]]
>>> print(a*b)
[[ 0 62 2 42 8]
[ 280 474 14 8 405]
[ 40 1012 60 715 882]
[ 645 560 102 954 456]
[1120 63 1232 1012 1872]]
>>> print(a/b)
[[0. 0.01612903 2. 0.21428571 2. ]
[0.08928571 0.07594937 3.5 8. 0.2 ]
[2.5 0.11956522 2.4 0.23636364 0.22222222]
[0.34883721 0.45714286 2.83333333 0.33962264 0.79166667]
[0.35714286 7. 0.39285714 0.52272727 0.30769231]]
>>> print(a**2)
[[ 0 1 4 9 16]
[ 25 36 49 64 81]
[100 121 144 169 196]
[225 256 289 324 361]
[400 441 484 529 576]]
>>> print(a>> print(a>b)
[[False False True False True]
[False False True True False]
[ True False True False False]
[False False True False False]
[False True False False False]]
>>> print(a.dot(b))
[[ 417 380 254 446 555]
[1262 1735 604 1281 1615]
[2107 3090 954 2116 2675]
[2952 4445 1304 2951 3735]
[3797 5800 1654 3786 4795]]
>>>
dot()函数计算两个数组的点积。(没看懂)
dot()函数称为点积。没看懂。
>>> a=np.arange(10)
>>> print(a)
[0 1 2 3 4 5 6 7 8 9]
>>> print(a.sum())
45
>>> print(a.min())
0
>>> print(a.max())
9
>>> print(a.cumsum())
[ 0 1 3 6 10 15 21 28 36 45]
>>>
cumsum()函数:首先将第一个元素和第二个元素相加,并将计算结果存储在一个列表中,然后将该结果添加到第三个元素中,然后再将该结果存储在一个列表中。(暂时没看懂)
花式索引是获取数组中我们想要的特定元素的有效方法。
>>> a=np.arange(0,100,10)
>>> indeces=[1,5,-1]
>>> b=a[indeces]
>>> print(a)
[ 0 10 20 30 40 50 60 70 80 90]
>>> print(b)
[10 50 90]
>>>
>>> import matplotlib.pyplot as plt
>>> a=np.linspace(0,2*np.pi,50)
>>> b=np.sin(a)
>>> plt.plot(a,b)
[]
>>> mask=b>=0
>>> plt.plot(a[mask],b[mask],'bo')
[]
>>> mask=(b>=0)&(a<=np.pi/2)
>>> plt.plot(a[mask],b[mask],'go')
[]
>>> plt.show()
>>>
>>> a=np.arange(0,100,10)
>>> b=a[:5]
>>> c=a[a>=50]
>>> print(a)
[ 0 10 20 30 40 50 60 70 80 90]
>>> print(b)
[ 0 10 20 30 40]
>>> print(c)
[50 60 70 80 90]
>>>
Where()函数是另外一个根据条件返回数组中的值的有效方法。只需要把条件传递给它,它就会返回一个使得条件为真的元素的列表。(没看懂)
>>> a=np.arange(0,100,10)
>>> b=np.where(a<50)
>>> c=np.where(a>=50)[0]
>>> print(b)
(array([0, 1, 2, 3, 4], dtype=int64),)
>>> print(c)
[5 6 7 8 9]
>>> print(a)
[ 0 10 20 30 40 50 60 70 80 90]
>>>