python - LinearSVC 中参数 class_weight 的最佳值是多少?
问题描述
我有一个多标签数据(一些类有 2 个和一些 10 个标签),我的模型过度拟合平衡值和无值。为 class_weight 参数设置的最佳值是什么。
from sklearn.svm import LinearSVC
svm = LinearSVC(C=0.01,max_iter=100,dual=False,class_weight=None,verbose=1)
解决方案
class_weight
参数实际上通过以下方式控制参数C
:
class_weight : {dict, 'balanced'}, 可选
将第 i 类的参数 C 设置
class_weight[i]*C
为 SVC。如果没有给出,所有的类都应该有一个权重。“平衡”模式使用 y 的值自动调整与输入数据中的类频率成反比的权重,如下所示n_samples / (n_classes * np.bincount(y))
尝试class_weight
在保持C
不变的情况下使用,例如 C=0.1
编辑
class_weight
这是为您的 171 个班级创建的绝妙方法。
# store the weights for each class in a list
weights_per_class = [2,3,4,5,6]
#Let's assume that you have a `y` like this:
y = [121, 122, 123, 124, 125]
你需要:
# create the `class_weight` dictionary
class_weight = {val:weights_per_class[index] for index,val in enumerate (y)}
print(class_weight)
#{121: 2, 122: 3, 123: 4, 124: 5, 125: 6}
# Use it as argument
svm = LinearSVC(class_weight=class_weight)
推荐阅读
- python - 下载问题
- css - 如何获得关键帧以使铃声看起来好像在响铃
- jquery - Wordpress 中的 Ajax / jQuery 未运行但控制台上没有错误
- r - 如何更改 x 轴刻度
- c# - Nuget Pack 不包括新文件
- spring-cloud-dataflow - 在 kubernetes 上为 spring 云数据流任务和流部署设置节点选择器
- robotframework - 我需要使用的元素在页面上,但 Robot Framework 找不到
- php - 在 HTML 页面上忽略 PHP
- python - 将 Python 与 OpenCV 结合使用时,哪些组件会做什么?
- firebase - Flutter Cloud-functions:如何配置它以调用在 localhost 上运行的函数