python imblearn 处理样本不平衡,svm验证

参考地址:https://blog.csdn.net/tonydz0523/article/details/84325823

1.样本分布不均的解决方法:

1.1 .过采样 通过增加分类中样本较少的类别的采样数量来实现平衡,最直接的方法是简单复制小样本数据时加入随机噪声、干扰数据等。
1.2 .欠采样(下采样) 通过减少分类中多数类样本的数量来实现样本均衡,最直接的方法是随机去掉一些多数类样本来减小多数类的规模,缺点是会丢失多数类中的一些重要信息。
1.3 .设置权重 对不同样本数量的类别赋予不同的权重(通常会设置为与样本量成反比 n samples / (n class * n bincount(y) )
1.4 .集成思想 每次生成训练集时,使用全量小样本,同时从大样本量中随机抽取等量数据量,反复多次会得到多个训练模型。最后在应用时,采用组合(例如投票、加权投票等)产生分类预测结果。

2.python处理

2.1 生成数据集
from collections import Counter
from sklearn.datasets import  make_classification
x,y = make_classification(n_samples=100000,n_features=10,n_informative=2,n_redundant=2, n_repeated=0, n_classes=3, n_clusters_per_class=1, weights=[0.89,0.1,0.01], class_sep=0.3,random_state=2020)
Counter(y)
Counter({0: 88486, 1: 10211, 2: 1303})
2.2 RandomOverSample(过采样)
使用RandomOverSampler从少数类的样本中进行随机采样来增加新的样本使各个分类均衡
from imblearn.over_sampling import RandomOverSampler
x_resampled,y_resampled = RandomOverSampler(random_state=0).fit_sample(x,y)
Counter(y_resampled)
Counter({0: 88486, 1: 88486, 2: 88486})
2.3 SMOTE(过采样)
对于少数类样本a, 随机选择一个最近邻的样本b, 然后从a与b的连线上随机选取一个点c作为新的少数类样本
from imblearn.over_sampling import SMOTE
x_resampled,y_resampled = SMOTE().fit_sample(x,y)
Counter(y_resampled)
Counter({0: 88486, 1: 88486, 2: 88486})
2.4 ADASYM(过采样)
ADASYN: 关注的是在那些基于K最近邻分类器被错误分类的原始样本附近生成新的少数类样本
from imblearn.over_sampling import ADASYN
x_resampled,y_resampled = ADASYN().fit_sample(x,y)
Counter(y_resampled)
Counter({0: 88486, 1: 88198, 2: 88138})
2.5 RandomUnderSampler(下采样)
andomUnderSampler函数是一种快速并十分简单的方式来平衡各个类别的数据: 随机选取数据的子集.
from imblearn.under_sampling import RandomUnderSampler
x_resampled,y_resampled = RandomUnderSampler().fit_sample(x,y)
Counter(y_resampled)
Counter({0: 1303, 1: 1303, 2: 1303})
2.6 设置权重(sklearn SVM举例)
class_weight : {dict, 'balanced'}, optional
    Set the parameter C of class i to class_weight[i]*C for
    SVC. If not given, all classes are supposed to have
    weight one.
    The "balanced" mode uses the values of y to automatically adjust
    weights inversely proportional to class frequencies in the input data
    as ``n_samples / (n_classes * np.bincount(y))``

    
def __init__(self, C=1.0, kernel='rbf', degree=3, gamma='scale',
             coef0=0.0, shrinking=True, probability=False,
             tol=1e-3, cache_size=200, class_weight=None,
             verbose=False, max_iter=-1, decision_function_shape='ovr',
             break_ties=False,
             random_state=None):
    
estimator = svm.SVC(max_iter=1,probability=True,class_weight='balanced',random_state=1234) ##此处还可以采用网格寻参
3. 测试样本不均衡对svm的影响
from sklearn import svm
clf = svm.SVC(class_weight='balanced',max_iter=10000)
from collections import Counter
from sklearn.datasets import  make_classification
x,y = make_classification(n_samples=10000,n_features=10,n_informative=2,n_redundant=2, n_repeated=0, n_classes=2, n_clusters_per_class=1, weights=[0.999,0.001], class_sep=0.3,random_state=2020)
Counter(y)
Counter({0: 9945, 1: 55})
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
x_stand = StandardScaler().fit_transform(x)
x_train,x_test,y_train,y_test = train_test_split(x_stand,y,test_size=0.2)
clf.fit(x_train,y_train)
pred = clf.predict(x_test)
pred
array([0, 1, 0, ..., 0, 0, 0])
y_test
array([0, 0, 0, ..., 0, 0, 0])
from sklearn.metrics import accuracy_score
accuracy_score(pred,y_test)
0.911
from imblearn.over_sampling import RandomOverSampler
x_resampled,y_resampled = RandomOverSampler(random_state=0).fit_sample(x,y)
Counter(y_resampled)
Counter({0: 9945, 1: 9945})
x_stand = StandardScaler().fit_transform(x_resampled)
x_train,x_test,y_train,y_test = train_test_split(x_stand,y_resampled,test_size=0.2)
clf.fit(x_train,y_train)
pred = clf.predict(x_test)
pred
array([0, 1, 0, ..., 1, 1, 0])
accuracy_score(pred,y_test)
0.9595274007038713
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
样本数量不平衡可能会对 SVM 的性能产生影响,原因如下: 1. 偏斜类别:在样本数量不平衡的情况下,某些类别的样本数量较少,这被称为偏斜类别。当一个类别的样本数量很少时,模型可能无法充分学习该类别的特征和模式,导致对少数类的预测性能较差。 2. 分类决策边界:SVM 通过寻找一个最优的决策边界来进行分类。在样本数量不平衡的情况下,由于某些类别的样本数量较少,模型可能更倾向于选择较多样本的类别作为主要决策边界,而忽略了少数类别。这可能导致模型在划分决策边界时偏向于多数类别,从而降低了对少数类别的分类性能。 3. 不平衡数据集评估指标:在处理平衡数据集时,准确度(Accuracy)并不是一个合适的评估指标。由于样本数量不平衡,仅使用准确度作为评估指标可能会给出误导性的结果。其他一些更适合不平衡数据集的评估指标如精确度(Precision)、召回率(Recall)和 F1 分数等可以提供更全面的模型性能评估。 为解决样本数量不平衡SVM 性能的影响,可采取以下策略: 1. 重采样:通过欠采样(undersampling)或过采样(oversampling)等技术调整样本分布,使得各类别样本数量更加平衡。 2. 类别权重调整:通过在训练过程中为各类别赋予不同的权重,使得模型更关注少数类别的分类性能。 3. 数据合成:使用合成的样本来增加少数类别的样本数量,例如使用生成对抗网络(GANs)或插值方法来生成新的少数类别样本。 4. 使用其他算法:考虑使用其他算法,如决策树、随机森林或 XGBoost 等,这些算法对于处理平衡数据集可能更加鲁棒。 综上所述,样本数量不平衡可能会导致 SVM 的性能下降,但可以通过合适的策略和评估指标来缓解这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值