numpy中矩阵是否与某个值相等:
matrix = numpy.array([1,2,3,4])
print (matrix==3)
matrix = numpy.array([[1,2,3,4],[2,4,6,8],[3,6,9,12]])
print(matrix)
equal_to_4 = (matrix[:,1]==4)
print(equal_to_4)
print (matrix[equal_to_4,:])
可以使用boolean值还取出矩阵中满足条件的值。
与或操作:
与:
matrix = numpy.array([5,10,15,20])
equal_5and10 = (matrix==5)&(matrix==10)
print(equal_5and10)
或:
matrix = numpy.array([5,10,15,20])
equal_5and10 = (matrix==5)|(matrix==10)
print(equal_5and10)
求最值:
matrix = numpy.array([[1,2,3,4],[4,6,2,0]])
print (matrix.max())
print (matrix.min())
求和:
matrix = numpy.array([[1,2,3,4],[4,6,2,0],[8,3,5,4]])
print (matrix.sum(axis=0)) #竖着加
print (matrix.sum(axis=1)) #横着加
矩阵加减法:
matrix_a = numpy.array([10,20,30,40])
matrix_b = numpy.arange(4)
print (matrix_a+matrix_b)
print (matrix_a+1)
print (matrix_b**2)
print (matrix_a < 22)
矩阵乘法:
a = numpy.array([[1,4],
[3,2]])
b = numpy.array([[2,3],
[5,6]])
print (a*b) #对应位置相乘
print (a.dot(b)) #矩阵乘法,一行乘一列再相加
print (numpy.dot(a,b)) #与上面一样只是写法不同
e与根号:
matrix = numpy.arange(4)
print (matrix)
print (numpy.exp(matrix))
print (numpy.sqrt(matrix)