我已經定義了一個函數,用以前由 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)
...
添加回答
舉報
0/150
提交
取消