前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SMOTE python实现

SMOTE python实现

作者头像
py3study
发布2020-01-08 15:19:22
8430
发布2020-01-08 15:19:22
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
from sklearn.neighbors import NearestNeighbors
from random import choice

X = np.array([[-1, -1],
              [-2, -1],
              [-3, -2],
              [1, 1],
              [2, 1],
              [3, 2]])
neigh = NearestNeighbors(n_neighbors = 5)
neigh.fit(X)
N=3

S = np.zeros(shape=(X.shape[0]*(N-1), X.shape[1]))
S = np.vstack((X, S))
print S
for i in xrange(X.shape[0]):
    nn = neigh.kneighbors(X[i].reshape(1, -1), return_distance=False)
    for n in xrange(N-1):
        nn_index = choice(nn[0])
        #NOTE: nn includes T[i], we don't want to select it
        while nn_index == i:
            nn_index = choice(nn[0])
        dif = X[nn_index] - X[i]
        # print dif
        gap = np.random.random()
        index = n + i * (N-1)+X.shape[0]
        print index
        S[index, :] = X[i,:] + gap * dif[:]

print S
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档