a = np.array([[1]])
print(a)
a = np.squeeze(a) # To make sure your cost's shape is what we expect (e.g. this turns [[17]] into 17).
print('a:',a)
print(a.shape)
assert(a.shape == ())
b = np.array(1)
print('b:',b)
print(b.shape)
print(a)
a = np.squeeze(a) # To make sure your cost's shape is what we expect (e.g. this turns [[17]] into 17).
print('a:',a)
print(a.shape)
assert(a.shape == ())
b = np.array(1)
print('b:',b)
print(b.shape)
assert(b.shape == ())
看样子应该np.squeeze转化为数,类型仍旧是ndarray,
如果b = 1
print(b.shape),则会出错
AttributeError: 'int' object has no attribute 'shape'
NumPy数组形状调整
本文通过实例演示了如何使用NumPy中的squeeze函数来调整数组的形状,并解释了当数组元素为单个数值时如何处理其形状属性。同时对比了NumPy数组与普通Python整数在属性上的区别。
3175

被折叠的 条评论
为什么被折叠?



