首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如果是python中的字符串语句

如果是python中的字符串语句
EN

Stack Overflow用户
提问于 2018-09-10 03:40:38
回答 1查看 0关注 0票数 0

I am doing a nano degree as a data analyst. So quite a green hand. In a hypothesis testing assignment I wrote this code:

Error_1 = 0
Error_2 = 0
Correct = 0

for x in jud_data['actual']:
    y= jud_data['predicted']
    if x == y :
        Correct +=1,
    elif x == 'innocent' & y == 'guilty' :
        Error_1 += 0,
    elif x == 'guilty' & y == 'innocent' :
        Error_2 += 0,

print ('Error type 1 = ', Error_1)
print ('Error type 2 =', Error_2)
print ('Correct =', Correct)

I got this error message ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What is wrong with this code? Knowing that the types of jud_data['actual'] and jud_data['predicted'] is object

Extra information:- I am using pandas

The Jud_data file looks like this

defendant_id actual predicted 0 22574 innocent innocent 1 35637 innocent innocent 2 39919 innocent innocent 3 29610 guilty guilty 4 38273 innocent innocent 5 24461 innocent innocent 6 34327 guilty guilty 7 33406 guilty guilty 8 21355 innocent innocent 9 36240 guilty guilty

我想计算1和2类错误。

我知道可能有其他方法可以做我想要的。但是,我想知道为什么这段代码不起作用。它应该工作......不是吗?

EN

回答 1

Stack Overflow用户

发布于 2018-09-10 12:56:53

Error_1 = 0
Error_2 = 0
Correct = 0

for x in jud_data['actual']:
    y= jud_data['predicted']
    if x == y :
        Correct += 1


print ('Error type 1 = ', Error_1)
print ('Error type 2 =', Error_2)
print ('Correct =', Correct)

我不确定你的代码的意图。但是,如果我做对了,这似乎工作正常。

  1. 没有必要将0加到现有的数字变量上
  2. 看起来你用“&”而不是“和”。
  3. 某些代码中不必要的“,”(添加后)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档