H5W3
当前位置:H5W3 > 其他技术问题 > 正文

python随机产生0和1,得出F1-score的问题,求大神帮忙

import numpy as np
array_P=[np.random.randint(0,2,10)]
array_A=[np.random.randint(0,2,10)]
代码如下
print(array_P)
print(array_A)
TP=0
FP=0
FN=0
for i in range(10):
if array_P[i]==1 and array_A[i]==1:
TP+=1
elif array_P[i]==1 and array_A[i]==1:
FP+=1
elif array_P[i]==0 and array_A[i]==1:
FN+=1
print(TP)
print(FP)
print(FN)
P=TP/(TP+FP)
R=TP/(TP+FN)
fl_score = (2*R*P)/(R+P)
print(‘name:’, fl_score)
报错
runfile(‘D:/python/files/未命名0.py’)
[array([1, 0, 1, 1, 0, 0, 0, 0, 1, 0])]
[array([1, 1, 0, 1, 1, 0, 1, 0, 0, 1])]
Traceback (most recent call last):

File “D:\python\files\未命名0.py”, line 30, in
if array_P[i]==1 and array_A[i]==1:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

回答

array_P=[np.random.randint(0,2,10)]
array_A=[np.random.randint(0,2,10)]
->
array_P=np.random.randint(0,2,10).tolist()
array_A=np.random.randint(0,2,10).tolist()
加上方括号把array变成数组的数组了

本文地址:H5W3 » python随机产生0和1,得出F1-score的问题,求大神帮忙

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址