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

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

將函數應用于多維數組

將函數應用于多維數組

侃侃無極 2022-12-20 15:25:41
我已經定義了一個函數,用以前由 LabelEncoder 生成的變量替換數組中的變量。我讓它適用于一維數組,但我現在想讓它適用于帶有 for 循環的多維數組。但是 for 循環似乎沒有按照我預期的方式遍歷行。我認為這是一個簡單的錯誤,但我們將不勝感激任何建議。這是我當前的代碼:def new_data (i):    for x in i:        x[0] = np.where(x[0] =='40-49', 2, x[0])        x[0] = np.where(x[0] == '50-59', 3, x[0])       #Etc for each item        prediction = classifier.predict([[x]])        prediction1 = np.where(prediction > 0.6, 1, prediction)        prediction1 = np.where(prediction <= 0.6, 0, prediction)        if prediction1 == 0:            return 'Prediction: No recurrence-events, with a confidence of: ' + str(prediction)        else:            return 'Prediction: Recurrence-events, with a confidence of: ' + str(prediction)輸入將是:new_predict = np.array(['40-49', 'ge40', '25-29', '6-8'],                        ['40-49', 'ge40', '25-29', '6-8'],                        ['40-49', 'ge40', '25-29', '6-8'])new_data(new_predict)然后我希望有一個輸出,例如:Prediction: No recurrence-events, with a confidence of: [prediction]Prediction: No recurrence-events, with a confidence of: [prediction]Prediction: No recurrence-events, with a confidence of: [prediction]其中每個預測都與數組中的一行相關。但我目前只收到類型錯誤,而不是遍歷數組的組件。Traceback (most recent call last):  File "/Users/Tom/MSc Project/venv/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code    exec(code_obj, self.user_global_ns, self.user_ns)  File "<ipython-input-19-fcc97a6a2285>", line 1, in <module>    new_predict = np.array(['40-49', 'ge40', '25-29', '6-8', 'yes', 2, 'right', 'right_up', 'no'],['40-49', 'ge40', '25-29', '6-8', 'yes', 2, 'right', 'right_up', 'no']TypeError: data type not understood
查看完整描述

1 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

這是錯誤的:


new_predict = np.array(['40-49', 'ge40', '25-29', '6-8'], 

                       ['40-49', 'ge40', '25-29', '6-8'], 

                       ['40-49', 'ge40', '25-29', '6-8'])

你想要這個:


new_predict = np.array([['40-49', 'ge40', '25-29', '6-8'], 

                       ['40-49', 'ge40', '25-29', '6-8'], 

                       ['40-49', 'ge40', '25-29', '6-8']])

...

不確定下面發生了什么,為什么要將 x 包裹在雙方括號中?

另外,你為什么要設置一些東西prediction1并立即在下面的行中覆蓋它?


...

prediction = classifier.predict([[x]])

prediction1 = np.where(prediction > 0.6, 1, prediction)

prediction1 = np.where(prediction <= 0.6, 0, prediction)

...


查看完整回答
反對 回復 2022-12-20
  • 1 回答
  • 0 關注
  • 92 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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