亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

編寫一個名為 randomization 的函數,該函數將正整數 n 作為輸入,并返回 A

編寫一個名為 randomization 的函數,該函數將正整數 n 作為輸入,并返回 A

夢里花落0921 2022-08-02 10:36:35
編寫一個名為 randomization 的函數,該函數將正整數 n 作為輸入,并返回 A,即隨機 n x 1 numpy 數組。以下是我所擁有的,但它不起作用。import numpy as npdef randomization(n):    if n>0 and n==int:        A=np.random.random([n, 1])    print(A)x=int(input("enter a positive number: "))r=randomization(x)print(r)如果我運行這個,我會收到一條消息,說“賦值前引用了局部變量'A'”。
查看完整描述

4 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

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


查看完整回答
反對 回復 2022-08-02
?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

首先,將始終為假,因為不是類型。請改用。n == intnintisinstance(n, int)

因此,永遠不會被分配,但隨后您調用就好像它被分配了樣。Aprint(A)


查看完整回答
反對 回復 2022-08-02
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

我認為你正在尋找的是這樣的東西。必須定義函數,將 A 設置為隨機矩陣 nx1,然后在定義中返回值 A。

      A = np.random.random([n,1])
            return A


查看完整回答
反對 回復 2022-08-02
?
不負相思意

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)*


查看完整回答
反對 回復 2022-08-02
  • 4 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號