Java examples for java.lang:Math Algorithm
Discretizes using a maximum entropy partitioning
/*/*w w w . j a v a2 s .c o m*/ * Java Information Dynamics Toolkit (JIDT) * Copyright (C) 2012, Joseph T. Lizier * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ //package com.java2s; import java.util.Arrays; public class Main { /** * Discretizes using a maximum entropy partitioning * * @param data * @param numBins * @return */ public static int[] discretiseMaxEntropy(double data[], int numBins) { int[] newData = new int[data.length]; double[] tempData = new double[data.length]; System.arraycopy(data, 0, tempData, 0, data.length); Arrays.sort(tempData); int compartmentSize; double[] cutOffValues = new double[numBins]; for (int i = 0; i < numBins; i++) { compartmentSize = (int) ((double) (i + 1) * (double) (data.length) / (double) numBins) - 1; // System.out.println(compartmentSize); cutOffValues[i] = tempData[compartmentSize]; } for (int i = 0; i < data.length; i++) { for (int m = 0; m < numBins; m++) { if (data[i] <= cutOffValues[m]) { newData[i] = m; break; } } } return newData; } /** * Discretizes each column of the data independently, * using a maximum entropy partitioning * * @param data * @param numBins * @return */ public static int[][] discretiseMaxEntropy(double data[][], int numBins) { int lastCol = data[0].length; int lastRow = data.length; int[][] newData = new int[lastRow][lastCol]; for (int j = 0; j < lastCol; j++) { double[] tempData = new double[lastRow]; for (int i = 0; i < lastRow; i++) { tempData[i] = data[i][j]; } Arrays.sort(tempData); int compartmentSize; double[] cutOffValues = new double[numBins]; for (int i = 0; i < numBins; i++) { compartmentSize = (int) ((double) (i + 1) * (double) (lastRow) / (double) numBins) - 1; // System.out.println(compartmentSize); cutOffValues[i] = tempData[compartmentSize]; } for (int i = 0; i < lastRow; i++) { for (int m = 0; m < numBins; m++) { if (data[i][j] <= cutOffValues[m]) { newData[i][j] = m; m = numBins; } } } } return newData; } /** * Copies all rows and columns between two double arrays * * @param src * @param dest */ public static void arrayCopy(double[][] src, double[][] dest) { for (int r = 0; r < src.length; r++) { System.arraycopy(src[r], 0, dest[r], 0, src[r].length); } } /** * Copies all rows and columns between two double arrays * * @param src * @param dest */ public static double[][] arrayCopy(double[][] src) { double[][] dest = new double[src.length][]; for (int r = 0; r < src.length; r++) { dest[r] = new double[src[r].length]; System.arraycopy(src[r], 0, dest[r], 0, src[r].length); } return dest; } /** * Copies the required rows and columns between two * double arrays * * @param src * @param srcStartRow * @param srcStartCol * @param dest * @param destStartRow * @param destStartCol * @param rows * @param cols */ public static void arrayCopy(double[][] src, int srcStartRow, int srcStartCol, double[][] dest, int destStartRow, int destStartCol, int rows, int cols) { for (int r = 0; r < rows; r++) { System.arraycopy(src[srcStartRow + r], srcStartCol, dest[destStartRow + r], destStartCol, cols); } } /** * Copies all rows and columns between two int arrays * * @param src * @param dest */ public static void arrayCopy(int[][] src, int[][] dest) { for (int r = 0; r < src.length; r++) { System.arraycopy(src[r], 0, dest[r], 0, src[r].length); } } /** * Copies the required rows and columns between two * double arrays * * @param src * @param srcStartRow * @param srcStartCol * @param dest * @param destStartRow * @param destStartCol * @param rows * @param cols */ public static void arrayCopy(int[][] src, int srcStartRow, int srcStartCol, int[][] dest, int destStartRow, int destStartCol, int rows, int cols) { for (int r = 0; r < rows; r++) { System.arraycopy(src[srcStartRow + r], srcStartCol, dest[destStartRow + r], destStartCol, cols); } } }