为了账号安全,请及时绑定邮箱和手机立即绑定

将函数应用于多维数组

将函数应用于多维数组

侃侃无极 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 关注
  • 130 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号