@@ -13,7 +13,8 @@ def selection_wrapper(data, num_instances, selection_method=None, num_features=N
1313 num_features = data .get_shape ().as_list ()[- 1 ]
1414
1515 values , indices = selection_method (data , num_instances , num_features )
16- return values , tf .gather (data , indices , axis = 1 )
16+ selected_features = tf .gather (data , indices , axis = 1 )
17+ return values , selected_features
1718
1819
1920def fisher (data , num_instances : list , top_k_features = 2 ):
@@ -27,15 +28,20 @@ def fisher(data, num_instances: list, top_k_features=2):
2728 :return: the list of most significant features.
2829 """
2930 assert len (num_instances ) == 2 , "Fisher selection method can be performed for two-class problems."
31+
3032 data = tf .convert_to_tensor (data )
3133 _ , num_features = data .get_shape ().as_list ()
3234 if top_k_features > num_features :
3335 top_k_features = num_features
3436 class1 , class2 = tf .split (data , num_instances )
35- mean1 , std1 = tf .nn .moments (class1 , axes = 0 )
36- mean2 , std2 = tf .nn .moments (class2 , axes = 0 )
37- fisher_coeffs = tf .abs (mean1 - mean2 ) / (std1 + std2 )
38- return tf .nn .top_k (fisher_coeffs , k = top_k_features )
37+
38+ with tf .name_scope ('fisher_selection' ):
39+ mean1 , std1 = tf .nn .moments (class1 , axes = 0 )
40+ mean2 , std2 = tf .nn .moments (class2 , axes = 0 )
41+ fisher_coeffs = tf .abs (mean1 - mean2 ) / (std1 + std2 )
42+ selected_features = tf .nn .top_k (fisher_coeffs , k = top_k_features )
43+
44+ return selected_features
3945
4046
4147def feature_correlation_with_class (data , num_instances : list , top_k_features = 10 ):
@@ -49,11 +55,15 @@ def feature_correlation_with_class(data, num_instances: list, top_k_features=10)
4955 if top_k_features > num_features :
5056 top_k_features = num_features
5157 class1 , class2 = tf .split (data , num_instances )
52- mean1 , std1 = tf .nn .moments (class1 , axes = 0 )
53- mean2 , std2 = tf .nn .moments (class2 , axes = 0 )
54- mean , std = tf .nn .moments (data , axes = 0 )
55- corr_coeffs = (tf .square (mean1 - mean ) + tf .square (mean2 - mean )) / 2 * tf .square (std )
56- return tf .nn .top_k (corr_coeffs , k = top_k_features )
58+
59+ with tf .name_scope ('corr_selection' ):
60+ mean1 , std1 = tf .nn .moments (class1 , axes = 0 )
61+ mean2 , std2 = tf .nn .moments (class2 , axes = 0 )
62+ mean , std = tf .nn .moments (data , axes = 0 )
63+ corr_coeffs = (tf .square (mean1 - mean ) + tf .square (mean2 - mean )) / 2 * tf .square (std )
64+ selected_features = tf .nn .top_k (corr_coeffs , k = top_k_features )
65+
66+ return selected_features
5767
5868
5969def t_test (data , num_instances : list , top_k_features = 10 ):
@@ -67,7 +77,29 @@ def t_test(data, num_instances: list, top_k_features=10):
6777 if top_k_features > num_features :
6878 top_k_features = num_features
6979 class1 , class2 = tf .split (data , num_instances )
70- mean1 , std1 = tf .nn .moments (class1 , axes = 0 )
71- mean2 , std2 = tf .nn .moments (class2 , axes = 0 )
72- t_test_coeffs = tf .abs (mean1 - mean2 ) / tf .sqrt (tf .square (std1 )/ num_instances [0 ] + tf .square (std2 ) / num_instances [1 ])
73- return tf .nn .top_k (t_test_coeffs , k = top_k_features )
80+
81+ with tf .name_scope ('t_test_selection' ):
82+ mean1 , std1 = tf .nn .moments (class1 , axes = 0 )
83+ mean2 , std2 = tf .nn .moments (class2 , axes = 0 )
84+ t_test_coeffs = tf .abs (mean1 - mean2 ) / tf .sqrt (
85+ tf .square (std1 ) / num_instances [0 ] + tf .square (std2 ) / num_instances [1 ])
86+ selected_features = tf .nn .top_k (t_test_coeffs , k = top_k_features )
87+
88+ return selected_features
89+
90+
91+ def random (data , num_instances : list , top_k_features = 10 ):
92+ data = tf .convert_to_tensor (data )
93+ _ , num_features = data .get_shape ().as_list ()
94+ if top_k_features > num_features :
95+ top_k_features = num_features
96+ class1 , class2 = tf .split (data , num_instances )
97+
98+ with tf .name_scope ('random_selection' ):
99+ mean1 , std1 = tf .nn .moments (class1 , axes = 0 )
100+ mean2 , std2 = tf .nn .moments (class2 , axes = 0 )
101+ t_test_coeffs = tf .abs (mean1 - mean2 ) / tf .sqrt (
102+ tf .square (std1 ) / num_instances [0 ] + tf .square (std2 ) / num_instances [1 ])
103+ selected_features = tf .nn .top_k (t_test_coeffs , k = top_k_features )
104+
105+ return selected_features
0 commit comments