我已经定义了一个函数,用以前由 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
提交
取消
