-
使用mat创建矩阵
a = np.mat([[5, 6], [7, 8]]) b = np.mat([[1, 2], [3, 4]]) print(a) print(b) print(type(a)) print(type(b))
-
矩阵的加减乘除运算
data1 = np.mat([[1, 2], [3, 4], [5, 6]]) data2 = np.mat([1, 2]) data3 = np.mat([[5, 6], [7, 8]]) print(data1 + data2) print(data1 - data2) print(data1 / data2) print(data1 * data3)
-
数组点乘运算
n1 = np.array([1, 2, 3]) n2 = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) print(np.multiply(n1, n2))
-
矩阵求转置
n1 = np.mat([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print('矩阵转置结果为:\n', n1.T)
-
矩阵求逆
n1 = np.mat([[1, 3, 3], [4, 5, 6], [7, 15, 9]]) print('矩阵的逆矩阵结果为:\n', n1.I)
-
使用Numpy内置函数进行数组加减乘除
n1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) n2 = np.array([10, 10, 10]) print('两个数组相加:') print(np.add(n1, n2)) print('两个数组相减:') print(np.subtract(n1, n2)) print('两个数组相乘:') print(np.multiply(n1, n2)) print('两个数组相除:') print(np.divide(n1, n2))
-
幂运算
n1 = np.array([10, 100, 1000]) print(np.power(n1, 3))
-
数组元素行列求和
n = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print('对数组元素求和:') print(n.sum()) print('对数组元素按列求和:') print(n.sum(axis=0)) print('对数组元素按行求和:') print(n.sum(axis=1))
-
数组元素求平均值
n = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print('对数组元素求平均值:') print(n.mean()) print('对数组元素按列求平均值:') print(n.mean(axis=0)) print('对数组元素按行求平均值:') print(n.mean(axis=1))
-
求最大最小值
n = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print('数组元素最大值:') print(n.max()) print('数组中每一列的最大值:') print(n.max(axis=0)) print('数组中每一行的最大值:') print(n.max(axis=1)) print('数组元素最小值:') print(n.min()) print('数组中每一列的最小值:') print(n.min(axis=0)) print('数组中每一行的最小值:') print(n.min(axis=1))
-
求数组中位数
n = np.array([34.5, 36, 37.8, 39, 39.8, 33.6]) # 数组排序后,查找中位数 sort_n = np.sort(n) print('数组排序:') print(sort_n) print('数组中位数为:') print(np.median(sort_n))
-
求数组方差标准差
n = np.array([34.5, 36, 37.8, 39, 39.8, 33.6]) print('数组方差:') print(np.var(n)) print('数组标准差:') print(np.std(n))
-
数组排序
n = np.array([[4, 7, 3], [2, 8, 5], [9, 1, 6]]) print('数组排序:') print(np.sort(n)) print('按列排序:') print(np.sort(n, axis=0)) print('按行排序:') print(np.sort(n, axis=1))