33# @Filename: ENN.py
44# @Author: Daniel Puente Ramírez
55# @Time: 16/11/21 17:14
6- # @Version: 5 .0
6+ # @Version: 6 .0
77
88import numpy as np
99import pandas as pd
@@ -18,6 +18,20 @@ def __init__(self, nearest_neighbors=3, power_parameter=2):
1818 self .power_parameter = power_parameter
1919 self .x_attr = None
2020
21+ def neighs (self , s_samples , s_targets , index , removed ):
22+ x_sample = s_samples [index - removed ]
23+ x_target = s_targets [index - removed ]
24+ knn = NearestNeighbors (n_jobs = - 1 ,
25+ n_neighbors = self .nearest_neighbors , p = 2 )
26+ samples_not_x = s_samples [:index - removed ] + s_samples [
27+ index - removed + 1 :]
28+ targets_not_x = s_targets [:index - removed ] + s_targets [
29+ index - removed + 1 :]
30+ knn .fit (samples_not_x )
31+ _ , neigh_ind = knn .kneighbors ([x_sample ])
32+
33+ return x_sample , x_target , targets_not_x , samples_not_x , neigh_ind
34+
2135 def filter (self , samples , y ):
2236 """
2337 Wilson, D. L. (1972). Asymptotic properties of nearest neighbor rules
@@ -42,17 +56,8 @@ def filter(self, samples, y):
4256 removed = 0
4357
4458 for index in range (size ):
45- x_sample = s_samples [index - removed ]
46- x_target = s_targets [index - removed ]
47- knn = NearestNeighbors (
48- n_jobs = 1 , n_neighbors = self .nearest_neighbors ,
49- p = self .power_parameter )
50- samples_not_x = s_samples [:index - removed ] + s_samples [
51- index - removed + 1 :]
52- targets_not_x = s_targets [:index - removed ] + s_targets [
53- index - removed + 1 :]
54- knn .fit (samples_not_x )
55- _ , neigh_ind = knn .kneighbors ([x_sample ])
59+ _ , x_target , targets_not_x , samples_not_x , neigh_ind = self .neighs (
60+ s_samples , s_targets , index , removed )
5661 y_targets = np .ravel (
5762 np .array ([targets_not_x [x ] for x in neigh_ind [0 ]])).astype (int )
5863 count = np .bincount (y_targets )
@@ -94,16 +99,8 @@ def filter_original_complete(self, original, original_y, complete,
9499 removed = 0
95100
96101 for index in range (size ):
97- x_sample = s_samples [index - removed ]
98- x_target = s_targets [index - removed ]
99- knn = NearestNeighbors (n_jobs = - 1 ,
100- n_neighbors = self .nearest_neighbors , p = 2 )
101- samples_not_x = s_samples [:index - removed ] + s_samples [
102- index - removed + 1 :]
103- targets_not_x = s_targets [:index - removed ] + s_targets [
104- index - removed + 1 :]
105- knn .fit (samples_not_x )
106- _ , neigh_ind = knn .kneighbors ([x_sample ])
102+ x_sample , x_target , targets_not_x , samples_not_x , neigh_ind = \
103+ self .neighs (s_samples , s_targets , index , removed )
107104 y_targets = [targets_not_x [x ] for x in neigh_ind [0 ]]
108105 count = np .bincount (y_targets )
109106 max_class = np .where (count == np .amax (count ))[0 ][0 ]
0 commit comments