[17848] | 1 | using System;
|
---|
| 2 | using HeuristicLab.Data;
|
---|
| 3 |
|
---|
| 4 | namespace HeuristicLab.Algorithms.DataAnalysis.ContinuedFractionRegression {
|
---|
| 5 | public class Transformation {
|
---|
| 6 |
|
---|
| 7 | DoubleMatrix dataMatrix;
|
---|
| 8 |
|
---|
| 9 | private double[] tempMinMax;
|
---|
| 10 | private double[] phiMinMax;
|
---|
| 11 | private double[] phipMinMax;
|
---|
| 12 |
|
---|
| 13 | public Transformation(DoubleMatrix dataMatrix) {
|
---|
| 14 | this.dataMatrix = dataMatrix;
|
---|
| 15 | transform0();
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | private void transform0() { // care! change the other two transformations accordingly!
|
---|
| 19 | tempMinMax = findMinMax(0);
|
---|
| 20 | minMaxTransformation(this.dataMatrix, tempMinMax, 0);
|
---|
| 21 | log10Transformation(this.dataMatrix, 2);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | public double[] transform0(double[] orig) {
|
---|
| 25 | double[] trans = new double[3];
|
---|
| 26 | trans[0] = minMaxTransformation(tempMinMax, orig[0]);
|
---|
| 27 | trans[1] = orig[1];
|
---|
| 28 | trans[2] = log10Transformation(orig[2]);
|
---|
| 29 |
|
---|
| 30 | return trans;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public void useSameTransformation(DoubleMatrix dataMatrix) {
|
---|
| 34 | minMaxTransformation(dataMatrix, tempMinMax, 0);
|
---|
| 35 | log10Transformation(dataMatrix, 2);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | //another transformation - not as good as i thougt
|
---|
| 39 | private void transform1() { // care! change retransformation accordingly!
|
---|
| 40 | tempMinMax = findMinMax(0);
|
---|
| 41 | minMaxTransformation(this.dataMatrix, this.tempMinMax, 0);
|
---|
| 42 | phiMinMax = findMinMax(1);
|
---|
| 43 | minMaxTransformation(this.dataMatrix, this.phiMinMax, 1);
|
---|
| 44 | log10Transformation(2); // no negativ values!
|
---|
| 45 | phipMinMax = findMinMax(2);
|
---|
| 46 | minMaxTransformation(this.dataMatrix, this.phipMinMax, 2); // Min and Max have to be different!
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public double[] transform1(double[] orig) {
|
---|
| 50 | double[] trans = new double[3];
|
---|
| 51 | trans[0] = minMaxTransformation(tempMinMax, orig[0]);
|
---|
| 52 | trans[1] = minMaxTransformation(phiMinMax, orig[1]);
|
---|
| 53 | trans[2] = log10Transformation(orig[2]);
|
---|
| 54 | trans[2] = minMaxTransformation(phipMinMax, trans[2]);
|
---|
| 55 |
|
---|
| 56 | return trans;
|
---|
| 57 | }
|
---|
| 58 | //
|
---|
| 59 |
|
---|
| 60 | private double[] findMinMax(int column) {
|
---|
| 61 | // the first row has index 0
|
---|
| 62 | double[] MinMax = new double[2] { double.MaxValue, double.MinValue };
|
---|
| 63 | // find min and max in column
|
---|
| 64 | for (int i = 0; i < dataMatrix.Rows; i++) {
|
---|
| 65 | if (dataMatrix[i, column] < MinMax[0])
|
---|
| 66 | MinMax[0] = dataMatrix[i, column];
|
---|
| 67 | if (dataMatrix[i, column] > MinMax[1])
|
---|
| 68 | MinMax[1] = dataMatrix[i, column];
|
---|
| 69 | }
|
---|
| 70 | return MinMax;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | private void minMaxTransformation(DoubleMatrix matrix, double[] minMax, int column) {
|
---|
| 74 | // transform all values in column
|
---|
| 75 | for (int i = 0; i < matrix.Rows; i++) {
|
---|
| 76 | matrix[i, column] = (matrix[i, column] - minMax[0]) / (minMax[1] - minMax[0]);
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | private double minMaxTransformation(double[] minMax, double x) {
|
---|
| 81 | return (x - minMax[0]) / (minMax[1] - minMax[0]);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | private void log10Transformation(DoubleMatrix matrix, int column) {
|
---|
| 85 | // the first row has index 0
|
---|
| 86 | for (int i = 0; i < matrix.Rows; i++) {
|
---|
| 87 | matrix[i, column] = Math.Log10(matrix[i, column]);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | private double log10Transformation(double x) {
|
---|
| 92 | return Math.Log10(x);
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | }
|
---|