-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVarianceThreshold.py
71 lines (50 loc) · 1.91 KB
/
VarianceThreshold.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
# -------------------------------------------- #
# ------- VARIANCE THRESHOLD DESCRIPTION ----- #
# -------------------------------------------- #
# #
# Type Problem: Feature Selection #
# Type Model: Filter #
# Type Evaluation: Unsupervised #
# #
# -------------------------------------------- #
import numpy as np
class VarianceThreshold:
def __init__(self, dataset, num_inputs, variance_max, variance_min):
# Initialize a list of features
data_features = [[0] * num_inputs] * len(dataset)
# Extract all row of features from dataset
i = 0
for row in dataset:
data_features[i] = row[:num_inputs]
i += 1
# Define variables
self.data = data_features
self.var_max = variance_max
self.var_min = variance_min
self.attributes_fitted = []
def execute(self):
# Transposed dataset
x_t = map(list, zip(*self.data))
attributes = []
# Iterate over all transposed rows
for row in x_t:
# Compute the variance of a row
var = np.var(row, axis=0)
# If the var is < or > of defined bounds then keep it
if self.var_min < var < self.var_max:
attributes.append([True, var])
else:
attributes.append([False, var])
# Set the fitted attributes
self.attributes_fitted = attributes
# Get mapped attributes [index_attr, true/false, variance]
def get_mapped_attributes(self):
attributes_map = []
# Iterate over all attributes
i = 0
for attr in self.attributes_fitted:
value = attr[0]
var = attr[1]
attributes_map.append([i, value, var])
i += 1
return attributes_map