numpy的使用
numpy的使用
一、创建ndarray
1. 使用np.array()由python list创建
注意:
- numpy默认ndarray的所有元素的类型是相同的
- 如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int
import numpy as np
l = [1,2,3,4,5,6,7]
print(type(l)) # list
nd = np.array(l)
print(type(nd)) # numpy.ndarray
-
求和
nd.sum()
-
求均方差
nd.var()
-
求标准差
nd.std()
-
随机数生成
x = np.arange(0,100000,1) print(x) # array([ 0, 1, 2, ..., 99997, 99998, 99999])
2. 使用np的routines函数创建
-
np.ones(shape, dtype=None, order='C') # shape 形状 dtype 数据类型
x = np.ones(shape = (5,5),dtype=np.int8) print(x) # 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)
-
np.zeros(shape, dtype=float, order='C')
x = np.zeros(shape = (2,3,4),dtype=np.float16) print(x) #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)
-
np.full(shape, fill_value, dtype=None, order='C')
x = np.full(shape = (3,5),fill_value=3.14) print(x) #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, 3.14, 3.14, 3.14]])
-
np.eye(N, M=None, k=0, dtype=float) # 对角线为1其他的位置为0
# 单位矩阵 x = np.eye(N = 5) print(x) #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.]])
-
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
# 等差数列 np.linspace(0,100,num = 21) #array([ 0., 5., 10., 15., 20., 25., 30., 35., 40., 45., 50., # 55., 60., 65., 70., 75., 80., 85., 90., 95., 100.])
-
np.arange([start, ]stop, [step, ]dtype=None)
np.arange(0,100,3) #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])
-
np.random.randint(low, high=None, size=None, dtype='l')
np.random.randint(0,100,size = (5,5)) #array([[ 4, 45, 66, 55, 42], # [28, 5, 75, 71, 44], # [ 0, 17, 89, 81, 66], # [95, 96, 89, 31, 6], # [75, 31, 51, 81, 38]])
-
np.random.randn(d0, d1, ..., dn) # 标准正态分布
# normal 正常,正太 # dimession 维度 # 平均值是0,方差是1 np.random.randn(4,5) #array([[-1.26185332, 0.29715466, 0.52047771, -1.55183841, -0.83663771], # [ 0.40776138, -0.7380327 , 0.22623508, 1.12275365, -0.38189704], # [ 0.67816239, 0.91695635, 0.13487838, 0.13769114, 0.68426452], # [ 0.00935704, 0.49087787, -0.34920945, 0.15688878, -0.98320155]])
-
np.random.normal(loc=0.0, scale=1.0, size=None)
np.random.normal(loc = 175,scale=10,size = 10000).round(2) #array([180.02, 162.26, 184.18, ..., 179.19, 182.83, 174.51])
-
np.random.random(size=None) # 生成0到1的随机数,左闭右开
np.random.random(10) #array([0.80403643, 0.60631454, 0.22301424, 0.03813725, 0.14537585, # 0.00946211, 0.39063408, 0.5558176 , 0.39426771, 0.74874309])
二、ndarray的属性
4个必记参数:
- ndim:维度
- shape:形状(各维度的长度)
- size:总长度
- dtype:元素类型
三、ndarray的基本操作
1. 索引
一维与列表完全一致,多维时同理
nd2 = np.random.randint(0,150,size = (4,5))
print(nd2)
#array([[124, 91, 52, 23, 16],
# [122, 106, 143, 88, 85],
# [100, 0, 141, 101, 72],
# [ 26, 93, 123, 4, 31]])
nd2[1,1] # 106
nd2[2] # array([100, 0, 141, 101, 72])
2. 切片
一维与列表完全一致,多维时同理
nd2
array([[124, 91, 52, 23, 16],
[122, 106, 143, 88, 85],
[100, 0, 141, 101, 72],
[ 26, 93, 123, 4, 31]])
nd2[0:3]
array([[124, 91, 52, 23, 16],
[122, 106, 143, 88, 85],
[100, 0, 141, 101, 72]])
nd2[-2:]
array([[100, 0, 141, 101, 72],
[ 26, 93, 123, 4, 31]])
nd2[0:3,0:3]
array([[124, 91, 52],
[122, 106, 143],
[100, 0, 141]])
-
将数据反转,例如[1,2,3]---->[3,2,1]
nd3 = nd[:10] nd3 array([189.04, 166.26, 172.39, 172.1 , 173. , 176.82, 176. , 177.74, 162.46, 176.13]) nd3[::-1] array([176.13, 162.46, 177.74, 176. , 176.82, 173. , 172.1 , 172.39, 166.26, 189.04]) ## 两个::进行切片
3. 变形
使用reshape函数,注意参数是一个tuple!
nd2
array([[124, 91, 52, 23, 16],
[122, 106, 143, 88, 85],
[100, 0, 141, 101, 72],
[ 26, 93, 123, 4, 31]])
nd2.reshape(2,10)
array([[124, 91, 52, 23, 16, 122, 106, 143, 88, 85],
[100, 0, 141, 101, 72, 26, 93, 123, 4, 31]])
nd2.reshape(5,4)
array([[124, 91, 52, 23],
[ 16, 122, 106, 143],
[ 88, 85, 100, 0],
[141, 101, 72, 26],
[ 93, 123, 4, 31]])
4. 级联
np.concatenate()
级联需要注意的点:- 级联的参数是列表:一定要加中括号或小括号
- 维度必须相同
- 形状相符
- 【重点】级联的方向默认是shape这个tuple的第一个值所代表的维度方向
- 可通过axis参数改变级联的方向
nd2
array([[124, 91, 52, 23, 16],
[122, 106, 143, 88, 85],
[100, 0, 141, 101, 72],
[ 26, 93, 123, 4, 31]])
np.concatenate([nd2,nd2])
array([[124, 91, 52, 23, 16],
[122, 106, 143, 88, 85],
[100, 0, 141, 101, 72],
[ 26, 93, 123, 4, 31],
[124, 91, 52, 23, 16],
[122, 106, 143, 88, 85],
[100, 0, 141, 101, 72],
[ 26, 93, 123, 4, 31]])
-
np.hstack
与np.vstack
水平级联与垂直级联,处理自己,进行维度的变更nd1 = np.random.randint(0,150,size = (4,5)) nd2 = np.random.randint(0,150,size = (2,5)) nd3 = np.random.randint(0,150,size = (4,8)) display(nd1,nd2,nd3) array([[ 64, 14, 132, 3, 19], [ 42, 79, 61, 142, 48], [ 38, 93, 149, 120, 36], [ 21, 6, 98, 67, 38]]) array([[113, 15, 9, 31, 22], [ 3, 94, 60, 116, 79]]) array([[137, 13, 104, 7, 43, 136, 147, 12], [ 19, 36, 98, 39, 72, 38, 47, 128], [ 89, 149, 123, 149, 20, 73, 81, 6], [100, 6, 138, 127, 104, 28, 31, 61]]) # horizontal 水平的,列数增加 nd5 = np.hstack((nd1,nd3)) # vertical 竖直方向增多,行数增多 nd4 = np.vstack((nd1,nd2))
5. 切分
与级联类似,三个函数完成切分工作:
-
np.split
切分nd4 array([[ 64, 14, 132, 3, 19], [ 42, 79, 61, 142, 48], [ 38, 93, 149, 120, 36], [ 21, 6, 98, 67, 38], [113, 15, 9, 31, 22], [ 3, 94, 60, 116, 79]]) np.split(nd4,indices_or_sections=3) [array([[ 64, 14, 132, 3, 19], [ 42, 79, 61, 142, 48]]), array([[ 38, 93, 149, 120, 36], [ 21, 6, 98, 67, 38]]), array([[113, 15, 9, 31, 22], [ 3, 94, 60, 116, 79]])] np.split(nd4,indices_or_sections=[1,3]) [array([[ 64, 14, 132, 3, 19]]), array([[ 42, 79, 61, 142, 48], [ 38, 93, 149, 120, 36]]), array([[ 21, 6, 98, 67, 38], [113, 15, 9, 31, 22], [ 3, 94, 60, 116, 79]])]
-
np.vsplit
竖直轴切分np.vsplit(nd4,indices_or_sections=2) [array([[ 64, 14, 132, 3, 19], [ 42, 79, 61, 142, 48], [ 38, 93, 149, 120, 36]]), array([[ 21, 6, 98, 67, 38], [113, 15, 9, 31, 22], [ 3, 94, 60, 116, 79]])]
-
np.hsplit
水平轴切割nd5 array([[ 2, 47, 19, 21, 27], [21, 66, 29, 86, 48], [12, 78, 87, 82, 90], [87, 21, 3, 56, 78], [ 7, 18, 8, 64, 95], [46, 2, 62, 36, 11]]) np.hsplit(nd5,indices_or_sections=[3]) [array([[ 2, 47, 19], [21, 66, 29], [12, 78, 87], [87, 21, 3], [ 7, 18, 8], [46, 2, 62]]), array([[21, 27], [86, 48], [82, 90], [56, 78], [64, 95],
6. 副本
所有赋值运算不会为ndarray的任何元素创建副本。对赋值后的对象的操作也对原来的对象生效。
nd7 = nd5
id(nd7)
2174915628688
id(nd5)
2174915628688
-
可使用copy()函数创建副本
nd8 = nd5.copy() id(nd8) 2174923593888