Skip to content

Commit 189b3e2

Browse files
Improved quality based on Codacy report #153
1 parent 94f740e commit 189b3e2

File tree

3 files changed

+33
-38
lines changed

3 files changed

+33
-38
lines changed

instance_selection/_CNN.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,24 @@
33
# @Filename: CNN.py
44
# @Author: Daniel Puente Ramírez
55
# @Time: 19/11/21 07:13
6-
# @Version: 3.0
6+
# @Version: 4.0
77

88
import numpy as np
99
import pandas as pd
1010

1111
from .utils import transform, delete_multiple_element
1212

1313

14+
def check_store(store, sample, store_classes):
15+
euc = []
16+
for s in store:
17+
euc.append(np.linalg.norm(s - sample))
18+
euc = np.array(euc)
19+
euc_nn = np.amin(euc)
20+
index_nn = np.ravel(np.where(euc == euc_nn))
21+
return store_classes[index_nn[0]]
22+
23+
1424
class CNN:
1525

1626
def __init__(self):
@@ -51,13 +61,7 @@ def filter(self, samples, y):
5161
handbag = []
5262

5363
for sample_class, sample in zip(samples.target, samples.data):
54-
euc = []
55-
for s in store:
56-
euc.append(np.linalg.norm(s - sample))
57-
euc = np.array(euc)
58-
euc_nn = np.amin(euc)
59-
index_nn = np.ravel(np.where(euc == euc_nn))
60-
nn_class = store_classes[index_nn[0]]
64+
nn_class = check_store(store, sample, store_classes)
6165

6266
if nn_class == sample_class:
6367
handbag.append((sample_class, sample))
@@ -71,13 +75,7 @@ def filter(self, samples, y):
7175
indexes = []
7276
for index, s2 in enumerate(handbag):
7377
sample_class, sample = s2
74-
euc = []
75-
for s in store:
76-
euc.append(np.linalg.norm(s - sample))
77-
euc = np.array(euc)
78-
euc_nn = np.amin(euc)
79-
index_nn = np.ravel(np.where(euc == euc_nn))
80-
nn_class = store_classes[index_nn[0]]
78+
nn_class = check_store(store, sample, store_classes)
8179
if nn_class != sample_class:
8280
store.append(sample)
8381
store_classes.append(sample_class)

instance_selection/_ENN.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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

88
import numpy as np
99
import 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]

instance_selection/_LocalSets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def filter(self, instances, labels):
9696
s_samples.append(instances[index])
9797
s_labels.append(labels[index])
9898

99-
x = pd.DataFrame(s_samples)
99+
x = pd.DataFrame(s_samples, columns=names)
100100
y = pd.DataFrame(s_labels)
101101
return x, y
102102

0 commit comments

Comments
 (0)