有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

机器学习如何在Java中生成均匀分布的随机数?

因此,在java中,给定某些数据集中某些属性的最大值和最小值(来自UCI机器学习的Iris),我很难生成均匀分布的随机数。我拥有的是虹膜数据集,在一些叫做样本的二维数组中。我根据iris数据集中每个属性的最大值和最小值(不含class属性)将随机值放入一个名为gworms的二维数组中(该数组为算法的其他一些值提供了一些额外字段)

到目前为止,完整的算法没有正常工作,我的想法是,可能gworms(4-d空间中的点)没有正确生成,或者没有很好的随机性。我认为这些点彼此接近(我认为这是因为后来得到的一些结果,这里没有显示它们的代码)。因此,我请求您帮助验证我为gworms实现“统一分发”的代码(对于de前4个位置):

/*
 * To change this license header, choose License Headers in Project 
 Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 package glowworms;

 import java.lang.Math;
 import java.util.ArrayList;
 import java.util.Random;
 import weka.core.AttributeStats;

 import weka.core.Instances;

/**
*
* @author oscareduardo937
*/

  public class GSO {

/* ************ Initializing parameters of CGSO algorithm ******************** */
int swarmSize = 1000; // Swarm size m
int maxIte = 200;
double stepSize = 0.03; // Step size for the movements
double luciferin = 5.0; // Initial luciferin level
double rho = 0.4; // Luciferin decay parameter
double gamma = 0.6; // Luciferin reinforcement parameter

double rs = 0.38; // Initial radial sensor range. This parameter depends on the data set and needs to be found by running experiments

double gworms[][] = null; // Glowworms of the swarm. 

/* ************ Initializing parameters of clustering problem and data set ******************** */
int numAtt; // Dimension of the position vector
int numClasses; // Number of classes
int total_data; //Number of instances
int threshold = 5;
int runtime = 1;
/*Algorithm can be run many times in order to see its robustness*/

double minValuesAtts[] = new double[this.numAtt]; // Minimum values for all attributes
double maxValuesAtts[] = new double[this.numAtt]; // Maximum values for all attributes

double samples[][] = new double[this.total_data][this.numAtt]; //Samples of the selected dataset.

ArrayList<Integer> candidateList;
double r;

/*a random number in the range [0,1)*/

/* *********** Method to put the instances in a matrix and get max and min values for attributes ******************* */

public void instancesToSamples(Instances data) {
    this.numAtt = data.numAttributes();
    System.out.println("********* NumAttributes: " + this.numAtt);
    AttributeStats attStats = new AttributeStats();
    if (data.classIndex() == -1) {
      //System.out.println("reset index...");
      data.setClassIndex(data.numAttributes() - 1);
    }

    this.numClasses = data.numClasses();
    this.minValuesAtts = new double[this.numAtt];
    this.maxValuesAtts = new double[this.numAtt];

    System.out.println("********* NumClasses: " + this.numClasses);
    this.total_data = data.numInstances();
    samples = new double[this.total_data][this.numAtt];

    double[] values = new double[this.total_data];

    for (int j = 0; j < this.numAtt; j++) {
        values = data.attributeToDoubleArray(j);

        for (int i = 0; i < this.total_data; i++) {
            samples[i][j] = values[i];
        }

    }

    for(int j=0; j<this.numAtt-1; j++){

        attStats = data.attributeStats(j);

        this.maxValuesAtts[j] = attStats.numericStats.max;
        this.minValuesAtts[j] = attStats.numericStats.min;

        //System.out.println("** Min Value Attribute " + j + ": " + this.minValuesAtts[j]);
        //System.out.println("** Max Value Attribute " + j + ": " + this.maxValuesAtts[j]);
    }

    //Checking
    /*for(int i=0; i<this.total_data; i++){
    for(int j=0; j<this.numAtt; j++){
        System.out.print(samples[i][j] + "** ");
      }
            System.out.println();
 }*/ 

} // End of method InstancesToSamples


public void initializeSwarm(Instances data) {
    this.gworms = new double[this.swarmSize][this.numAtt + 2]; // D-dimensional vector plus luciferin, fitness and intradistance.
    double intraDistance = 0;
    Random r = new Random(); //Random r;

    for (int i = 0; i < this.swarmSize; i++) {

        for (int j = 0; j < this.numAtt - 1; j++) {
            //Uniform randomization of d-dimensional position vector
            this.gworms[i][j] = this.minValuesAtts[j] + (this.maxValuesAtts[j] - this.minValuesAtts[j]) * r.nextDouble();   

        }

        this.gworms[i][this.numAtt - 1] = this.luciferin; // Initial luciferin level for all swarm
        this.gworms[i][this.numAtt] = 0; // Initial fitness for all swarm
        this.gworms[i][this.numAtt + 1] = intraDistance; // Intra-distance for gworm i
    }


    //Checking gworms
    /*for(int i=0; i<this.swarmSize; i++){
    for(int j=0; j<this.numAtt+2; j++){
        System.out.print(gworms[i][j] + "** ");
      }
            System.out.println();
 }*/

} // End of method initializeSwarm
    }

主要课程是:

package uniformrandomization;

 /**
 *
 * @author oscareduardo937
 */

 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.FileNotFoundException;

 import weka.core.Instances;
 import glowworms.GSO;


 public class UniformRandomization {

public UniformRandomization(){
    super();
}

//Loading the data from the filename file to the program. It can be .arff or .csv
public static BufferedReader readDataFile(String filename) {
    BufferedReader inputReader = null;

    try {
        inputReader = new BufferedReader(new FileReader(filename));
    } catch (FileNotFoundException ex) {
        System.err.println("File not found: " + filename);
    }

    return inputReader;
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws Exception {
    // TODO code application logic here

    BufferedReader datafile1 = readDataFile("src/data/iris.arff");
    Instances data = new Instances(datafile1);

GSO gso = new GSO();
    gso.instancesToSamples(data);
    gso.initializeSwarm(data);

    System.out.println("Fin...");
}

 }

所以我想知道,通过这段代码,gworms的位置ij的数字是否在属性j的最大值和最小值范围内生成

非常感谢你的帮助


共 (0) 个答案