示例#1
0
  public int predictState(int from_state, int number_of_steps) {
    long lStartTime = System.currentTimeMillis();
    int from_bin = getBinNumber((float) from_state);
    SimpleMatrix result = new SimpleMatrix(transition_matrix);
    for (int i = 1; i < number_of_steps; i++) result = result.mult(transition_matrix);

    SimpleMatrix row_result = result.extractVector(true, from_bin);
    //			row_result.print();

    double maxVal = row_result.get(0, 0);
    int maxValIndex = 0;
    for (int i = 1; i < row_result.numCols(); i++) {
      if (row_result.get(0, i) >= maxVal) {
        maxVal = row_result.get(0, i);
        maxValIndex = i;
      }
    }
    long lEndTime = System.currentTimeMillis();
    logger.info(
        ("From: "
            + from_state
            + " In NumberOfSteps: "
            + number_of_steps
            + " MaxProbabilityBin: "
            + maxValIndex
            + "  MostProbablyTo: "
            + getOrgValFromBinNumber(maxValIndex)));
    logger.info(("Time taken for prediction = " + (lEndTime - lStartTime)));
    return getOrgValFromBinNumber(maxValIndex);
  }
  public static void main(String[] args) {
    try {
      if (args.length < 5) {
        System.out.println(
            "Invalid Syntax usage. \n The Syntax is KMeans inputfile distance-metric #Centroids #Iterations Tolerance featureSet crossValidation testDataSet");
      }
      String inputFile = args[0];
      String distanceMetric = args[1];
      int centroids = Integer.parseInt(args[2]);
      int iterations = Integer.parseInt(args[3]);
      double tolerance = Double.parseDouble(args[4]);
      String inpFeatures = args[5];
      String crossValidation = args[6];
      String testFile = args[7];
      String[] temp = inpFeatures.split(",");
      int[] featureSet = new int[temp.length];
      boolean hasConvered = false;
      for (int i = 0; i < temp.length; i++) {
        featureSet[i] = Integer.parseInt(temp[i]);
      }
      System.out.println("Features Considered are" + Arrays.toString(featureSet));
      SimpleMatrix dataSet = new SimpleMatrix().loadCSV(inputFile);

      // Cluster Parameters
      int dsRows = dataSet.numRows();
      int dsCol = dataSet.numCols();

      // Cluster Initialization
      List<Cluster> kcentroids = new ArrayList<Cluster>();
      for (int i = 0; i < centroids; i++) {
        int random = genRandom(0, dsRows - 1);
        int[] currPoints = new int[dsRows];
        int[] intIndex = new int[dsRows];
        // This will have remnance of the first chosen element
        Random r = new Random();
        SimpleMatrix t = new SimpleMatrix().random(1, dsCol, 0, 0, r);
        Cluster centers =
            new Cluster(i, dataSet.extractVector(true, random), t, currPoints, intIndex);
        kcentroids.add(centers);
      }

      // printKCentroids(kcentroids);
      // dataSet.print();
      SimpleMatrix distMatrix = compDist(kcentroids, dataSet, featureSet, distanceMetric);
      // distMatrix.print();
      // System.out.println(distMatrix.get(0, 1));
      // ReassignCentrids(kcentroids, distMatrix, dataSet,featureSet);
      // printKCentroids(kcentroids);
      for (int k = 0; k < iterations; k++) {
        System.out.println("------------------------Iteration " + k + " ------------------------");
        distMatrix = compDist(kcentroids, dataSet, featureSet, distanceMetric);
        // distMatrix.print();
        ReassignCentrids(kcentroids, distMatrix, dataSet, featureSet);
        printKCentroids(kcentroids);
        hasConvered = detectConvergence(kcentroids, featureSet, tolerance);
        // System.out.println(hasConvered);
        if (hasConvered) {
          System.out.println(
              "------------------------Has Converged : Tolerance------------------------");
          break;
        }
      }
      if (!hasConvered)
        System.out.println(
            "------------------------Has Converged : Iterations------------------------");
      if (crossValidation.equals("true")) {
        SimpleMatrix test = new SimpleMatrix().loadCSV(testFile);
        SimpleMatrix dist = compDist(kcentroids, test, featureSet, distanceMetric);
        System.out.println("----------Associating Set Data Sets to Calculated Centroid-------");
        // dist.print();
        List<Cluster> kCentroids_l = kcentroids;
        int[] clusterLoc = new int[test.numRows()];
        for (int iRows = 0; iRows < test.numRows(); iRows++) {
          int clusterNo = 1;
          double minvalue = dist.get(iRows, 1);
          for (int iCentroid = 0; iCentroid < kCentroids_l.size(); iCentroid++) {
            // System.out.println(iRows+" "+iCentroid);
            if (dist.get(iRows, iCentroid) < minvalue) {
              clusterNo = iCentroid;
              minvalue = dist.get(iRows, iCentroid);
            }
          }
          clusterLoc[iRows] = clusterNo;
        }
        // System.out.println(Arrays.toString(clusterLoc));
        for (int i = 0; i < kCentroids_l.size(); i++) {
          kCentroids_l.get(i).backup();
          kCentroids_l.get(i).noPoints = 0;
          Arrays.fill(kCentroids_l.get(i).currPoints, -1);
          Arrays.fill(kCentroids_l.get(i).intIndex, -1);
          // System.out.println("Clusters Backed Up!");
        }
        // printKCentroids(kCentroids_l);
        for (int i = 0; i < clusterLoc.length; i++) {
          int insLoc = kCentroids_l.get(clusterLoc[i]).noPoints;
          // System.out.println("Getting element"+dataSet.get(i, 0));
          kCentroids_l.get(clusterLoc[i]).currPoints[insLoc] = (int) test.get(i, 0);
          kCentroids_l.get(clusterLoc[i]).intIndex[insLoc] = i;
          kCentroids_l.get(clusterLoc[i]).noPoints++;
        }
        printKCentroids(kCentroids_l);
      }

    } catch (Exception e) {
      System.out.println("Unfortunately There is an error.");
      e.printStackTrace();
    }
  }