我有一个关于 (N,) 维数组和 (N,1) 维数组之间转换的问题。例如,y 是 (2,) 维。
A=np.array([[1,2],[3,4]])
x=np.array([1,2])
y=np.dot(A,x)
y.shape
Out[6]: (2,)
但下面将显示 y2 为 (2,1) 维度。
x2=x[:,np.newaxis]
y2=np.dot(A,x2)
y2.shape
Out[14]: (2, 1)
在不复制的情况下将 y2 转换回 y 的最有效方法是什么?
谢谢,
汤姆
reshape http://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html为此工作
a = np.arange(3) # a.shape = (3,)
b = a.reshape((3,1)) # b.shape = (3,1)
b2 = a.reshape((-1,1)) # b2.shape = (3,1)
c = b.reshape((3,)) # c.shape = (3,)
c2 = b.reshape((-1,)) # c2.shape = (3,)
还请注意reshape
除非需要新形状,否则不会复制数据(此处不需要这样做):
a.__array_interface__['data'] # (22356720, False)
b.__array_interface__['data'] # (22356720, False)
c.__array_interface__['data'] # (22356720, False)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)