Within numpy.ndarray.reshape https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.reshape.html, the shape
参数是一个整数或整数元组, and
新形状应与原始形状兼容。如果
整数,那么结果将是该长度的一维数组。
文档签名只是:
# Note this question doesn't apply to the function version, `np.reshape`
np.ndarray.reshape(shape, order='C')
实际上,规范似乎并不那么严格。根据上面的描述,我预计需要使用:
import numpy as np
a = np.arange(12)
b = a.reshape((4,3)) # (4,3) is passed to `newshape`
但相反,我可以通过以下方式逃脱:
c = a.reshape(4,3) # Seems like just 4 would be passed to `newshape`
# and 3 would be passed to next parameter, `order`
print(np.array_equal(b,c))
# True
我怎么能做到这一点?我知道如果我只是简单地输入2, 3
在 Python shell 中,从技术上讲,无论我是否使用括号,它都是一个元组。但上面的比较似乎违反了位置参数如何传递到关键字 args 的字典的基本法则。 IE。:
def f(a, b=1, order='c'):
print(a)
print(b)
f((4,3))
print()
f(4,3)
# (4, 3)
# 1
#
# 4
# 3
...并且没有明星运营商reshape
。 (类似于def f(*a, order='c')
above.)