-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsmote.py
181 lines (105 loc) · 3.89 KB
/
smote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'''
Created on 27/03/2017
@author: Kaushal Shetty
This is an implementation of the SMOTE Algorithm.
See: "SMOTE: synthetic minority over-sampling technique" by
Chawla, N.V et al.
'''
import numpy as np
import random
from sklearn.neighbors import NearestNeighbors
import math
from random import randint
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
class Smote(object):
"""docstring for Smote"""
def __init__(self,distance):
super(Smote, self).__init__()
self.synthetic_arr= []
self.newindex = 0
self.distance_measure = distance
def Populate(self,N,i,indices,min_samples,k):
"""
Populates the synthitic array
Returns:Synthetic Array to generate_syntheic_points
"""
while N!=0:
arr = []
nn = randint(0,k-2)
features = len(min_samples[0])
for attr in range(features):
diff = min_samples[indices[nn]][attr] - min_samples[i][attr]
gap = random.uniform(0,1)
arr.append(min_samples[i][attr] + gap*diff)
self.synthetic_arr.append(arr)
self.newindex = self.newindex + 1
N = N-1
def k_neighbors(self,euclid_distance,k):
nearest_idx_npy = np.empty([euclid_distance.shape[0],euclid_distance.shape[0]],dtype=np.int64)
for i in range(len(euclid_distance)):
idx = np.argsort(euclid_distance[i])
nearest_idx_npy[i] = idx
idx = 0
return nearest_idx_npy[:,1:k]
def find_k(self,X,k):
"""
Finds k nearest neighbors using euclidian distance
Returns: The k nearest neighbor
"""
euclid_distance = np.empty([X.shape[0],X.shape[0]],dtype = np.float32)
for i in range(len(X)):
dist_arr = []
for j in range(len(X)):
dist_arr.append(math.sqrt(sum((X[j]-X[i])**2)))
dist_arr = np.asarray(dist_arr,dtype = np.float32)
euclid_distance[i] = dist_arr
return self.k_neighbors(euclid_distance,k)
def generate_synthetic_points(self,min_samples,N,k):
"""
Returns (N/100) * n_minority_samples synthetic minority samples.
Parameters
----------
min_samples : Numpy_array-like, shape = [n_minority_samples, n_features]
Holds the minority samples
N : percetange of new synthetic samples:
n_synthetic_samples = N/100 * n_minority_samples. Can be < 100.
k : int. Number of nearest neighbours.
Returns
-------
S : Synthetic samples. array,
shape = [(N/100) * n_minority_samples, n_features].
"""
if N < 100:
raise ValueError("Value of N cannot be less than 100%")
if self.distance_measure not in ('euclidian','ball_tree'):
raise ValueError("Invalid Distance Measure.You can use only Euclidian or ball_tree")
if k>min_samples.shape[0]:
raise ValueError("Size of k cannot exceed the number of samples.")
N = int(N/100)
T = min_samples.shape[0]
if self.distance_measure == 'euclidian':
indices = self.find_k(min_samples,k)
elif self.distance_measure=='ball_tree':
nb = NearestNeighbors(n_neighbors = k,algorithm= 'ball_tree').fit(min_samples)
distance,indices = nb.kneighbors(min_samples)
indices = indices[:,1:]
for i in range(indices.shape[0]):
self.Populate(N,i,indices[i],min_samples,k)
return np.asarray(self.synthetic_arr)
def plot_synthetic_points(self,min_samples,N,k):
"""
Plot the over sampled synthtic samples in a scatterplot
"""
if N < 100:
raise ValueError("Value of N cannot be less than 100%")
if self.distance_measure not in ('euclidian','ball_tree'):
raise ValueError("Invalid Distance Measure.You can use only Euclidian or ball_tree")
if k>min_samples.shape[0]:
raise ValueError("Size of k cannot exceed the number of samples.")
synthetic_points = self.generate_synthetic_points(min_samples,N,k)
pca = PCA(n_components=2)
pca.fit(synthetic_points)
pca_synthetic_points = pca.transform(synthetic_points)
plt.scatter(pca_synthetic_points[:,0],pca_synthetic_points[:,1])
plt.show()