[9093] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9093] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
[9217] | 26 | using HeuristicLab.Core;
|
---|
[9093] | 27 | using HeuristicLab.Random;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
| 30 | public class FeatureSelection : ArtificialRegressionDataDescriptor {
|
---|
[9217] | 31 | private int nTrainingSamples;
|
---|
| 32 | private int nTestSamples;
|
---|
[9093] | 33 |
|
---|
| 34 | private int numberOfFeatures;
|
---|
| 35 | private double selectionProbability;
|
---|
| 36 | private double noiseRatio;
|
---|
[9217] | 37 | private IRandom xRandom;
|
---|
| 38 | private IRandom weightRandom;
|
---|
[9093] | 39 |
|
---|
| 40 | public override string Name { get { return string.Format("FeatSel-{0}-{1:0%}-{2:0%}", numberOfFeatures, selectionProbability, noiseRatio); } }
|
---|
| 41 | public override string Description {
|
---|
| 42 | get {
|
---|
| 43 | return "This problem is specifically designed to test feature selection." + Environment.NewLine
|
---|
[9217] | 44 | + "In this instance the number of rows for training (" + nTrainingSamples +
|
---|
[9093] | 45 | ") is only slightly larger than the number of columns (" + numberOfFeatures +
|
---|
| 46 | ") and only a subset of the columns must be selected for the predictive model." + Environment.NewLine
|
---|
[9094] | 47 | + "The target variable is calculated as a noisy linear combination of randomly selected features: y = w * S + n." + Environment.NewLine
|
---|
| 48 | + "Where is the S is a N x d matrix containing the selected columns from N x k the matrix of all features X" + Environment.NewLine
|
---|
| 49 | + "For each feature the probability that it is selected is " + selectionProbability + "%" + Environment.NewLine
|
---|
[10440] | 50 | + "X(i,j) ~ N(0, 1) iid, w(i) ~ U(0, 10) iid, n ~ N(0, sigma(w*S) * SQRT(" + noiseRatio / (1 - noiseRatio) + "))" + Environment.NewLine
|
---|
[9094] | 51 | + "The noise level is " + noiseRatio + " * sigma, thus an optimal model has R² = "
|
---|
[9231] | 52 | + Math.Round(optimalRSquared, 2) + " (or equivalently: NMSE = " + noiseRatio + ")" + Environment.NewLine
|
---|
[9217] | 53 | + "N = " + (nTrainingSamples + nTestSamples) + " (" + nTrainingSamples + " training, " + nTestSamples + " test)" + Environment.NewLine
|
---|
[9093] | 54 | + "k = " + numberOfFeatures;
|
---|
| 55 | ;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[9217] | 59 | private double[] w;
|
---|
| 60 | public double[] Weights {
|
---|
| 61 | get { return w; }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | private string[] selectedFeatures;
|
---|
| 65 | public string[] SelectedFeatures {
|
---|
| 66 | get { return selectedFeatures; }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | private double optimalRSquared;
|
---|
| 70 | public double OptimalRSquared {
|
---|
| 71 | get { return optimalRSquared; }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | public FeatureSelection(int numberOfFeatures, double selectionProbability, double noiseRatio, IRandom xGenerator, IRandom weightGenerator)
|
---|
| 76 | : this((int)Math.Round(numberOfFeatures * 1.2), 5000, numberOfFeatures,
|
---|
| 77 | selectionProbability, noiseRatio, xGenerator, weightGenerator) { }
|
---|
| 78 |
|
---|
| 79 | public FeatureSelection(int nTrainingSamples, int nTestSamples, int numberOfFeatures,
|
---|
| 80 | double selectionProbability, double noiseRatio, IRandom xGenerator, IRandom weightGenerator) {
|
---|
[9093] | 81 | this.numberOfFeatures = numberOfFeatures;
|
---|
[9217] | 82 | this.nTrainingSamples = nTrainingSamples;
|
---|
| 83 | this.nTestSamples = nTestSamples;
|
---|
[9093] | 84 | this.selectionProbability = selectionProbability;
|
---|
| 85 | this.noiseRatio = noiseRatio;
|
---|
[9217] | 86 | this.xRandom = xGenerator;
|
---|
| 87 | this.weightRandom = weightGenerator;
|
---|
[9093] | 88 | }
|
---|
| 89 |
|
---|
| 90 | protected override string TargetVariable { get { return "Y"; } }
|
---|
| 91 |
|
---|
| 92 | protected override string[] VariableNames {
|
---|
| 93 | get { return AllowedInputVariables.Concat(new string[] { "Y" }).ToArray(); }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | protected override string[] AllowedInputVariables {
|
---|
| 97 | get {
|
---|
| 98 | return Enumerable.Range(1, numberOfFeatures)
|
---|
| 99 | .Select(i => string.Format("X{0:000}", i))
|
---|
| 100 | .ToArray();
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[9217] | 103 |
|
---|
[9093] | 104 | protected override int TrainingPartitionStart { get { return 0; } }
|
---|
[9217] | 105 | protected override int TrainingPartitionEnd { get { return nTrainingSamples; } }
|
---|
| 106 | protected override int TestPartitionStart { get { return nTrainingSamples; } }
|
---|
| 107 | protected override int TestPartitionEnd { get { return nTrainingSamples + nTestSamples; } }
|
---|
[9093] | 108 |
|
---|
[9217] | 109 |
|
---|
[9093] | 110 | protected override List<List<double>> GenerateValues() {
|
---|
| 111 | List<List<double>> data = new List<List<double>>();
|
---|
| 112 | for (int i = 0; i < AllowedInputVariables.Count(); i++) {
|
---|
[9217] | 113 | data.Add(Enumerable.Range(0, TestPartitionEnd)
|
---|
| 114 | .Select(_ => xRandom.NextDouble())
|
---|
| 115 | .ToList());
|
---|
[9093] | 116 | }
|
---|
| 117 |
|
---|
| 118 | var random = new MersenneTwister();
|
---|
| 119 | var selectedFeatures =
|
---|
| 120 | Enumerable.Range(0, AllowedInputVariables.Count())
|
---|
| 121 | .Where(_ => random.NextDouble() < selectionProbability)
|
---|
| 122 | .ToArray();
|
---|
[9217] | 123 |
|
---|
| 124 | w = selectedFeatures.Select(_ => weightRandom.NextDouble()).ToArray();
|
---|
[9093] | 125 | var target = new List<double>();
|
---|
| 126 | for (int i = 0; i < data[0].Count; i++) {
|
---|
| 127 | var s = selectedFeatures
|
---|
| 128 | .Select(index => data[index][i])
|
---|
| 129 | .ToArray();
|
---|
| 130 | target.Add(ScalarProd(s, w));
|
---|
| 131 | }
|
---|
| 132 | var targetSigma = target.StandardDeviation();
|
---|
[10440] | 133 | var noisePrng = new NormalDistributedRandom(random, 0, targetSigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));
|
---|
[9093] | 134 |
|
---|
| 135 | data.Add(target.Select(t => t + noisePrng.NextDouble()).ToList());
|
---|
| 136 |
|
---|
[9217] | 137 | // set property listing the selected features as string[]
|
---|
| 138 | this.selectedFeatures = selectedFeatures.Select(i => AllowedInputVariables[i]).ToArray();
|
---|
| 139 | optimalRSquared = 1 - noiseRatio;
|
---|
[9093] | 140 | return data;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | private double ScalarProd(double[] s, double[] w) {
|
---|
| 144 | if (s.Length != w.Length) throw new ArgumentException();
|
---|
| 145 | return s.Zip(w, (a, b) => a * b).Sum();
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|