spark实现smote近邻采样
一.smote相关理论
(1).
SMOTE是一种对普通过采样(oversampling)的一个改良。普通的过采样会使得训练集中有很多重复的样本。
SMOTE的全称是Synthetic Minority Over-Sampling Technique,译为“人工少数类过采样法”。
SMOTE没有直接对少数类进行重采样,而是设计了算法来人工合成一些新的少数类的样本。
为了叙述方便,就假设阳性为少数类,阴性为多数类
合成新少数类的阳性样本的算法如下:
- 选定一个阳性样本ss
- 找到ss最近的kk个样本,kk可以取5,10之类。这kk个样本可能有阳性的也有阴性的。
- 从这kk个样本中随机挑选一个样本,记为rr。
- 合成一个新的阳性样本s′s′,s′=λs+(1−λ)rs′=λs+(1−λ)r,λλ是(0,1)(0,1)之间的随机数。换句话说,新生成的点在rr与ss之间的连线上。
重复以上步骤,就可以生成很多阳性样本。
=======画了几张图,更新一下======
用图的形式说明一下SMOTE的步骤:
1.先选定一个阳性样本(假设阳性为少数类)
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5Ojf/2wBDAQoKCg0MDRoPDxo3JR8lNzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzf/wgARCADJASIDAREAAhEBAxEB/8QAGwABAQADAQEBAAAAAAAAAAAAAAEEBQYCAwf/xAAaAQEBAQEBAQEAAAAAAAAAAAAAAQQDAgUG/9oADAMBAAIQAxAAAAD9xAAAAAAAAIteQUAAAAAAAAAAAAAAAAYVnxefS7GegAAAAAAAAAAAABEkVbQkaDvw0/bhlc+vXZ9IAAAAAAAAAAAAEjj9GfE6+Oizd9146CCAtqAAAAAAAAAAaj1z47dk8x+kYN3pQAAAAAAABJAtFAAAAABJAW0AAAAAAAIYlnH68o7DJpzPPpQAAAAAAAAAAACFAAARBVAAAAAAAAAAAAA4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dHPMu72KZTIlLZWzCbLaFK1kFalkmwF7kk9AH+DP/xAAsEQABAwIEBAYCAwAAAAAAAAABAAIDESEEFEBREjAxUhATICJQYkFCYGFw/9oACAECAQE/APlgNl5Z2XARrr7emKXy1nDspcSXilNZ0UcwYOizf1Uj+I1p6KaiMCvuQZhfyU5mGpY/CWQohogsp9llPsjZD58RQ968mHv1EeGDhWqybe5SsDTQLpoQE2KMi7kYoR+yeGg251/C+lt8AxzQbrzoe1Pewiwp6GkAoSx9qM0XanFpPwVlZDlNbU0WU+yyn29Y8JHFrKhQOL2VKHLoFQcyqur8l7ZWtNSomyOb7UOULpuGeUcM8I200jeIUUbOAU5gcd1xHfTj+ScB2XA/ZEEIXUeFLxWqOEp+ykZwmldBEGk+5CPD7p8cNDwnmVoUMSWhZl5TiSfAH+1ffQgHWC6ijDjQrJsH7KaAMFQdMIZCKgLLy7Itp15Yqr7o6YTSAUBWYl3RdXr/AIZ//8QALREAAQMCAwYGAgMAAAAAAAAAAQACAwQRIUBREhMUMDFBBRAgUFJhYGIicHH/2gAIAQMBAT8A92Jst41bYztwrj0BSw7a4T7TKfZzr4i5cKR3TGBozkl7YIvqB0Ca+oviEL29jxWOU4r9VxX6ppv+AGWX4oSzfFDLuqHDsuKdoonlwxXXIk2TpnjoEJZT2TC4jHnYaKw0QtksFh5Y+wPa8jArdS6pjCOvocLhGJx7psTh3TRYe3E2XEn4oVJ+PIBKoqcT1DYybArxGkbTShjTdXKHKxVzywrKwVh6woSNoX6Kln8PfM1sQ/l/irJKGN9psTbRSlu0bdEeSTZGpDU2pBQN8tSz8NMJAL2VbVmqk2yLcwtCDAEMv9X/ACMyNCEjUDdEp0xauKJ7JjwRkJS4DBF8yY+XvzCLo090KeyAsPKwVhpkSRnZZC0YIVTj2UU1ziMtvotVv4tU11+WbIAaID6y25i0W4i0TW2/oz//2Q==" alt="" data-filename="1 (1).JPG" />
2.找出这个阳性样本的k近邻(假设k=5)。5个近邻已经被圈出。
aaarticlea/jpeg;base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EQQSMDFAEBQhIkFQUVIgMhNCYnD/2gAIAQIBAT8A7tcKuO8IimuoVQ/FlqLRSi30+qkdfNUBTWBC1/yt7/n41GwDTxNYfsjHZ/KlEYHJ2TjpYor6Nkp1UkNwVrrACqAZlHskQaTzFGGD2UjGhvKdRG0F1ELKwjNGztAzwgcFgBzX4ofZOjiANHY/FVKBKGgAVQjXskT2gUIQkj9VK4E8B8I3NGYQmg9U98RHKKYgbogrxVToYYg5Cxk/spYQwLL5mnVSzXKBqBGJwxmkoAeVU4E7Xn6p1Rmomv6nDa29wQsr06BzRU6YlZp8N44gKvu8q+dKSgK9la2nYz2ACquO8K6R02NFTRPsd796IWO6PvXQwsipzFXLP5UrWD6ocMRrruSFqkCdaHvFCNlVKz8vVMZ+MUrXBCu/MbOONzbKDEhga5tSVurPZTNAdQaZoJNFu0h6IwPGYxGkBAhEknTNJBqt5kHVGd5zPZObZw/zj//EACwRAAEDAQYFBAIDAAAAAAAAAAEAAgMRBBIhMEBBBRQxUFEQEyBSMmFgcHH/2gAIAQMBAT8A7tfar7UDXXVCqPi6zByFkHlRsu67lj9kLMftkDTSveOiEk52ULpD17JQegGjlkuBNtVdlHMXHWEqpKHZJXPAwCbLL4UTyTjqJCQMEZ3jZCdxKBqMkhXUB83k7IyS+E2STcaCgVBoSUAQhT0HYpIySjG/yomuAxPwewnovZm8pjJR1OZe0d0IADQyvLVzIaPxUUpdkYAVC4dw1lqhc8mhCxrRDLxWOaQiSNkABkcOls0d4zivhWN8ErAYBQbq3T2F8JELaO/xDpjlOdRcyE2cO0901rVWLiXKsIu1rmEL22r2xp6ALE7/AMFr2Euor7fKBB39CUZ/0mTfrQyPlrgr8yie49c0tvLlWeU2Bo3QVEYj4TI6bZV7JFM/FY5ks5B6IWk06KE4aZxuhcwwITtKGOW4A7IgU6JraaZwvBcuwoQNCGHZMVj/AFx//9k=" alt="" data-filename="3 (1).JPG" />
4.在阳性样本和被选出的这个近邻之间的连线上,随机找一点。这个点就是人工合成的新的阳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+Mf0KrrQfVPc11hp0+DuoYQ8XTcEfspmch/MFZJ8rhJyhd9OgCuqAagJQDR3RNdCZr/aUecSZ3ULZQ6r9NkRevSPCfhyzM7UlAJ8VXhyFtMOIXUPlFxOza6qLkG7CmyfJyKNyZb4Imi5A66hZnvWgu7LpO8ItcOETA80UmBDvcvTFjSebYXuoGYcj9yjFhRYqUNB/TUjlcywRxUhTpjJdUQJT4XO9yYKC6A0aflRZIU1q6oUUDXtzKGFYBWqkaA6g2hTG1svTTeEYJAKkagJQJVSTtCmOpZepm8ozyEUJ+Dp/wAP/8QALBEAAQMBBgYCAQUAAAAAAAAAAQACAxEEITAxQEEFEhMUIFEQUEIiI1JhcP/aAAgBAwEBPwD7YvAQeDuga60Ee0CD4G9PsYduhYgN0xvKKax7A4Zo2QndRwFqA8L1fp5Hy1uXUnUT3HPRgaAooU0QUkpYu6/pRzc2qKJoECaJp86BU1c7iMl1paZKJ8jjeNNkpZC0XLu3eky0uO2GGoDzeXjJdWUbKJ7jnjGioPSFPWhqqq/4u+hfHKcnJsU38kwUR+XskORQhl9qNhGeGHaOnjRUw5ZnNKNrB/FRP5sGHhrH2Ezk3ogC4YlMUtaqk/imjzzK4bNY4weuKqGSzGzF4H7a4nNYntHbihw3ycq7oJkwdo6AK75h4o6Oymz8txRywy0FdMIMpo4rXBKaNKJOw8Kb1w6aPehVr4r0JDGG1VjtnQkLiK1VitRtDOalPoaoGpqE+BrnFxauFRc0rgWqNgY2gFNYS0boSNO6a8O+JZAwZIz1OShkKBQAGNlkpHzg/pXPOVFzkX4j4mPzQs0YTIuT4IqKUToiNlHGGquCHed6KFcWmKVJM9pXcPdsoiSL9IE51F3EXtCeM74ha1crU0AaQJzart4vSEEY2/zL/9k=" alt="" data-filename="4 (1).JPG" />
以上来自http://sofasofa.io/forum_main_post.php?postid=1000817中的叙述
(2).
With this approach, the positive class is over-sampled by taking each minority class sample and introducing synthetic examples along the line segments joining any/all of the k minority class nearest neighbours. Depending upon the amount of over-sampling required, neighbours from the k nearest neighbours are randomly chosen. This process is illustrated in the following Figure, where xixi is the selected point, xi1xi1 to xi4xi4are some selected nearest neighbours and r1r1 to r4r4 the synthetic data points created by the randomized interpolation. The implementation of this work uses only one nearest neighbour with the euclidean distance, and balances both classes to 50% distribution.
Synthetic samples are generated in the following way: Take the difference between the feature vector (sample) under consideration and its nearest neighbour. Multiply this difference by a random number between 0 and 1, and add it to the feature vector under consideration. This causes the selection of a random point along the line segment between two specific features. This approach effectively forces the decision region of the minority class to become more general. An example is detailed in the next Figure.
In short, the main idea is to form new minority class examples by interpolating between several minority class examples that lie together. In contrast with the common replication techniques (for example random oversampling), in which the decision region usually become more specific, with SMOTE the overfitting problem is somehow avoided by causing the decision boundaries for the minority class to be larger and to spread further into the majority class space, since it provides related minority class samples to learn from. Specifically, selecting a small k-value could also avoid the risk of including some noise in the data.
以上来自https://sci2s.ugr.es/multi-imbalanced中的叙述
二.spark实现smote
核心代码如下,完整代码https://github.com/jiangnanboy/spark-smote/blob/master/spark%20smote.txt
/**
* (1) 对于少数类(X)中每一个样本x,计算它到少数类样本集(X)中所有样本的距离,得到其k近邻。
* (2) 根据样本不平衡比例设置一个采样比例以确定采样倍率sampling_rate,对于每一个少数类样本x,
* 从其k近邻中随机选择sampling_rate个近邻,假设选择的近邻为 x(1),x(2),...,x(sampling_rate)。
* (3) 对于每一个随机选出的近邻 x(i)(i=1,2,...,sampling_rate),分别与原样本按照如下的公式构建新的样本
* xnew=x+rand(0,1)?(x(i)?x)
*
* http://sofasofa.io/forum_main_post.php?postid=1000817
* http://sci2s.ugr.es/multi-imbalanced
* @param session
* @param labelFeatures
* @param knn 样本相似近邻
* @param samplingRate 近邻采样率 (knn * samplingRate),从knn中选择几个近邻
* @parm rationToMax 采样比率(与最多类样本数的比率) 0.1表示与最多样本的比率是 -> (1:10),即达到最多样本的比率
* @return
*/
public static Dataset<Row> smote(SparkSession session, Dataset<Row> labelFeatures, int knn, double samplingRate, double rationToMax) { Dataset<Row> labelCountDataset = labelFeatures.groupBy("label").agg(count("label").as("keyCount"));
List<Row> listRow = labelCountDataset.collectAsList();
ConcurrentMap<String, Long> keyCountConMap = new ConcurrentHashMap<>(); //每个label对应的样本数
for(Row row : listRow)
keyCountConMap.put(row.getString(0), row.getLong(1));
Row maxSizeRow = labelCountDataset.select(max("keyCount").as("maxSize")).first();
long maxSize = maxSizeRow.getAs("maxSize");//最大样本数 JavaPairRDD<String, SparseVector> sparseVectorJPR = labelFeatures.toJavaRDD().mapToPair(row -> {
String label = row.getString(0);
SparseVector features = (SparseVector) row.get(1);
return new Tuple2<String, SparseVector>(label, features);
}); JavaPairRDD<String, List<SparseVector>> combineByKeyPairRDD = sparseVectorJPR.combineByKey(sparseVector -> {
List<SparseVector> list = new ArrayList<>();
list.add(sparseVector);
return list;
}, (list, sparseVector) -> {list.add(sparseVector);return list;},
(list_A, list_B) -> {list_A.addAll(list_B);return list_A;}); JavaSparkContext jsc = JavaSparkContext.fromSparkContext(session.sparkContext());
final Broadcast<ConcurrentMap<String, Long>> keyCountBroadcast = jsc.broadcast(keyCountConMap);
final Broadcast<Long> maxSizeBroadcast = jsc.broadcast(maxSize);
final Broadcast<Integer> knnBroadcast = jsc.broadcast(knn);
final Broadcast<Double> samplingRateBroadcast = jsc.broadcast(samplingRate);
final Broadcast<Double> rationToMaxBroadcast = jsc.broadcast(rationToMax); /**
* JavaPairRDD<String, List<SparseVector>>
* JavaPairRDD<String, String>
* JavaRDD<Row>
*/
JavaPairRDD<String, List<SparseVector>> pairRDD = combineByKeyPairRDD
.filter(slt -> {
return slt._2().size() > 1;
})
.mapToPair(slt -> {
String label = slt._1();
ConcurrentMap<String, Long> keySizeConMap = keyCountBroadcast.getValue();
long oldSampleSize = keySizeConMap.get(label);
long max = maxSizeBroadcast.getValue();
double ration = rationToMaxBroadcast.getValue();
int Knn = knnBroadcast.getValue();
double rate = samplingRateBroadcast.getValue();
if (oldSampleSize < maxSize * rationToMax) {
int needSampleSize = (int) (max * ration - oldSampleSize);
List<SparseVector> list = generateSample(slt._2(), needSampleSize, Knn, rate);
return new Tuple2<String, List<SparseVector>>(label, list);
} else {
return slt;
}
}); JavaRDD<Row> javaRowRDD = pairRDD.flatMapToPair(slt -> {
List<Tuple2<String, SparseVector>> floatPairList = new ArrayList<>();
String label = slt._1();
for(SparseVector sv : slt._2())
floatPairList.add(new Tuple2<String, SparseVector>(label, sv));
return floatPairList.iterator();
}).map(svt->{
return RowFactory.create(svt._1(), svt._2());
}); Dataset<Row> resultDataset = session.createDataset(javaRowRDD.rdd(), EncoderInit.getlabelFeaturesRowEncoder());
return resultDataset;
}
spark实现smote近邻采样的更多相关文章
- 机器学习 —— 类不平衡问题与SMOTE过采样算法
在前段时间做本科毕业设计的时候,遇到了各个类别的样本量分布不均的问题——某些类别的样本数量极多,而有些类别的样本数量极少,也就是所谓的类不平衡(class-imbalance)问题. 本篇简述了以下内 ...
- [转]类不平衡问题与SMOTE过采样算法
在前段时间做本科毕业设计的时候,遇到了各个类别的样本量分布不均的问题——某些类别的样本数量极多,而有些类别的样本数量极少,也就是所谓的类不平衡(class-imbalance)问题. 本篇简述了以下内 ...
- Spark之数据倾斜 --采样分而治之解决方案
1 采样算法解决数据倾斜的思想 2 采样算法在spark数据倾斜中的具体操作
- 大数据开发认知--spark
1. Spark rdd生成过程· Spark的任务调度分为四步 1RDD objects RDD的准备阶段,组织RDD及RDD的依赖关系生成大概的RDD的DAG图,DAG图是有向环图. 2DAG s ...
- Spark源码剖析 - 计算引擎
本章导读 RDD作为Spark对各种数据计算模型的统一抽象,被用于迭代计算过程以及任务输出结果的缓存读写.在所有MapReduce框架中,shuffle是连接map任务和reduce任务的桥梁.map ...
- 过采样中用到的SMOTE算法
平时很多分类问题都会面对样本不均衡的问题,很多算法在这种情况下分类效果都不够理想.类不平衡(class-imbalance)是指在训练分类器中所使用的训练集的类别分布不均.比如说一个二分类问题,100 ...
- 过采样算法之SMOTE
SMOTE(Synthetic Minority Oversampling Technique),合成少数类过采样技术.它是基于随机过采样算法的一种改进方案,由于随机过采样采取简单复制样本的策略来增加 ...
- 机器学习入门-数据过采样(上采样)1. SMOTE
from imblearn.over_sampling import SMOTE # 导入 overstamp = SMOTE(random_state=0) # 对训练集的数据进行上采样,测试集的 ...
- 从信用卡欺诈模型看不平衡数据分类(1)数据层面:使用过采样是主流,过采样通常使用smote,或者少数使用数据复制。过采样后模型选择RF、xgboost、神经网络能够取得非常不错的效果。(2)模型层面:使用模型集成,样本不做处理,将各个模型进行特征选择、参数调优后进行集成,通常也能够取得不错的结果。(3)其他方法:偶尔可以使用异常检测技术,IF为主
总结:不平衡数据的分类,(1)数据层面:使用过采样是主流,过采样通常使用smote,或者少数使用数据复制.过采样后模型选择RF.xgboost.神经网络能够取得非常不错的效果.(2)模型层面:使用模型 ...
随机推荐
- 计算广告CTR预估系列(七)--Facebook经典模型LR+GBDT理论与实践
计算广告CTR预估系列(七)--Facebook经典模型LR+GBDT理论与实践 2018年06月13日 16:38:11 轻春 阅读数 6004更多 分类专栏: 机器学习 机器学习荐货情报局 版 ...
- python 小数精度控制
可以用:round(数值,保留小数位数) 详情参考 https://www.cnblogs.com/herbert/p/3402245.html
- C# 常用类库说明
Array类 用括号声明数组是C#中使用Array类的记号.在后台使用C#语法,会创建一个派生于抽象基类Array的新类.这样,就可以使用Array类为每个C#数组定义的方法和属性了. Array类实 ...
- 【原创】Linux基础之logrotate
logrotate logrotate ‐ rotates, compresses, and mails system logs logrotate is designed to ease admin ...
- 无障碍开发(六)之ARIA在HTML中的使用规则
ARIA使用规则一 如果你使用的元素( HTML5 )具有语义化,应该使用这些元素,而不应该重新定义一个添加ARIA的角色.状态或属性的元素. 浏览器的语义化标签已经默认隐含ARIA语义,像nav,a ...
- QPushButton样式
QPushButton:hover:!pressed { border: 1px solid #434E7A; }
- 帝国cms 修改分页样式
帝国cms 修改分页样式(路径) /e/class/t_functions.php
- DevOps与Kubernetes 、容器的关系
近两年,随着容器.Kubernetes 等技术的兴起,DevOps 这个概念被广泛提及并被大量使用. 本文将会从以下几个方面着手,结合实验展现的方式,让读者真正理解 DevOps 的含义. DevOp ...
- A query was run and no Result Maps were found for...原来是mapper.xml文件出了问题,是使用MyBatis最常见的一种错误
今天遇到一个问题,原来是mapper.xml文件出了问题,是使用MyBatis最常见的一种错误 报错的结果是这样的: A query was run and no Result Maps were f ...
- 7.控制计划任务crontab命令
at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的Linux 系统上面原本就有非常多的计划 性工作,因此这个系统服务是默认 ...