参考链接: Python中的numpy.divide
1.基本的矩阵操作:
'''1.算数运算符:加减乘除'''
n1 = np.random.randint(0, 10, size=(4, 5))
print(n1)
n2 = n1 10 # 对n1进行加法(减法,乘法,除法是一样的用法)
print(n2)
'''2.利用方法:加(np.add())减(np.subtract())乘(np.multiply())除(np.divide())'''
n1_add = np.add(n1, 10)
print("加的方法结果为:", n1_add)
n1_subtract = np.subtract(n1, 3)
print("减的方法结果为:", n1_subtract)
n1_multiply = np.multiply(n1, 2)
print("乘的方法结果为:", n1_multiply)
n1_divide = np.divide(n1, 2)
print("除的方法结果为:", n1_divide)
'''3.矩阵积'''
a = np.random.randint(0,10,size=(2,3))
b = np.random.randint(0,10,size=(3,2))
print(a)
print(b)
c_dot = np.dot(a,b) # 给a与b求矩阵积
print("a与b的矩阵积:",c_dot)
矩阵积的具体算法:
'''4.广播机制
ndarray两条规则:
·规则一: 为缺失的维度补1 (1代表的是补了1行或者1列)
·规则二:假定缺失元素用已有值填充
'''
n1 = np.ones((2,3))
n2 = np.arange(3)
print("n1:",n1)
print("n2:",n2)
'''numpy的广播机制,维度不对应,自动补全'''
print("n1 n2:",n1 n2)