我定义了一个函数,它用以前由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
这是错误的:
您希望这样做:
不知道下面发生了什么,为什么要用双方括号将x括起来?
另外,为什么要将某些内容设置为
prediction1
,并立即在下面的行中覆盖它相关问题 更多 >
编程相关推荐