数据不平衡处理_如何处理多类不平衡数据说不可以

数据不平衡处理

重点 (Top highlight)

One of the common problems in Machine Learning is handling the imbalanced data, in which there is a highly disproportionate in the target classes.

机器学习中的常见问题之一是处理不平衡的数据,其中目标类别的比例非常不均衡。

Hello world, this is my second blog for the Data Science community. In this blog, we are going to see how to deal with the multiclass imbalanced data problem.

大家好,这是我的第二本面向数据科学社区的博客 。 在此博客中,我们将看到如何处理多类不平衡数据问题。

什么是多类不平衡数据? (What is Multiclass Imbalanced Data?)

When the target classes (two or more) of classification problems are not equally distributed, then we call it Imbalanced data. If we failed to handle this problem then the model will become a disaster because modeling using class-imbalanced data is biased in favor of the majority class.

当分类问题的目标类别(两个或多个)没有平均分布时,我们称其为不平衡数据。 如果我们不能解决这个问题,那么该模型将成为灾难,因为使用类不平衡数据进行建模会偏向多数类。

There are different methods of handling imbalanced data, the most common methods are Oversampling and creating synthetic samples.

处理不平衡数据的方法多种多样,最常见的方法是过采样和创建合成样本。

什么是SMOTE? (What is SMOTE?)

SMOTE is an oversampling technique that generates synthetic samples from the dataset which increases the predictive power for minority classes. Even though there is no loss of information but it has a few limitations.

SMOTE是一种过采样技术,可从数据集中生成合成样本,从而提高了少数群体的预测能力。 即使没有信息丢失,它也有一些局限性。

Image for post
Synthetic Samples
合成样品

Limitations:

局限性:

  1. SMOTE is not very good for high dimensionality data

    SMOTE对于高维数据不是很好
  2. Overlapping of classes may happen and can introduce more noise to the data.

    类的重叠可能会发生,并可能给数据带来更多的噪音。

So, to skip this problem, we can assign weights for the class manually with the ‘class_weight’ parameter.

因此,要跳过此问题,我们可以使用' class_weight '参数为该类手动分配权重。

为什么要使用班级重量? (Why use Class weight?)

Class weights modify the loss function directly by giving a penalty to the classes with different weights. It means purposely increasing the power of the minority class and reducing the power of the majority class. Therefore, it gives better results than SMOTE.

类权重通过对具有不同权重的类进行惩罚来直接修改损失函数。 这意味着有目的地增加少数群体的权力,并减少多数阶级的权力。 因此,它比SMOTE提供更好的结果。

概述: (Overview:)

I aim to keep this blog very simple. We have a few most preferred techniques for getting the weights for the data which worked for my Imbalanced learning problems.

我的目的是使这个博客非常简单。 我们有一些最优选的技术来获取对我的失衡学习问题有用的数据权重。

  1. Sklearn utils.

    Sklearn实用程序。
  2. Counts to Length.

    数到长度。
  3. Smoothen Weights.

    平滑权重。
  4. Sample Weight Strategy.

    样品重量策略。

1. Sklearn实用程序: (1. Sklearn utils:)

We can get class weights using sklearn to compute the class weight. By adding those weight to the minority classes while training the model, can help the performance while classifying the classes.

我们可以使用sklearn计算班级权重。 通过在训练模型时将这些权重添加到少数类中,可以在对类进行分类的同时帮助提高性能。

from sklearn.utils import class_weightclass_weight = class_weight.compute_class_weight('balanced,
np.unique(target_Y),
target_Y)model = LogisticRegression(class_weight = class_weight)
model.fit(X,target_Y)# ['balanced', 'calculated balanced', 'normalized'] are hyperpaameters whic we can play with.

We have a class_weight parameter for almost all the classification algorithms from Logistic regression to Catboost. But XGboost has scale_pos_weight for binary classification and sample_weights (refer 4) for both binary and multiclass problems.

对于从Logistic回归到Catboost的几乎所有分类算法,我们都有一个class_weight参数。 但是XGboost具有用于二进制分类的scale_pos_weight和用于二进制和多类问题的sample_weights(请参阅4)。

2.数长比: (2. Counts to Length Ratio:)

Very simple and straightforward! Dividing the no. of counts of each class with the no. of rows. Then

非常简单明了! 除数 每个班级的人数 行。 然后

weights = df[target_Y].value_counts()/len(df)
model = LGBMClassifier(class_weight = weights)
model.fit(X,target_Y)

3.平滑权重技术: (3. Smoothen Weights Technique:)

This is one of the preferable methods of choosing weights.

这是选择权重的首选方法之一。

labels_dict is the dictionary object contains counts of each class.

labels_dict是字典对象,包含每个类的计数。

The log function smooths the weights for the imbalanced class.

对数函数可平滑不平衡类的权重。

def class_weight(labels_dict,mu=0.15):
total = np.sum(labels_dict.values())
keys = labels_dict.keys()
weight = dict()for i in keys:
score = np.log(mu*total/float(labels_dict[i]))
weight[i] = score if score > 1 else 1return weight# random labels_dict
labels_dict = weights = class_weight(labels_dict)model = RandomForestClassifier(class_weight = weights)
model.fit(X,target_Y)

4.样本权重策略: (4. Sample Weight Strategy:)

This below function is different from the class_weight parameter which is used to get sample weights for the XGboost algorithm. It returns different weights for each training sample.

下面的函数不同于用于获取XGboost算法的样本权重的class_weight参数。 对于每个训练样本,它返回不同的权重。

Sample_weight is an array of the same length as data, containing weights to apply to the model’s loss for each sample.

Sample_weight是与数据长度相同的数组,其中包含权重以应用于每个样本的模型损失。

def BalancedSampleWeights(y_train,class_weight_coef):
classes = np.unique(y_train, axis = 0)
classes.sort()
class_samples = np.bincount(y_train)
total_samples = class_samples.sum()
n_classes = len(class_samples)
weights = total_samples / (n_classes * class_samples * 1.0)
class_weight_dict = {key : value for (key, value) in zip(classes, weights)}
class_weight_dict[classes[1]] = class_weight_dict[classes[1]] *
class_weight_coef
sample_weights = [class_weight_dict[i] for i in y_train]
return sample_weights#Usage
weight=BalancedSampleWeights(
model = XGBClassifier(sample_weight = weight)
model.fit(X,

class_weights vs sample_weight:

class_weights与sample_weight:

sample_weights is used to give weights for each training sample. That means that you should pass a one-dimensional array with the exact same number of elements as your training samples.

sample_weights用于给出每个训练样本的权重。 这意味着您应该传递一维数组,该数组具有与训练样本完全相同数量的元素。

class_weights is used to give weights for each target class. This means you should pass a weight for each class that you are trying to classify.

class_weights用于为每个目标类赋予权重。 这意味着您应该为要分类的每个类传递权重。

结论: (Conclusion:)

The above are few methods of finding class weights and sample weights for your classifier. I mention almost all the techniques which worked well for my project.

上面是为分类器找到分类权重和样本权重的几种方法。 我提到几乎所有对我的项目都有效的技术。

I’m requesting the readers to give a try on these techniques that could help you, if not take it as learning 😄 it may help you another time 😜

我要求读者尝试这些可以帮助您的技术,如果不以学习为learning,那可能会再次帮助您😜

Reach me at LinkedIn 😍

在LinkedIn上到达我

翻译自: https://towardsdatascience.com/how-to-handle-multiclass-imbalanced-data-say-no-to-smote-e9a7f393c310

数据不平衡处理

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389513.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

最小二乘法以及RANSAC(随机采样一致性)思想及实现

线性回归–最小二乘法(Least Square Method) 线性回归: 什么是线性回归? 举个例子,某商品的利润在售价为2元、5元、10元时分别为4元、10元、20元, 我们很容易得出商品的利润与售价的关系符合直线&#xf…

软键盘弹起,导致底部被顶上去

计算出可视界面的高度,当软键盘弹起时让底部元素隐藏掉,当键盘收起时再让它显示,实在没办法时这种方法也不失为一种方法1 var hdocument.documentElement.clientHeight; 2 $(window).resize(function(){ 3 let heightdocument.documentEl…

关于LaaS,PaaS,SaaS一些个人的理解

关于LaaS,PaaS,SaaS一些个人的理解 关于LaaS,PaaS,SaaS一些个人的理解 其实如果从整个程序运营的角度来考虑比较好 第一个LaaS 这个也叫做Haas 就是硬件或者基础设置即服务 比如现在的 aws azure 阿里云 腾讯云 百度云 都是提供服务器基础设置服务的 包括服务器的硬件…

糖药病数据集分类_使用optuna和mlflow进行心脏病分类器调整

糖药病数据集分类背景 (Background) Data science should be an enjoyable process focused on delivering insights and real benefits. However, that enjoyment can sometimes get lost in tools and processes. Nowadays it is important for an applied data scientist to…

Android MVP 框架

为什么80%的码农都做不了架构师?>>> 前言 根据网络上的MVP套路写了一个辣鸡MVP DEMO 用到的 android studio MVPHelper插件,方便自动生成框架代码rxjavaretrofit什么是MVP MVP就是英文的Model View Presenter,然而实际分包并不是只有这三个包…

相似图像搜索的哈希算法思想及实现(差值哈希算法和均值哈希算法)

图像相似度比较哈希算法: 什么是哈希(Hash)? • 散列函数(或散列算法,又称哈希函数,英语:Hash Function)是一种从任何一种数据中创建小 的数字“指纹”的方法。散列函数把消息或数…

腾讯云AI应用产品总监王磊:AI 在传统产业的最佳实践

欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 背景:5月23-24日,以“焕启”为主题的腾讯“云未来”峰会在广州召开,广东省各级政府机构领导、海内外业内学术专家、行业大咖及技术大牛等在现场共议云计算与数字化产业创…

标准化(Normalization)和归一化实现

概念: 原因: 由于进行分类器或模型的建立与训练时,输入的数据范围可能比较大,同时样本中各数据可 能量纲不一致,这样的数据容易对模型训练或分类器的构建结果产生影响,因此需要对其进行标准 化处理&#x…

Toast源码深度分析

目录介绍 1.最简单的创建方法 1.1 Toast构造方法1.2 最简单的创建1.3 简单改造避免重复创建1.4 为何会出现内存泄漏1.5 吐司是系统级别的 2.源码分析 2.1 Toast(Context context)构造方法源码分析2.2 show()方法源码分析2.3 mParams.token windowToken是干什么用的2.4 schedul…

序列化框架MJExtension详解 + iOS ORM框架

当开发中你的模型中属性名称和 字典(JSON/XML) 中的key 不能一一对应时, 或者当字典中嵌套了多层字典数组时..., 以及教你如何用 MJExtension 配置类来统一管理你的模型配置, 下面罗列了开发中常见的一些特殊情况, 请参考!(MJExtension/github) 最基本用法: // 将字典转为模型 …

运行keras出现 FutureWarning: Passing (type, 1) or ‘1type‘ as a synonym of type is deprecated解决办法

运行keras出现 FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, 原则来说,没啥影响,还是能运行,但是看着难受 解决办法: 点击蓝色的链接: 进入 …

RedirectToAction()转移方式及参数传递

今天在做一个功能的时,使用RedirectToAction()需要从这里传几个参数,从网上查了一下,这样解决。真好。 Return RedirectToAction("Index","ManageInfo",new{type0,page1});转载于:https://www.cnblogs.com/ZaraNet/p/978…

软件项目风险管理

近几年来软件开发技术、工具都有了很大的进步,但是软件项目开发超时、超支、甚至不能满足用户需求而根本没有得到实际使用的情况仍然比比皆是。软件项目开发和管理中一直存在着种种不确定性,严重影响着项目的顺利完成和提交。但这些软件风险并未得到充分…

mongdb 群集_群集文档的文本摘要

mongdb 群集This is a part 2 of the series analyzing healthcare chart notes using Natural Language Processing (NLP)这是使用自然语言处理(NLP)分析医疗保健图表笔记的系列文章的第2部分。 In the first part, we talked about cleaning the text and extracting sectio…

keras框架实现手写数字识别

详细细节可学习从零开始神经网络:keras框架实现数字图像识别详解! 代码实现: [1]将训练数据和检测数据加载到内存中(第一次运行需要下载数据,会比较慢): (mnist是手写数据集) train_images是用于训练系统…

gdal进行遥感影像读写_如何使用遥感影像进行矿物勘探

gdal进行遥感影像读写Meet Jose Manuel Lattus, a geologist from Chile. In the latest Soar Cast, he discusses his work in mineral exploration and environmental studies, and explains how he makes a living by creating valuable information products based on diff…

从零开始神经网络:keras框架实现数字图像识别详解!

接口实现可参考:keras框架实现手写数字识别 思路: 我们的代码要导出三个接口,分别完成以下功能: 初始化initialisation,设置输入层,中间层,和输出层的节点数。训练train:根据训练数据不断的更…

大数据学习第一贴

搞了这么久的开发,一直没有养成发博客的习惯,今天开始对大数据所需内容进行总结性记录,并对以后遇到的问题形成一个自己的知识库。就这些!转载于:https://blog.51cto.com/13921538/2299765

推荐算法的先验算法的连接_数据挖掘专注于先验算法

推荐算法的先验算法的连接So here we are diving into the world of data mining this time, let’s begin with a small but informative definition;因此,这一次我们将进入数据挖掘的世界,让我们从一个小的但内容丰富的定义开始; 什么是数…

Android 页面多状态布局管理

一、现状 页面多状态布局是开发中常见的需求,即页面在不同状态需要显示不同的布局,实现的方式也比较多,最简单粗暴的方式就是在 XML 中先将不同状态对应的布局隐藏起来,根据需要改变其可见状态,如果多个界面公用相同的…