4 回答

TA貢獻1818條經驗 獲得超8個贊
除了chepner所說的之外,np.random.rand期望維度作為參數而不是列表。也就是說,您應該使用 A=np.random.rand(n, 1)。請注意,這將返回均勻分布的隨機向量。此外,函數不返回任何值。use - 在末尾返回 A。

TA貢獻1818條經驗 獲得超11個贊
首先,將始終為假,因為不是類型。請改用。n == int
n
int
isinstance(n, int)
因此,永遠不會被分配,但隨后您調用就好像它被分配了一樣。A
print(A)

TA貢獻1851條經驗 獲得超5個贊
我認為你正在尋找的是這樣的東西。必須定義函數,將 A 設置為隨機矩陣 nx1,然后在定義中返回值 A。
A = np.random.random([n,1]) return A

TA貢獻1777條經驗 獲得超10個贊
嘗試使用這個。請確保您的縮進正確無誤:
def operations(h,w):
"""
Takes two inputs, h and w, and makes two Numpy arrays A and B of size
h x w, and returns A, B, and s, the sum of A and B.
Arg:
h - an integer describing the height of A and B
w - an integer describing the width of A and B
Returns (in this order):
A - a randomly-generated h x w Numpy array.
B - a randomly-generated h x w Numpy array.
s - the sum of A and B.
"""
A = np.random.random([h,w])
B = np.random.random([h,w])
s = A + B
return A,B,s
A,B,s = operations(3,4)
assert(A.shape == B.shape == s.shape)*
添加回答
舉報