导入numpy库,并查看numpy版本
In [1]:
import numpy as np
In [3]:
np.__version__
Out[3]:
'1.24.3'
参数为列表:[1,4,2,5,3]
注意:
numpy默认ndarray的所有元素的类型是相同的 如果传进去的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int
In [4]:
l=[1,4,2,5,7]
l
Out[4]:
[1, 4, 2, 5, 7]
In [8]
type(l)
Out[8]:
list
In [10]:
nd=np.array(l)
nd
Out[10]:
array([1, 4, 2, 5, 7])
In [11]:
type(nd)
Out[11]:
numpy.ndarray
In [12]:
l2=[[1,3,5],[2,4,6]]
l2
Out[12]:
[[1, 3, 5], [2, 4, 6]]
In [13]:
nd2=np.array(l2)
nd2
Out[13]:
array([[1, 3, 5], [2, 4, 6]])
In [15]:
nd.max()
Out[15]:
7
In [16]:
x=np.arange(1,10000,1)
x
Out[16]:
array([ 1, 2, 3, ..., 9997, 9998, 9999])
In [17]:
%timeit x.sum()
5.21 µs ± 201 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
In [18]:
def sum1(x):
res=0
for i in x:
res+=i
return res
In [19]:
%timeit sum1(x)
952 µs ± 30.3 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
包含以下常见创建方法: 1)np.ones(shape,dtype=None,order='C')
创建个指定形状的全为1的数组
In [2]:
np.ones(shape=(5,5),dtype=np.int8)
Out[2]:
array([[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]], dtype=int8)
2)np.zeros(shape,dtype=float,order='C')
在创建指定形状的全为0的数组
In [3]:
np.zeros(shape=(2,3,4),dtype=np.float16)
Out[3]:
array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]], dtype=float16)
3)np.full(shape,fill_value,dtype=None,order='C')
再创建指定形状并填充特定值的数组
In [5]:
np.full(shape=(3,4),fill_value=3.14)
Out[5]:
array([[3.14, 3.14, 3.14, 3.14], [3.14, 3.14, 3.14, 3.14], [3.14, 3.14, 3.14, 3.14]])
4)np.eye(N,M=none,k=0,dtype=float) 对角线为1,其他位置为0
创建一个单位矩阵,对角线为1,其他位置为0
In [6]:
np.eye(N=5)
Out[6]:
array([[1., 0., 0., 0., 0.], [0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.], [0., 0., 0., 0., 1.]])
5)np.linspace(start,stop,num=50,endpoint=True,retstep=False,dtype=None)
让它在指定范围内生成指定数量的等间隔数值
In [14]:
np.linspace(0,100,num=51)
Out[14]:
array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20., 22., 24., 26., 28., 30., 32., 34., 36., 38., 40., 42., 44., 46., 48., 50., 52., 54., 56., 58., 60., 62., 64., 66., 68., 70., 72., 74., 76., 78., 80., 82., 84., 86., 88., 90., 92., 94., 96., 98., 100.])
6)np.arange([start,]stop,[stop,]dtype=None)
让它在指定范围内以指定步长生成数组
In [15]:
np.arange(0,100,3)
Out[15]:
array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99])
7)np.random.randint(low,high=None,size=None,dtype='l')
生成指定范围内的随机整数数组
随机一下:
In [16]:
np.random.randint(0,100,(5,5))
Out[16]:
array([[29, 48, 25, 62, 47], [54, 95, 67, 16, 80], [ 5, 1, 97, 32, 40], [55, 44, 71, 21, 2], [ 7, 21, 89, 85, 93]])
8)np.random.randn(d0,d1,d2,...,dn) 标准正态分布
生成服从标准正态分布的随机数组
In [17]:
#平均值为0,方差为1
np.random.randn(4,5)
Out[17]:
array([[ 0.12986708, 0.38932889, -0.68477293, -0.48738848, -1.29017705], [-0.62040021, 1.77145003, -0.03924932, -0.3581067 , -0.63517962], [ 0.09991903, -1.3522429 , -0.58625064, 1.70491804, -3.71604832], [-0.3656734 , 2.28166109, 0.43121117, 0.74830232, 0.30966411]])
9)np.random.normal(loc=0.0,scala=1.0,size=None) loc:位置 scala:标准差
In [27]:
nd=np.random.normal(175,10,size=1000).round(2)
#round(n):取小数位后n位 nd
. . .
In [28]:
nd.mean()
Out[28]:
174.88183999999998
In [29]:
nd.var()
#方差
Out[29]:
98.93779501440001
In [31]:
nd.std()
#标准差:方差开平方
Out[31]:
9.946747961741064
10)np.random.random(size=None) 生成0到1的随机数,左闭右开
In [32]:
np.random.random(100)
. . .
4个必记参数:ndim:维度 shape:形状(各维度的长度) size:总长度 dtype:元素类型
In [33]:
nd.ndim
Out[33]:
1
In [34]:
nd.shape
Out[34]:
(1000,)
In [35]:
nd.size
Out[35]:
1000
In [36]:
nd.dtype
Out[36]:
dtype('float64')
一维与列表完全一致 多维时同理
In [38]:
nd2=np.random.randint(0,150,size=(4,5))
nd2
Out[38]:
array([[ 26, 85, 41, 21, 49], [ 27, 2, 51, 55, 34], [133, 78, 63, 52, 135], [ 26, 56, 77, 51, 13]])
In [39]:
nd2[1,1]
Out[39]:
2
In [40]:
nd2[2]
Out[40]:
array([133, 78, 63, 52, 135])
根据索引修改数据
一维与列表完全一致 多维时同理
In [41]:
nd2
Out[41]:
array([[ 26, 85, 41, 21, 49], [ 27, 2, 51, 55, 34], [133, 78, 63, 52, 135], [ 26, 56, 77, 51, 13]])
In [43]:
nd2[0:3]
Out[43]:
array([[ 26, 85, 41, 21, 49], [ 27, 2, 51, 55, 34], [133, 78, 63, 52, 135]])
In [45]:
nd2[-2:]
Out[45]:
array([[133, 78, 63, 52, 135], [ 26, 56, 77, 51, 13]])
In [46]:
nd2[0:3,0:3]
Out[46]:
array([[ 26, 85, 41], [ 27, 2, 51], [133, 78, 63]])
将数据反转,例如[1,2,3]---->[3,2,1]
In [50]:
nd3=nd[:10]
nd3
Out[50]:
array([167.79, 195.33, 178.68, 171.35, 170.08, 154.38, 180.5 , 173.5 , 168.88, 154.37])
In [51]:
nd3[::-1]
Out[51]:
array([154.37, 168.88, 173.5 , 180.5 , 154.38, 170.08, 171.35, 178.68, 195.33, 167.79])
两个::进行切片
In [52]:
nd3[::2]
#隔一行算一行
Out[52]:
array([167.79, 178.68, 170.08, 180.5 , 168.88])