一. ndarray的聚合操作
- 求和np.sum
n = np.arange(11)
n
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
np.sum(n)
55
n = np.random.randint(0,100, size=(5,6))
n
array([[62, 65, 17, 21, 90, 66],
[80, 1, 54, 60, 57, 40],
[19, 30, 97, 92, 8, 41],
[ 0, 55, 28, 10, 20, 48],
[64, 44, 94, 25, 3, 17]])
np.sum(n, axis=1)
array([321, 292, 287, 161, 247])
np.sum(n,axis = 0)
array([225, 195, 290, 208, 178, 212])
这里的axis参数有0和1 ,当axis=1的时候,得到是行元素相加的和,当axis=0的时候,所加的和是列元素所加的和.
- 最大最小值平均值:np.max/ np.min/np.mean
n = np.arange(11)
n
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
np.mean(n)
5.0
n = np.random.randint(0,100,size=10)
n
array([ 7, 61, 53, 89, 10, 4, 11, 33, 79, 10])
np.mean(n)
35.7
np.max(n)
89
np.min(n)
4
n = np.random.randint(0,100, size=(5,6))
n
array([[82, 44, 0, 33, 72, 99],
[66, 25, 36, 88, 74, 78],
[ 3, 53, 33, 76, 96, 69],
[62, 10, 16, 22, 12, 31],
[41, 57, 43, 79, 34, 7]])
np.max(n, axis=0)
array([82, 57, 43, 88, 96, 99])
找出每一列最小元素的索引值(index)
np.argmin(n, axis=0)
array([2, 3, 0, 3, 3, 4], dtype=int64)
- 其他的聚合操作
Function Name NaN-safe Version Description
np.sum np.nansum Compute sum of elements
np.prod np.nanprod Compute product of elements
np.mean np.nanmean Compute mean of elements
np.std np.nanstd Compute standard deviation
np.var np.nanvar Compute variance
np.min np.nanmin Find minimum value
np.max np.nanmax Find maximum value
np.argmin np.nanargmin Find index of minimum value
np.argmax np.nanargmax Find index of maximum value
np.median np.nanmedian Compute median of elements
np.percentile np.nanpercentile Compute rank-based statistics of elements
np.any N/A Evaluate whether any elements are true
np.all N/A Evaluate whether all elements are true
np.power 幂运算
np.sum 和 np.nansum 的区别 nan not a number
a = np.array([1,2,np.nan])
a
array([ 1., 2., nan])
np.nansum(a)
3.0
二. ndarray的矩阵操作
- 算数运算符
- 加减乘除
n = np.random.randint(0,10, size=(4,5))
n
array([[2, 5, 0, 4, 6],
[0, 0, 7, 5, 0],
[6, 3, 2, 9, 2],
[5, 7, 0, 4, 5]])
# 加
n + 1
array([[ 3, 6, 1, 5, 7],
[ 1, 1, 8, 6, 1],
[ 7, 4, 3, 10, 3],
[ 6, 8, 1, 5, 6]])
# 减
n - 1
array([[ 1, 4, -1, 3, 5],
[-1, -1, 6, 4, -1],
[ 5, 2, 1, 8, 1],
[ 4, 6, -1, 3, 4]])
# 两个矩阵相加
n2 = np.random.randint(0,10,size=(4,5))
n2
array([[2, 4, 2, 5, 9],
[6, 6, 9, 6, 2],
[9, 7, 5, 6, 1],
[4, 6, 7, 2, 9]])
n + n2
array([[ 4, 9, 2, 9, 15],
[ 6, 6, 16, 11, 2],
[15, 10, 7, 15, 3],
[ 9, 13, 7, 6, 14]])
n3 = np.random.randint(0,10,size=(2,5))
n3
array([[8, 0, 0, 5, 8],
[4, 0, 3, 6, 7]])
n2 + n3

注意: 当两个矩阵行和列不相等的时候,不能做相加减.
矩阵积np.dot()
n1 = np.random.randint(0,10,size=(2,3))
n1
array([[6, 7, 7],
[2, 3, 9]])
n2 = np.random.randint(0,10, size=(3,5))
n2
array([[0, 3, 3, 3, 7],
[3, 4, 0, 6, 1],
[1, 3, 2, 1, 4]])
np.dot(n1,n2)
array([[28, 67, 32, 67, 77],
[18, 45, 24, 33, 53]])
两个矩阵运算矩阵积 需要n1的列与n2的行相等才能运算.
如图:

【重要】ndarray广播机制的两条规则
- 规则一:为缺失的维度补1
- 规则二:假定缺失元素用已有值填充
例1: m = np.ones((2, 3)) a = np.arange(3) 求m+a
m = np.ones((2,3),dtype=int)
m
array([[1, 1, 1],
[1, 1, 1]])
a = np.arange(3)
a
array([0, 1, 2])
这时候广播机制发挥作用将a的维度补1,并且值用已经有的值填充,就变成:
array([[0, 1, 2],
[0, 1, 2]])
m + a
array([[1, 2, 3],
[1, 2, 3]])
例2: a = np.arange(3).reshape((3, 1)) b = np.arange(3) 求a+b
a = np.arange(3).reshape((3,1))
a
array([[0],
[1],
[2]])
广播后:
array([[0,0,0],
[1,1,1],
[2,2,2]])
b = np.arange(3)
b
array([0, 1, 2])
广播后:
array([[0, 1, 2],
[0, 1, 2],
[0, 1, 2]])
然后各个元素相加,是不是很清晰明了.
a + b
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4]])
三.ndarry的排序
小测验: 使用以上所学numpy的知识,对一个ndarray对象进行选择排序。
def Sortn(x):
代码越短越好
#冒泡排序
n = [5,2,3,6,9]
def bubble(n):
for i in range(len(n) -1):
for j in range(i+1, len(n)):
if n[i] > n[j]:
n[i], n[j] = n[j], n[i]
bubble(n)
n
[2, 3, 5, 6, 9]
# 选择排序
def select(n):
for i in range(len(n)):
# 选出最小值的索引
index = np.argmin(n[i:]) + i
# 把最小值和当前值的位置换一下
n[i], n[index] = n[index], n[i]
n = [4, 6,1,0,3]
select(n)
n
[0, 1, 3, 4, 6]
快速排序
np.sort()与ndarray.sort()都可以,但有区别:
- np.sort()不改变输入
-
ndarray.sort()本地处理,不占用空间,但改变输入
image.png
部分排序:
np.partition(a,kth)
有的时候我们不是对全部数据感兴趣,我们可能只对最小或最大的一部分感兴趣。
当kth为正时,我们想要得到最小的kth个数
当kth为负时,我们想要得到最大的kth个数
网友评论