本文共 2473 字,大约阅读时间需要 8 分钟。
import numpy as npa =np.array([1,2,3])b = np.array([4,2,6])print(a*b)# 结果 array([ 4, 4, 18])c = np.array([[1],[2],[3]])d = np.array([[4],[2],[6]])print(c*d)# array([[ 4],# [ 4],# [18]])>>> e array([[ 4, 8, 12], [ 2, 4, 6], [ 6, 12, 18]])>>> f array([[2, 3, 4], [2, 4, 6], [6, 3, 7]])>>> e*f array([[ 8, 24, 48], [ 4, 16, 36], [ 36, 36, 126]])
>>> aarray([1, 2, 3])>>> darray([[4], [2], [6]])>>> a*darray([[ 4, 8, 12], [ 2, 4, 6], [ 6, 12, 18]])
>>> h array([[2, 3], [3, 5], [4, 6]])>>> g array([[2, 3, 5], [3, 5, 6]])>>> g*h Traceback (most recent call last): File "", line 1, in g*hValueError: operands could not be broadcast together with shapes (2,3) (3,2) >>> h*g Traceback (most recent call last): File " ", line 1, in h*gValueError: operands could not be broadcast together with shapes (3,2) (2,3)
>>> h array([[2, 3], [3, 5], [4, 6]])>>> i array([[3], [6], [8]])>>> h*i array([[ 6, 9], [18, 30], [32, 48]])>>> i*h array([[ 6, 9], [18, 30], [32, 48]])>>> h array([[2, 3], [3, 5], [4, 6]])>>> g = np.array([3,4]) >>> h*g array([[ 6, 12], [ 9, 20], [12, 24]])>>> g*h array([[ 6, 12], [ 9, 20], [12, 24]])
首先需要说明的是,如果你的array是行向量。那么前两个转置方法是没有用的。如果是列向量或者矩阵,要转置成为行向量,那么前两个是有用的。示例如下:
>>> b array([4, 2, 6])>>> b.T array([4, 2, 6])>>> b.transpose() array([4, 2, 6])>>> c array([[1], [2], [3]])>>> c.T array([[1, 2, 3]])>>> c.transpose() array([[1, 2, 3]])>>> h array([[2, 3], [3, 5], [4, 6]])>>> h.T array([[2, 3, 4], [3, 5, 6]])>>> h.transpose() array([[2, 3, 4], [3, 5, 6]])
其原型如下:
numpy.reshape(a, newshape, order='C')[source]¶# a:待reshape的数组# newshape: 新数组的样式,(x,y), 表示x行,y列,如果y为-1,那么它会根据原数组中数据的数量,以及行x的数量,自动计算y的值。
使用示例:
import numpy as npa = np.array([1,2,3]);b = np.reshape(a,(3, -1));print(b)#结果:[[1]# [2]# [3]]
Q = np.array([[-0.6, -0.8], [-0.8, 0.6]])np.linalg.inv(Q)
转载地址:http://ewbtb.baihongyu.com/