Created
September 11, 2019 19:46
-
-
Save jmlon/856375ca62722f361efa8bc75062c98b to your computer and use it in GitHub Desktop.
Generador de conjuntos de puntos 2D para pruebas de clusterización
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.upb.estalg.Estructuras.UnionFind; | |
import edu.princeton.cs.algs4.StdDraw; | |
import edu.princeton.cs.algs4.StdOut; | |
import edu.princeton.cs.algs4.StdRandom; | |
import java.awt.*; | |
import java.util.Arrays; | |
public class GenerateGaussianClusters { | |
public static double[][] clusters(int k, int n, int d) { | |
double[][] datapoints = new double[k*n][]; | |
int pos=0; | |
double[] mu = new double[d]; | |
double[] sigma = new double[d]; | |
double min=Double.POSITIVE_INFINITY, max=Double.NEGATIVE_INFINITY; | |
for(int i=0; i<k; i++) { | |
for(int l=0; l<d; l++) { | |
mu[l] = 2*(StdRandom.uniform() - 0.5); | |
sigma[l] = StdRandom.uniform() / 3; | |
} | |
for(int j=0; j<n; j++) { | |
double[] sample = new double[d]; | |
for(int l=0; l<d; l++) { | |
sample[l] = StdRandom.gaussian(mu[l], sigma[l]); | |
min = sample[l]<min ? sample[l] : min; | |
max = sample[l]>max ? sample[l] : max; | |
} | |
datapoints[pos++] = sample; | |
StdOut.println(Arrays.toString(sample)); | |
} | |
} | |
StdOut.println("min = "+min+", max="+max); | |
return datapoints; | |
} | |
public static void plotDataPoints(double[][] datapoints) { | |
Color[] colors = { StdDraw.CYAN, StdDraw.RED, StdDraw.GREEN }; | |
StdDraw.setScale(-2,2); | |
StdDraw.setPenRadius(0.004); | |
for(int i=0; i<datapoints.length; i++) { | |
StdDraw.point(datapoints[i][0], datapoints[i][1]); | |
} | |
} | |
public static void main(String[] args) { | |
StdRandom.setSeed(454); | |
double[][] datapoints = clusters(3, 50, 2); | |
plotDataPoints(datapoints); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment