/
AbstractCountBasedFeatureSelector.java
157 lines (131 loc) · 6.87 KB
/
AbstractCountBasedFeatureSelector.java
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
/**
* Copyright (C) 2013-2016 Vasilis Vryniotis <bbriniotis@datumbox.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datumbox.framework.core.machinelearning.common.abstracts.featureselectors;
import com.datumbox.framework.common.Configuration;
import com.datumbox.framework.common.dataobjects.Dataframe;
import com.datumbox.framework.common.dataobjects.Record;
import com.datumbox.framework.common.dataobjects.TypeInference;
import com.datumbox.framework.common.storageengines.interfaces.StorageEngine;
import com.datumbox.framework.core.machinelearning.common.abstracts.AbstractTrainer;
import java.util.*;
/**
* Abstract class which is the base of every Count Based Feature Selection algorithm.
*
* @author Vasilis Vryniotis <bbriniotis@datumbox.com>
* @param <MP>
* @param <TP>
*/
public abstract class AbstractCountBasedFeatureSelector<MP extends AbstractCountBasedFeatureSelector.AbstractModelParameters, TP extends AbstractCountBasedFeatureSelector.AbstractTrainingParameters> extends AbstractScoreBasedFeatureSelector<MP, TP> {
/**
* @param trainingParameters
* @param configuration
* @see AbstractTrainer#AbstractTrainer(AbstractTrainer.AbstractTrainingParameters, Configuration)
*/
protected AbstractCountBasedFeatureSelector(TP trainingParameters, Configuration configuration) {
super(trainingParameters, configuration);
}
/**
* @param storageName
* @param configuration
* @see AbstractTrainer#AbstractTrainer(String, Configuration)
*/
protected AbstractCountBasedFeatureSelector(String storageName, Configuration configuration) {
super(storageName, configuration);
}
/** {@inheritDoc} */
@Override
protected void _fit(Dataframe trainingData) {
StorageEngine storageEngine = knowledgeBase.getStorageEngine();
TP trainingParameters = knowledgeBase.getTrainingParameters();
MP modelParameters = knowledgeBase.getModelParameters();
Set<TypeInference.DataType> supportedXDataTypes = getSupportedXDataTypes();
Map<Object, TypeInference.DataType> xDataTypes = trainingData.getXDataTypes();
Map<Object, Integer> tmp_classCounts = new HashMap<>(); //map which stores the counts of the classes
Map<List<Object>, Integer> tmp_featureClassCounts = storageEngine.getBigMap("tmp_featureClassCounts", (Class<List<Object>>)(Class<?>)List.class, Integer.class, StorageEngine.MapType.HASHMAP, StorageEngine.StorageHint.IN_MEMORY, false, true); //map which stores the counts of feature-class combinations.
Map<Object, Double> tmp_featureCounts = storageEngine.getBigMap("tmp_featureCounts", Object.class, Double.class, StorageEngine.MapType.HASHMAP, StorageEngine.StorageHint.IN_MEMORY, false, true); //map which stores the counts of the features
//find the featureCounts
logger.debug("Estimating featureCounts");
for(Record r : trainingData) {
for(Map.Entry<Object, Object> entry : r.getX().entrySet()) {
Object column = entry.getKey();
if(!supportedXDataTypes.contains(xDataTypes.get(column))) {
continue;
}
Double value = TypeInference.toDouble(entry.getValue());
if(value>0.0) {
double featureCounter = tmp_featureCounts.getOrDefault(column, 0.0);
tmp_featureCounts.put(column, ++featureCounter);
}
}
}
//remove rare features
Integer rareFeatureThreshold = trainingParameters.getRareFeatureThreshold();
if(rareFeatureThreshold != null && rareFeatureThreshold>0) {
removeRareFeatures(tmp_featureCounts, rareFeatureThreshold);
}
//now find the classCounts and the featureClassCounts
logger.debug("Estimating classCounts and featureClassCounts");
for(Record r : trainingData) {
Object theClass = r.getY();
Integer classCounter = tmp_classCounts.getOrDefault(theClass, 0);
tmp_classCounts.put(theClass, ++classCounter);
for(Map.Entry<Object, Object> entry : r.getX().entrySet()) {
Object column = entry.getKey();
if(!supportedXDataTypes.contains(xDataTypes.get(column))) {
continue;
}
Double value = TypeInference.toDouble(entry.getValue());
if(value>0.0) {
List<Object> featureClassTuple = Arrays.asList(column, theClass);
Integer featureClassCounter = tmp_featureClassCounts.getOrDefault(featureClassTuple, 0);
tmp_featureClassCounts.put(featureClassTuple, ++featureClassCounter);
}
}
}
//call the overriden method to get the scores of the features.
final Map<Object, Double> featureScores = modelParameters.getFeatureScores();
estimateFeatureScores(featureScores, trainingData.size(), tmp_classCounts, tmp_featureClassCounts, tmp_featureCounts);
//drop the unnecessary stastistics tables
tmp_classCounts.clear();
storageEngine.dropBigMap("tmp_featureClassCounts", tmp_featureClassCounts);
storageEngine.dropBigMap("tmp_featureCounts", tmp_featureCounts);
//keep only the top features
Integer maxFeatures = trainingParameters.getMaxFeatures();
if(maxFeatures != null && maxFeatures<featureScores.size()) {
keepTopFeatures(featureScores, maxFeatures);
}
}
/** {@inheritDoc} */
@Override
protected Set<TypeInference.DataType> getSupportedXDataTypes() {
return new HashSet<>(Arrays.asList(TypeInference.DataType.BOOLEAN, TypeInference.DataType.NUMERICAL));
}
/** {@inheritDoc} */
@Override
protected Set<TypeInference.DataType> getSupportedYDataTypes() {
return new HashSet<>(Arrays.asList(TypeInference.DataType.BOOLEAN, TypeInference.DataType.CATEGORICAL, TypeInference.DataType.ORDINAL));
}
/**
* Abstract method which is responsible for estimating the score of each Feature.
*
* @param featureScores
* @param N
* @param classCounts
* @param featureClassCounts
* @param featureCounts
*/
protected abstract void estimateFeatureScores(Map<Object, Double> featureScores, int N, Map<Object, Integer> classCounts, Map<List<Object>, Integer> featureClassCounts, Map<Object, Double> featureCounts);
}