我已经定义了一个函数,用以前由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
发布于 2020-06-03 10:03:59
这是错误的:
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)
...
https://stackoverflow.com/questions/62153233
复制相似问题