python编译报错 ImportError: cannot import name ‘jaccard

python编译报错 ImportError: cannot import name ‘jaccard,第1张

 

报错如下:

问题代码:

import numpy as np
from sklearn.metrics import jaccard_similarity_score
y_pred = [0, 2, 1, 3]
y_true = [0, 1, 2, 3]
print (jaccard_similarity_score(y_true, y_pred))

一开始以为是scikit-learn安装出了问题,尝试直接定义jaccard,修改sklearn为scikit-learn都不行,找到好久才发现是引入的包的文件名不对。

报错:from sklearn.metrics import jaccard_similarity_score。

原因:新的scikit-learn不再自动修改语法。通过路径找到存放jaccard的py文件,发现其中的jaccard函数名称为jaccard_score

修改:将 from sklearn.metrics import jaccard_similarity_score改为 from sklearn.metrics import jaccard_score

修改后的代码:

import numpy as np
from sklearn.metrics import jaccard_score
y_pred = [0, 2, 1, 3]
y_true = [0, 1, 2, 3]
print (jaccard_score(y_true, y_pred))

仍然有报错:ValueError: Target is multiclass but average='binary'. Please choose another average setting, one of [None, 'micro', 'macro', 'weighted'].

原因:二分类时average参数默认是binary;多分类时,可选参数有micro、macro、weighted和samples。

修改:将jaccard_score(y_true, y_pred)改为jaccard_score(y_true, y_pred,average='micro)

编译后发现能正常运行了

import numpy as np
from sklearn.metrics import jaccard_score
y_pred = [0, 2, 1, 3]
y_true = [0, 1, 2, 3]
print (jaccard_score(y_true, y_pred,average='micro'))

运行结果

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/917385.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存