赞
踩
目录
- >>>import numpy as np
- >>>np.array([1,2,3,4,5]) #把列表转换为一维数组
- array([1, 2, 3, 4, 5])
- >>>print(_)
- [1 2 3 4 5]
- >>>np.array((1,2,3,4,5)) #把元组转换为一维数组
- array([1, 2, 3, 4, 5])
- >>>np.array(range(5)) #把range对象转换成一维数组
- array([0, 1, 2, 3, 4])
- >>>arr=np.array([[1,2,3],[4,5,6]]) #二维数组,外[]不可少
- >>>arr
- array([[1, 2, 3],
- [4, 5, 6]])
- >>>print(arr)
- [[1 2 3]
- [4 5 6]]
- >>>np.arange(8) #类似于内置函数range()
- array([0, 1, 2, 3, 4, 5, 6, 7])
- >>>np.arange(1,10,2)
- array([1, 3, 5, 7, 9])
- >>>np.linspace(0,10,11) #等差数组,包含11个数
- array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
- >>>np.linspace(0,10,11,endpoint=False) #不包含终点
- array([0. , 0.90909091, 1.81818182, 2.72727273, 3.63636364,
- 4.54545455, 5.45454545, 6.36363636, 7.27272727, 8.18181818,
- 9.09090909])
- >>>np.logspace(0,100,10) #相当于10**np.linspace(0,100,10)
- array([1.00000000e+000, 1.29154967e+011, 1.66810054e+022, 2.15443469e+033,
- 2.78255940e+044, 3.59381366e+055, 4.64158883e+066, 5.99484250e+077,
- 7.74263683e+088, 1.00000000e+100])
- >>>np.logspace(1,6,5,base=2) #相当于2**np.linspace(1,,6,5)
- array([ 2. , 4.75682846, 11.3137085 , 26.90868529, 64. ])
- >>>np.zeros(3) #全0一维数组
- array([0., 0., 0.])
- >>>np.ones(3) #全1一维数组
- array([1., 1., 1.])
- >>>np.zeros((3,3)) #全零二维数组,三行三列
- array([[0., 0., 0.],
- [0., 0., 0.],
- [0., 0., 0.]])
- >>>np.zeros((3,1)) #全零二维数组,三行一列
- array([[0.],
- [0.],
- [0.]])
- >>> np.zeros((1,3)) #全零二维数组,一行三列
- array([[0., 0., 0.]])
- >>> np.ones((3,3)) #全1二维数组,三行三列
- array([[1., 1., 1.],
- [1., 1., 1.],
- [1., 1., 1.]])
- >>> np.ones((1,3)) #全一二维数组,一行三列
- array([[1., 1., 1.]])
- >>> np.identity(3) #单位矩阵,三行三列
- array([[1., 0., 0.],
- [0., 1., 0.],
- [0., 0., 1.]])
- >>> np.random.randint(0,50,5) #随机数组,5个0-50之间的数字
- array([49, 34, 36, 34, 27])
- >>> np.random.randint(0,50,(3,5)) #三行五列,共15个随机数,都介于[0,50]
- array([[36, 13, 39, 15, 40],
- [26, 32, 14, 27, 22],
- [ 2, 5, 15, 14, 14]])
- >>> np.random.rand(10) #10个介于[0,1)的随机数
- array([0.1339926 , 0.91646838, 0.05426131, 0.19442916, 0.16623762,
- 0.2365288 , 0.33290243, 0.250113 , 0.96977386, 0.59846432])
- >>> np.random.standard_normal(5) #从标准正态分布中随机采样五个数字
- array([-0.97958578, 0.0814909 , 0.89747636, -1.23791227, -0.73942231])
- >>> x=np.random.standard_normal(size=(3,4,2))
- >>> x
- array([[[ 1.1494124 , -0.47706184],
- [-1.10716196, 0.28543639],
- [ 0.05352001, -0.45464289],
- [ 0.2345641 , 0.954789 ]],
-
- [[-1.20323603, 2.6723773 ],
- [-0.49191396, -2.1001691 ],
- [ 0.18914176, -0.52134758],
- [-1.25792163, 0.03047616]],
-
- [[-0.98496622, -0.59795298],
- [ 0.81130705, 0.56014691],
- [ 0.27234357, -0.87448426],
- [-0.26274332, -0.91526728]]])
- >>> np.diag([1,2,3,4]) #对角矩阵
- array([[1, 0, 0, 0],
- [0, 2, 0, 0],
- [0, 0, 3, 0],
- [0, 0, 0, 4]])
- >>> import numpy as np
- >>> x=np.array([1,2,3,4.001,5])
- >>> y=np.array([1,1.9999,3,4.01,5.1])
- >>> print(np.allclose(x,y))
- False
- >>> print(np.allclose(x,y,rtol=0.2)) #设置相对误差参数
- True
- >>> print(np.allclose(x,y,atol=0.2)) #设置绝对误差参数
- True
- >>> print(np.isclose(x,y))
- [ True False True False False]
- >>> print(np.isclose(x,y,atol=0.2))
- [ True True True True True]
- >>> x=np.arange(8)
- >>> x
- array([0, 1, 2, 3, 4, 5, 6, 7])
- >>> np.append(x,8) #返回新数组,在尾部追加一个元素
- array([0, 1, 2, 3, 4, 5, 6, 7, 8])
- >>> np.append(x,[9,10]) #返回新数组,在尾部追加多个元素
- array([ 0, 1, 2, 3, 4, 5, 6, 7, 9, 10])
- >>> x #不影响原来的数组
- array([0, 1, 2, 3, 4, 5, 6, 7])
- >>> x[3]=8 #使用下标的形式原地修改元素值
- >>> x #原来的数组被修改了
- array([0, 1, 2, 8, 4, 5, 6, 7])
- >>> np.insert(x,1,8) #返回新数组,插入元素
- array([0, 8, 1, 2, 8, 4, 5, 6, 7])
- >>> x.put(0,9) #修改原数组指定位置上的元素
- >>> x=np.array([[1,2,3],[4,5,6],[7,8,9]])
- >>> x[0,2]=4 #修改第0行到第2列的元素值
- >>> x[1:,1:]=1 #切片,把行下标大于等于1,且列下标也大于等于1的元素值都设置为1
- >>> x
- array([[1, 2, 4],
- [4, 1, 1],
- [7, 1, 1]])
- >>> x[1:,1:]=[1,2] #同时修改多个元素值
- >>> x
- array([[1, 2, 4],
- [4, 1, 2],
- [7, 1, 2]])
- >>> x[1:,1:]=[[1,2],[3,4]]
- >>> x
- array([[1, 2, 4],
- [4, 1, 2],
- [7, 3, 4]])
- >>> import numpy as np
- >>> x=np.array((1,2,3,4,5))
- >>> x
- array([1, 2, 3, 4, 5])
- >>> x*2
- array([ 2, 4, 6, 8, 10])
- >>> x/2
- array([0.5, 1. , 1.5, 2. , 2.5])
- >>> x//2
- array([0, 1, 1, 2, 2], dtype=int32)
- >>> x**3
- array([ 1, 8, 27, 64, 125], dtype=int32)
- >>> x+2
- array([3, 4, 5, 6, 7])
- >>> x%3
- array([1, 2, 0, 1, 2], dtype=int32)
- >>> 2**x
- array([ 2, 4, 8, 16, 32], dtype=int32)
- >>> 2/x
- array([2. , 1. , 0.66666667, 0.5 , 0.4 ])
- >>> 63//x
- >>> np.array([1,2,3,4])+np.array([4,3,2,1])
- array([5, 5, 5, 5])
- >>> np.array([1,2,3,4])+np.array([4])
- array([5, 6, 7, 8])
- >>> a=np.array((1,2,3))
- >>> a+a
- array([2, 4, 6])
- >>> a*a
- array([1, 4, 9])
- >>> a-a
- array([0, 0, 0])
- >>> a/a
- array([1., 1., 1.])
- >>> a**a
- array([ 1, 4, 27])
- >>> b=np.array([[1,2,3],[4,5,6],[7,8,9]])
- >>> c=a*b
- >>> c
- array([[ 1, 4, 9],
- [ 4, 10, 18],
- [ 7, 16, 27]])
- >>> a+b
- array([[ 2, 4, 6],
- [ 5, 7, 9],
- [ 8, 10, 12]])
- >>>x=np.array([3,1,2])
- >>>np.argsort(x) #返回排序后元素的原下标
- array([1, 2, 0], dtype=int64)
- >>>x[_] #使用数组做下标,获取对应位置的元素
- array([1, 2, 3])
- >>>x=np.array([3,1,2,4])
- >>> x.argmax(),x.argmin() #最大值和最小值的下标
- (3, 1)
- >>> np.argsort(x)
- array([1, 2, 0, 3], dtype=int64)
- >>> x[_]
- array([1, 2, 3, 4])
- >>> x.sort() #原地排序
- >>> x
- array([1, 2, 3, 4])
- >>> x=np.random.randint(1,100,10) #随机整数
- >>> x
- array([39, 94, 74, 83, 60, 20, 76, 71, 81, 20])
- >>> np.argsort(x) #排序后原下标
- array([5, 9, 0, 4, 7, 2, 6, 8, 3, 1], dtype=int64)
- >>> x[_] #按序访问元素
- array([20, 20, 39, 60, 71, 74, 76, 81, 83, 94])
- >>> x[sorted(np.argsort(x)[-5:])] #按原来的顺序返回最大的五个数
- array([94, 74, 83, 76, 81])
- >>> x=np.array([[0,3,4],[2,2,1]])
- >>> np.argsort(x,axis=0) #二维数组纵向排序,返回原下标
- array([[0, 1, 1],
- [1, 0, 0]], dtype=int64)
- >>> np.argsort(x,axis=1) #二维数组横向排序
- array([[0, 1, 2],
- [2, 0, 1]], dtype=int64)
- >>> x.sort(axis=1) #原地排序,横向;注意,是每行单独排序
- >>> x
- array([[0, 3, 4],
- [1, 2, 2]])
- >>> x.sort(axis=0) #原地排序,纵向,每列单独排序
- >>> x
- array([[0, 2, 2],
- [1, 3, 4]])
- >>> import numpy as np
- >>> x=np.array([1,2,3])
- >>> x.repeat(3) #每个元素都重复3次
- array([1, 1, 1, 2, 2, 2, 3, 3, 3])
- >>> x.repeat([1,2,3]) #三个元素分别重复1,2,3次
- array([1, 2, 2, 3, 3, 3])
- >>> x=np.random.randint(1,10,(2,3))
- >>> x
- array([[4, 4, 9],
- [1, 1, 7]])
- >>> x.repeat(2) #重复后变一维数组了
- array([4, 4, 4, 4, 9, 9, 1, 1, 1, 1, 7, 7])
- >>> x.repeat([2,3],axis=0) #第一行重复2次,第二行重复3次
- array([[4, 4, 9],
- [4, 4, 9],
- [1, 1, 7],
- [1, 1, 7],
- [1, 1, 7]])
- >>> x=np.random.randint(1,10,(2,3))
- >>> x
- array([[3, 6, 4],
- [1, 8, 6]])
- >>> np.tile(x,2) #铺瓷砖(不跨块)
- array([[3, 6, 4, 3, 6, 4],
- [1, 8, 6, 1, 8, 6]])
- >>> np.tile(x,3)
- array([[3, 6, 4, 3, 6, 4, 3, 6, 4],
- [1, 8, 6, 1, 8, 6, 1, 8, 6]])
- >>> b=np.array(([1,2,3],[4,5,6],[7,8,9]))
- >>> b
- array([[1, 2, 3],
- [4, 5, 6],
- [7, 8, 9]])
- >>> b.T #转置
- array([[1, 4, 7],
- [2, 5, 8],
- [3, 6, 9]])
- >>> a=np.array((1,2,3,4))
- >>> a
- array([1, 2, 3, 4])
- >>> a.T #一维数组转置后和原来是一样的
- array([1, 2, 3, 4])
- >>> x=np.array([3,1,2,4])
- >>> x.cumsum() #累计和
- array([ 3, 4, 6, 10])
- >>> x.cumprod() #累计积
- array([ 3, 3, 6, 24])
- >>> x
- array([3, 1, 2, 4])
- >>> np.intersect1d([1,3,4,2],[3,1,2,1]) #交集,返回有序数组。“1”是数字
- array([1, 2, 3])
- >>> from functools import reduce
- >>> reduce(np.intersect1d,([1,3,4,3],[3,1,2,1],[6,3,4,2]))
- array([3])
- >>> np.union1d([1,4,3,3],[3,1,2,1]) #并集,返回有序数组
- array([1, 2, 3, 4])
- >>> np.in1d([1,3,4,3],[3,1,2,1]) #前一个数组的每个元素是否在第二个数组中
- array([ True, True, False, True])
- >>> np.setdiff1d([1,3,4,3],[3,1,2,1]) #差集
- array([4])
- >>> np.setxor1d([1,3,4,3],[3,1,2,1]) #对称差集
- array([2, 4])
- >>> import numpy as np
- >>> x=np.array((1,2,3))
- >>> y=np.array((4,5,6))
- >>> print(np.dot(x,y)) #输出结果都是32
- 32
- >>> print(x.dot(y))
- 32
- >>> print(sum(x*y))
- 32
- >>> a=np.array((5,6,7))
- >>> c=np.array(([1,2,3],[4,5,6],[7,8,9])) #二维数组
- >>> c.dot(a) #二维数组的每行与一维数组计算内积
- array([ 38, 92, 146])
- >>> c[0].dot(a) #验证一下,两个一维向量计算内积
- 38
- >>> c[1].dot(a)
- 92
- >>> c[2].dot(a)
- 146
- >>> a.dot(c) #一维向量与二维向量的每列计算内积
- array([ 78, 96, 114])
- >>> cT=c.T #转置
- >>> a.dot(cT[0]) #验证一下
- 78
- >>> a.dot(cT[1])
- 96
- >>> a.dot(cT[2])
- 114
- >>>b=np.array(([1,2,3],[4,5,7],[7,8,9]))
- >>>b
- array([[1, 2, 3],
- [4, 5, 7],
- [7, 8, 9]])
- >>>b[0] #第零行所有元素
- array([1, 2, 3])
- >>>b[0][0] #第零行和第零列的元素
- 1
- >>>b[0,0] #第零行和第零列的元素
- 1
- >>>b[[0,1]] #第零行和第一列的所有元素,只指定行下标,不指定列下标,表示所有列
- array([[1, 2, 3],
- [4, 5, 7]])
- >>>b[[0,2,1],[2,1,0]] #第零行第二列,第二行第一列,第一行第零列;第一个列表表示行下标,第二列表表示列下标
- array([3, 8, 4])
- >>>a=np.arange(10)
- >>>a
- array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
- >>>a[[2,4,6]] #可同时访问多个数组中的数据
- array([2, 4, 6])
- >>>x=np.random.randint(1,10,(2,3))
- >>>x
- array([[8, 2, 7],
- [6, 3, 8]])
- >>> x.take([0,4]) #第一个和第五个元素,行优先
- array([8, 3])
- >>> x.take([0,3])
- array([8, 6])
- >>> x.take([0,1],axis=0) #前两行
- array([[8, 2, 7],
- [6, 3, 8]])
- >>> x.take([0,1],axis=1) #前两列
- array([[8, 2],
- [6, 3]])
- >>> x=np.random.randint(1,10,(2,2,3))
- >>> x
- array([[[3, 6, 8],
- [8, 6, 2]],
-
- [[4, 9, 8],
- [6, 7, 5]]])
- >>> x.take(0,axis=0)
- array([[3, 6, 8],
- [8, 6, 2]])
- >>> x.take(0)
- 3
- >>> x.take(11)
- 5
- >>> x.take([0,1],axis=2)
- array([[[3, 6],
- [8, 6]],
-
- [[4, 9],
- [6, 7]]])
- >>> x.take([0,1],axis=1)
- array([[[3, 6, 8],
- [8, 6, 2]],
-
- [[4, 9, 8],
- [6, 7, 5]]])
- >>> x.take(1,axis=1)
- array([[8, 6, 2],
- [6, 7, 5]])
- >>> x[0]
- array([[3, 6, 8],
- [8, 6, 2]])
- >>> x[1]
- array([[4, 9, 8],
- [6, 7, 5]])
- >>> x[0][0]
- array([3, 6, 8])
- >>> x[0][0][2]
- 8
- >>> 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])
- >>> c=np.arange(25) #创建数组
- >>> c.shape=5,5 #修改数组形状
- >>> c
- array([[ 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]])
- >>> c[0,2:5] #第零行中下标[2,5)之间的元素值
- array([2, 3, 4])
- >>> c[1] #第一行所有元素,不指定列下标,表示所有列
- array([5, 6, 7, 8, 9])
- >>> c[2:5,2:5] #行下标和列下标都介于[2,5)之间的元素值
- array([[12, 13, 14],
- [17, 18, 19],
- [22, 23, 24]])
- >>> c[[1,3],[2,4]] #第一行第二列的元素和第三行第四列的元素
- array([ 7, 19])
- >>> c[[1,3],2:3] #第一行和第三行的二三列
- array([[ 7],
- [17]])
- >>> c[:,[2,4]] #第二列和第四列所有元素,对行下标进行切片,冒号表示所有行
- array([[ 2, 4],
- [ 7, 9],
- [12, 14],
- [17, 19],
- [22, 24]])
- >>> c[:,3] #第三列所有元素
- array([ 3, 8, 13, 18, 23])
- >>> c[[1,3]] #第一行和第三行所有元素
- array([[ 5, 6, 7, 8, 9],
- [15, 16, 17, 18, 19]])
- >>> c[[1,3]][:,[2,4]] #第一、三行的2、4列元素
- array([[ 7, 9],
- [17, 19]])
- >>> x=np.arange(0,100,10,dtype=np.floating)
- Warning (from warnings module):
- File "<pyshell#67>", line 1
- DeprecationWarning: Converting `np.inexact` or `np.floating` to a dtype is deprecated. The current result is `float64` which is not strictly correct.
- >>> print(x)
- [ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90.]
- >>> print(np.sin(x)) #一维数组中所有元素求正弦值
- [ 0. -0.54402111 0.91294525 -0.98803162 0.74511316 -0.26237485
- -0.30481062 0.77389068 -0.99388865 0.89399666]
- >>> x=np.array(([1,2,3],[4,5,6],[7,8,9]))
- >>> print(x)
- [[1 2 3]
- [4 5 6]
- [7 8 9]]
- >>> print(np.cos(x)) #二维数组中所有元素求余弦值
- [[ 0.54030231 -0.41614684 -0.9899925 ]
- [-0.65364362 0.28366219 0.96017029]
- [ 0.75390225 -0.14550003 -0.91113026]]
- >>> print(np.round(np.cos(x))) #四舍五入
- [[ 1. -0. -1.]
- [-1. 0. 1.]
- [ 1. -0. -1.]]
- >>> print(np.ceil(x/2)) #向上取整
- [[1. 1. 2.]
- [2. 3. 3.]
- [4. 4. 5.]]
- >>> np.absolute(-3) #绝对值或模
- 3
- >>> np.isnan(np.NAN)
- True
- >>> np.log2(8) #对数
- 3.0
- >>> np.log10(100)
- 2.0
- >>> np.log10([100,100,10000])
- array([2., 2., 4.])
- >>> np.multiply(3,8)
- 24
- >>> np.multiply([1,2,3],[4,5,6])
- array([ 4, 10, 18])
- >>> np.multiply(3,[5,6])
- array([15, 18])
- >>> np.multiply(3,np.array([5,6]))
- array([15, 18])
- >>> np.sqrt([9,16,35])
- array([3. , 4. , 5.91607978])
- >>> np.sqrt(range(10))
- array([0. , 1. , 1.41421356, 1.73205081, 2. ,
- 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])
- >>> x=np.arange(1,11,1)
- >>> x
- array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
- >>> x.shape #查看数组的形状
- (10,)
- >>> x.size #数组中元素的数量
- 10
- >>> x.shape=2,5 #改为2行5列
- >>> x
- array([[ 1, 2, 3, 4, 5],
- [ 6, 7, 8, 9, 10]])
- >>> x.shape
- (2, 5)
- >>> x.shape=5,-1 #-1表示自动计算
- >>> x
- array([[ 1, 2],
- [ 3, 4],
- [ 5, 6],
- [ 7, 8],
- [ 9, 10]])
- >>> x=x.reshape(2,5) #reshape()方法返回新数组
- >>> x
- array([[ 1, 2, 3, 4, 5],
- [ 6, 7, 8, 9, 10]])
- >>> x=np.array(range(5))
- >>> x.reshape((1,10)) #reshape()不能修改数组元素个数,出错
- Traceback (most recent call last):
- File "<pyshell#14>", line 1, in <module>
- x.reshape((1,10))
- ValueError: cannot reshape array of size 5 into shape (1,10)
- >>> x.resize((1,10)) #resize()可以改变数组元素个数
- >>> x
- array([[0, 1, 2, 3, 4, 0, 0, 0, 0, 0]])
- >>> np.resize(x,(1,3)) #使用Numpy的resize()返回新数组
- array([[0, 1, 2]])
- >>> x #不对原数组进行任何修改
- array([[0, 1, 2, 3, 4, 0, 0, 0, 0, 0]])
- >>> arr=np.random.randint(1,10,(3,4))
- >>> arr
- array([[9, 3, 7, 4],
- [2, 2, 4, 6],
- [7, 8, 8, 8]])
- >>> arr.ravel() #默认行优先,C语言顺序
- array([9, 3, 7, 4, 2, 2, 4, 6, 7, 8, 8, 8])
- >>> arr.ravel('F') #列优先,Fortran顺序
- array([9, 2, 7, 3, 2, 8, 7, 4, 8, 4, 6, 8])
- >>> arr.flatten() #行优先
- array([9, 3, 7, 4, 2, 2, 4, 6, 7, 8, 8, 8])
- >>> arr.flatten('F') #列优先
- array([9, 2, 7, 3, 2, 8, 7, 4, 8, 4, 6, 8])
- >>> arr=np.random.randint(1,10,(2,3,4))
- >>> arr
- array([[[4, 1, 5, 8],
- [1, 8, 3, 1],
- [2, 2, 5, 6]],
-
- [[3, 8, 5, 2],
- [5, 2, 5, 3],
- [4, 4, 1, 3]]])
- >>> arr.flatten()
- array([4, 1, 5, 8, 1, 8, 3, 1, 2, 2, 5, 6, 3, 8, 5, 2, 5, 2, 5, 3, 4, 4,
- 1, 3])
- >>> arr.flatten('F')
- array([4, 3, 1, 5, 2, 4, 1, 8, 8, 2, 2, 4, 5, 5, 3, 5, 5, 1, 8, 2, 1, 3,
- 6, 3])
- >>> np.split(np.array(range(10)),2)
- [array([0, 1, 2, 3, 4]), array([5, 6, 7, 8, 9])]
- >>> np.split(np.array(range(12)),3)
- [array([0, 1, 2, 3]), array([4, 5, 6, 7]), array([ 8, 9, 10, 11])]
- >>> np.split(np.array(range(16)).reshape((4,4)),2)
- [array([[0, 1, 2, 3],
- [4, 5, 6, 7]]), array([[ 8, 9, 10, 11],
- [12, 13, 14, 15]])]
- >>> x=np.random.rand(10)*50 #10个随机数
- >>> x
- array([16.69881646, 9.02845598, 39.62579936, 1.30399031, 42.20507989,
- 28.33553525, 10.56996153, 30.96119602, 7.90198546, 4.56703219])
- >>> np.int64(x) #取整
- array([16, 9, 39, 1, 42, 28, 10, 30, 7, 4], dtype=int64)
- >>> x-np.int64(x) #小数部分
- array([0.69881646, 0.02845598, 0.62579936, 0.30399031, 0.20507989,
- 0.33553525, 0.56996153, 0.96119602, 0.90198546, 0.56703219])
- >>> a=np.arange(0,60,10).reshape(-1,1) #列向量
- >>> a
- array([[ 0],
- [10],
- [20],
- [30],
- [40],
- [50]])
- >>> b=np.arange(0,6) #行向量
- >>> b
- array([0, 1, 2, 3, 4, 5])
- >>> a[0]+b #数组与标量的加法
- array([0, 1, 2, 3, 4, 5])
- >>> a[1]+b
- array([10, 11, 12, 13, 14, 15])
- >>> a+b #广播
- array([[ 0, 1, 2, 3, 4, 5],
- [10, 11, 12, 13, 14, 15],
- [20, 21, 22, 23, 24, 25],
- [30, 31, 32, 33, 34, 35],
- [40, 41, 42, 43, 44, 45],
- [50, 51, 52, 53, 54, 55]])
- >>> a*b #广播
- array([[ 0, 0, 0, 0, 0, 0],
- [ 0, 10, 20, 30, 40, 50],
- [ 0, 20, 40, 60, 80, 100],
- [ 0, 30, 60, 90, 120, 150],
- [ 0, 40, 80, 120, 160, 200],
- [ 0, 50, 100, 150, 200, 250]])
- >>> x=np.random.randint(0,10,7)
- >>> x
- array([0, 1, 5, 4, 1, 8, 2])
- >>> np.bincount(x) #元素出现次数,0出现1次……
- array([1, 2, 1, 0, 1, 1, 0, 0, 1], dtype=int64)
- >>> np.sum(_) #所有元素出现次数之和等于数组长度
- 7
- >>> len(x)
- 7
- >>> np.unique(x) #返回唯一元素值,把x变成集合类
- array([0, 1, 2, 4, 5, 8])
- >>> x=np.random.randint(0,10,2)
- >>> x
- array([8, 4])
- >>> np.bincount(x) #结果数组的长度取决于原始数组中最大元素值
- array([0, 0, 0, 0, 1, 0, 0, 0, 1], dtype=int64)
- >>> x=np.random.randint(0,10,10)
- >>> x
- array([3, 0, 3, 5, 4, 3, 8, 3, 0, 3])
- >>> y=np.random.rand(10) #随机小数,模拟权重
- >>> y=np.round_(y,1) #保留一位小数
- >>> np.sum(x*y)/np.sum(np.bincount(x)) #加权总和/出现总次数或元素个数
- 1.69
- >>> sum(x*y)/len(x) #数组支持python内置函数
- 1.69
- >>>import numpy as np
- >>>x=np.random.rand(10) #包含10个随机数的数组
- >>>x
- array([0.25343298, 0.41688277, 0.98967738, 0.33084737, 0.97938066,
- 0.35567276, 0.79729858, 0.94640714, 0.19360246, 0.72732656])
- >>>x>0.5 #比较数组中每个元素是否大于0.5
- array([False, False, True, False, True, False, True, True, False,
- True])
- >>>x[x>0.5] #获取数组中大于0.5的元素,可用于检测和过滤异常值
- array([0.98967738, 0.97938066, 0.79729858, 0.94640714, 0.72732656])
- >>> x<0.5
- array([ True, True, False, True, False, True, False, False, True,
- False])
- >>> sum((x>0.4)&(x<0.6)) #值大于0.4且小于0.6的元素数量,True表示1,F表示0
- 1
- >>> np.all(x<1) #测试是否全部元素都小于1
- True
- >>> np.any(x>0.8) #是否存在大于0.8的元素
- True
- >>> a=np.array([1,2,3])
- >>> b=np.array([3,2,1])
- >>> a>b #两个数组中对应位置上的元素比较
- array([False, False, True])
- >>> a[a>b] #数组a中大于b数组对应位置上元素的值
- array([3])
- >>> a==b
- array([False, True, False])
- >>> a[a==b]
- array([2])
- >>> x=np.arange(1,10)
- >>> x
- array([1, 2, 3, 4, 5, 6, 7, 8, 9])
- >>> x[(x%2==0)&(x>5)] #大于5的偶数,两个数组进行布尔与运算
- array([6, 8])
- >>> x[(x%2==0)|(x>5)] #大于5的元素或者偶数元素,布尔或运算
- array([2, 4, 6, 7, 8, 9])
- >>> data=np.array([[1,2,3],[2,3,3],[3,4,5],[1,2,3],[4,5,6],[1,2,3]])
- >>> data==[1,2,3] #每行的每元素对应比较
- array([[ True, True, True],
- [False, False, True],
- [False, False, False],
- [ True, True, True],
- [False, False, False],
- [ True, True, True]])
- >>> index=list(map(lambda row:all(row==[1,2,3]),data))
- >>> print(index)
- [True, False, False, True, False, True]
- >>> data[index] #获取所有[1,2,3]的行
- array([[1, 2, 3],
- [1, 2, 3],
- [1, 2, 3]])
- >>> x=np.random.randint(0,10,size=(1,10))
- >>> x
- array([[5, 5, 4, 1, 8, 2, 9, 3, 9, 3]])
- >>> np.where(x<5,0,1) #小于5的元素值对应0,其他对应1
- array([[1, 1, 0, 0, 1, 0, 1, 0, 1, 0]])
- >>> x.resize((2,5)) #改变数组形状
- >>> x
- array([[5, 5, 4, 1, 8],
- [2, 9, 3, 9, 3]])
- >>> np.piecewise(x,[x<4,x>7],[lambda x:x*2,lambda x:x*3]) #小于4的元素*2,大于7的元素*3,其他元素为0
- array([[ 0, 0, 0, 2, 24],
- [ 4, 27, 6, 27, 6]])
- >>> np.piecewise(x,[x<3,(3<x)&(x<5),x>7],[-1,1,lambda x:x*4]) #<3的元素变为-1,大于3的元素变为1,大于7的元素*4
- array([[ 0, 0, 1, -1, 32],
- [-1, 36, 0, 36, 0]])
- >>> data=np.random.randint(1,100,(8,5))
- >>> data
- array([[33, 66, 84, 84, 45],
- [25, 12, 47, 42, 37],
- [45, 85, 35, 98, 4],
- [80, 84, 49, 98, 18],
- [85, 93, 69, 45, 46],
- [49, 80, 19, 44, 99],
- [60, 67, 22, 35, 81],
- [69, 51, 11, 72, 57]])
- >>> np.delete(data,0,axis=0) #删除下标为0的行,返回新数组
- array([[25, 12, 47, 42, 37],
- [45, 85, 35, 98, 4],
- [80, 84, 49, 98, 18],
- [85, 93, 69, 45, 46],
- [49, 80, 19, 44, 99],
- [60, 67, 22, 35, 81],
- [69, 51, 11, 72, 57]])
- >>> np.delete(data,3,axis=1) #删除下标为3的列,返回新数组
- array([[33, 66, 84, 45],
- [25, 12, 47, 37],
- [45, 85, 35, 4],
- [80, 84, 49, 18],
- [85, 93, 69, 46],
- [49, 80, 19, 99],
- [60, 67, 22, 81],
- [69, 51, 11, 57]])
- >>> np.delete(data,3) #删除按行存储的下标为3的元素,返回一维数组
- array([33, 66, 84, 45, 25, 12, 47, 42, 37, 45, 85, 35, 98, 4, 80, 84, 49,
- 98, 18, 85, 93, 69, 45, 46, 49, 80, 19, 44, 99, 60, 67, 22, 35, 81,
- 69, 51, 11, 72, 57])
- >>> np.delete(data,np.arange(0,len(data),2)) #删除偶数下标的元素,返回一维数组
- array([66, 84, 25, 47, 42, 37, 45, 85, 35, 98, 4, 80, 84, 49, 98, 18, 85,
- 93, 69, 45, 46, 49, 80, 19, 44, 99, 60, 67, 22, 35, 81, 69, 51, 11,
- 72, 57])
- >>> np.delete(data,[0,2,6,7]) #删除下标为0,2,6,7的元素
- array([66, 84, 45, 25, 42, 37, 45, 85, 35, 98, 4, 80, 84, 49, 98, 18, 85,
- 93, 69, 45, 46, 49, 80, 19, 44, 99, 60, 67, 22, 35, 81, 69, 51, 11,
- 72, 57])
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。