python - sklearn 分类的 class_weight 字典格式
问题描述
尝试对约 2500*~4000 个大型特征进行分类,并在训练数据随附一个置信度文档。
我正在尝试将置信度值用作class_weight
分类器的参数,并且无法理解 class_weight 所需的字典格式。由于使用格式为 {0:1, 1:0.66, 2:0.66, 3:1 ...} 的字典,我一直在寻找错误的解决方案,但最近了解到该参数需要 [{ 0:1、1:1}、{0:1、1:5}、{0:1、1:1}、{0:1、1:1}] [ https://scikit-learn.org/稳定/模块/生成/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier]
我想我不明白 [{a:b , c:d}...] 的格式我相信 d 是重量,但不确定结构的其余部分或如何从我的 csv 文件中到达那里。
到目前为止我所拥有的:
>>> with open('confidence.csv') as csvfile:
>>> reader = csv.DictReader(csvfile, delimiter=",")
>>> confidence_dict={int(row['ID'])-1:int(float(row['confidence'])) for row in reader} #float(row['confidence'])
>>> print(confidence_dict)
{0: 0.66, 1: 1, 2: 0.66, 3: 0.66, 4: 1, ...}
>>> print(X)
v0 v1 v2 v3 ...
0 1.413 0.874 0.506 1.790
1 0.253 0.253 0.486 1.864
2 1.863 0.174 0.018 1.789
3 0.253 0.213 0.486 1.834
...
>>> print(y)
0 0
1 0
2 1
3 1
...
>>> linearSVC = LinearSVC(random_state=0, tol=1e-6, class_weight=confidence_dict)
>>> linearSVC.fit(X, y)
Class label {} not present.
尝试使用当前字典形式中的类权重时返回。如果没有输入班级权重,则不会发生这种情况。
ValueError: Class label 2 not present.
网上关于这个话题的信息有限,所以我想我会试着写一个清晰的帖子,希望能掌握如何实现这个。
解决方案
经过吉米在评论中的一些进一步研究和指导,我了解到我错误地认为输入需要以表格形式出现
dict({x1, x2, x3,...xn})
其中 x 是该预测的置信度。class_weight 需要是字典形式
dict({0:y0, 1:z0}, {0:y1, 1:z2}, {0:y1, 1:z1},...)
其中 y 是结果 0 的置信度/权重,z 是结果 1 的置信度/权重。
这就是ValueError: Class label 2 not present.
发生的原因。它正在寻找下一本词典
推荐阅读
- javascript - 从 Flickity 滑块过渡中删除淡入淡出
- python - networkx ImportError:无法导入名称_distributor_init
- javascript - 图片未在点击时下载,而不是打开图片的 URL
- elasticsearch - Xpack 是否必须用于加密的跨集群搜索
- django - 在 Form/Formwizard 中动态填充 ChoiceField Select
- c# - 有没有办法从一组具有不同值类型的字典中通用地返回一个值?
- git - git-svn 克隆所有历史记录,但只克隆当前在 HEAD 版本中的内容
- asp.net-mvc - 当日期列等于特定值时如何以不同方式显示?
- android - Google 地图如何在不使用前台服务的情况下知道我的位置?
- swift - 将 [UInt8] 的部分转换为 UInt8