返回标量时保持为numpy数组类型

时间:2016-06-17 21:49:08

标签: python arrays numpy types scalar

数组a = np.array(1)很方便,因为它允许a.shape **

如何在执行返回标量的操作时保留numpy类类型,例如

b = 0.

c = np.dot(a.T, a)

b+=c

b.shape
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-19-82ed078d10e8> in <module>()
----> 1 b.shape

AttributeError: 'float' object has no attribute 'shape'

**在应该返回标量的复杂操作期间进行维度检查时非常有用(例如,多索引张量求和)

0 个答案:

没有答案