๐Ÿ“ฆ sorend / mecj

๐Ÿ“„ FeatureSelectionHelper.java ยท 43 lines
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
43package svu.featureselection;

import svu.meclassifier.DistanceFunctionFactory;
import svu.meclassifier.MultimodalEvolutionaryClassifier;
import svu.util.AccuracyHelper;

public class FeatureSelectionHelper {

	// private static Logger logger = Logger.getLogger(FeatureSelectionHelper.class);
	
	public static int[] selectWithMEC(final double[][] X, final int[] Y, final DistanceFunctionFactory dff, int howManyFeatures) {

		FeaturesEvaluator evaluator = new FeaturesEvaluator() {
			@Override
			public double evaluate(int[] featureIdx) {
				// create classifier with featureIdx set.
				MultimodalEvolutionaryClassifier mec = new MultimodalEvolutionaryClassifier(5 * (featureIdx.length + 1), dff).featureIdx(featureIdx);
				// train and predict this classifier
				int[] yPredicted = mec.fit(X, Y).predict(X);
				// get accuracy of predictions
				double r = 1.0 - AccuracyHelper.accuracy(Y, yPredicted);
				// logger.info("featureIdx " + ListUtil.prettyArray(featureIdx) + " accuracy " + (1.0 - r));
				return r;
			}
		};
		
		GeneticAlgorithmFeatureSelection selection =
				new GeneticAlgorithmFeatureSelection(evaluator);

		if (howManyFeatures > 0) {
			int m = X[0].length;
			int n = Math.min(howManyFeatures, m);
			selection.encoding = FeatureSelectionEncoding.Factory.fixed(n);
		}
		else {
			// or do this for variable
			selection.encoding = FeatureSelectionEncoding.Factory.variable();
		}

		return selection.selectFeatures(X, Y);
	}
}