SMOTE python实现

发布时间:2019-09-03 08:55:52编辑:auto阅读(1907)

    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

关键字