Instantly share code, notes, and snippets.
Last active
July 14, 2016 13:51
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save ertugrulozcan/3807f9cfc8cd8ccae49354f2a92e0136 to your computer and use it in GitHub Desktop.
Swing RadioButton
This file contains hidden or 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
import com.navin.csv.CSVReader; | |
import java.io.IOException; | |
import java.util.Arrays; | |
public class GUI // extends JFrame | |
{ | |
//... | |
private double distance; | |
// Contructor | |
public GUI() | |
{ | |
//... | |
radiobutton1.addActionListener(new ActionListener() | |
{ | |
@Override | |
public void actionPerformed(ActionEvent e) | |
{ | |
// Update distance | |
SetDistance(); | |
} | |
}); | |
//... | |
} | |
private void SetDistance() | |
{ | |
this.distance = distance(Program.kmeanProvider.GetCalculationResult().labels, Program.kmeanProvider.GetCalculationResult().CENTROIDS); | |
} | |
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
public static double distance(double[] a, double[] b) | |
{ | |
return Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2)); | |
} | |
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
public static double distance1(double[] a, double[] b, double[][] Sigma) throws Exception | |
{ | |
double[][] Sigma_inverse = Matrix.Inverse(Sigma); | |
double[][] diff = new double[1][a.length]; | |
for (int i = 0; i < a.length - 1; i++) { | |
diff[0][i] = a[i] - b[i]; | |
} | |
double result[][] = Matrix.mulMatrix(diff, Sigma_inverse); | |
result = Matrix.mulMatrix(result, Matrix.Transpose(diff)); | |
return Math.sqrt(result[0][0]); | |
} | |
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
//... | |
} | |
public class Program | |
{ | |
public static kmeanProvider; | |
/** | |
* @param args | |
* the command line arguments | |
* | |
* | |
*/ | |
public static void main(String[] args) throws IOException, Exception | |
{ | |
kmeanProvider = new Kmean1(2); | |
} | |
// TODO code application logic here | |
} | |
public class Kmean1 | |
{ | |
// Clustor size | |
private int k = 2; | |
public int GetClusterSize() | |
{ | |
return this.k; | |
} | |
/* | |
private double[][] data; | |
public double[][] GetDataArray() | |
{ | |
return this.data; | |
} | |
private double[][] CENTROIDS; | |
public double[][] GetCenteroids() | |
{ | |
return this.CENTEROIDS; | |
} | |
*/ | |
private KMeansResult result; | |
public KMeansResult GetCalculationResult() | |
{ | |
return this.result; | |
} | |
// Constructor | |
public Kmean1(int clusterSize) | |
{ | |
this.k = clusterSize; | |
double[][] rawData = CSVReader.readCSV("Data.csv", 150, false); | |
this.data = new double[rawData.length][]; | |
// Assume column size is 2 | |
for (int i = 0; i < rawData.length; i++) | |
{ | |
this.data[i] = new double[] { rawData[i][0], rawData[i][1] }; | |
} | |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
this.CENTROIDS = Intialization.datasample(data, k); | |
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// Print numbers to search | |
System.out.print("Data: "); | |
for (double[] arr : data) | |
{ | |
System.out.println(Arrays.toString(arr)); | |
} | |
System.out.println("\n"); | |
this.result = kMeans(data, CENTROIDS, 0.0001f); | |
// result.labels = assignLabels(data, CENTROIDS); | |
// assert CENTROIDS.length == k; | |
// Print out the data belonging to each cluster | |
for (int i = 0; i < k; ++i) | |
{ | |
System.out.println("Cluster[" + i + "] at (" + result.CENTROIDS[i][0] + "," + result.CENTROIDS[i][1] + ")"); | |
} | |
System.out.println(); | |
/* | |
double[][] xs = new double[k][]; | |
double[][] ys = new double[k][]; | |
for (int i = 0; i < k; i++) | |
{ | |
System.out.print("Cluster[" + i + "] data: "); | |
ArrayList<double[]> dataList = new ArrayList<>(); | |
for (int j = 0; j < data.length; j++) | |
{ | |
if (result.labels[j] == i) | |
{ | |
System.out.print("(" + data[j][0] + "," + data[j][1] + "), "); | |
dataList.add(new double[] { data[j][0], data[j][1] }); | |
} | |
} | |
System.out.println(); | |
} | |
*/ | |
} | |
// minchange0.00001f is threshold for converge | |
public static KMeansResult kMeans(double[][] data, double[][] CENTROIDS, double minChange) throws Exception | |
{ | |
int[] labels = null; // IDX assigned to clusters | |
double[][] Sigma = Intialization.computeCovarianceMatrix(data); // for | |
// mahal | |
// distance | |
boolean changed = true; // ? | |
// why this condition ?? | |
while (changed) | |
{ | |
labels = assignLabels(data, CENTROIDS); | |
double[][] newCENTROIDS = new double[CENTROIDS.length][2]; | |
// Compute new centeroid positions ( update centeriod | |
for (int i = 0; i < CENTROIDS.length; i++) | |
{ | |
double[] mean = new double[2]; | |
int count = 0; | |
for (int j = 0; j < data.length; j++) | |
{ | |
if (labels[j] == i) | |
{ | |
mean[0] += data[j][0]; | |
mean[1] += data[j][1]; | |
count++; | |
} | |
} | |
newCENTROIDS[i][0] = mean[0] / (double) count; | |
newCENTROIDS[i][1] = mean[1] / (double) count; | |
} | |
changed = false; | |
// Check if any of the centeriod changes exceed the threshold | |
for (int i = 0; i < CENTROIDS.length; i++) | |
{ | |
if (distance(CENTROIDS[i], newCENTROIDS[i]) >= minChange) | |
{ | |
changed = true; | |
break; | |
} | |
} | |
CENTROIDS = newCENTROIDS; | |
} | |
// To return all the results we need we use a class | |
KMeansResult result = new KMeansResult(); // produce error when we | |
// remove static from class | |
// kmean result | |
result.labels = labels; | |
result.CENTROIDS = CENTROIDS; | |
return result; | |
} | |
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
public static int[] assignLabels(double[][] data, double[][] CENTROIDS1) throws Exception | |
{ | |
// Initialize labels | |
double[][] Sigma = Intialization.computeCovarianceMatrix(data); | |
int[] labels = new int[data.length]; | |
// Find the cluster closest to this data and | |
// assign it as the new label | |
for (int i = 0; i < data.length; i++) | |
{ | |
double minDistance = Double.MAX_VALUE; | |
for (int j = 0; j < CENTROIDS1.length; j++) | |
{ | |
double distance = distance(data[i], CENTROIDS1[j]); | |
if (distance < minDistance) | |
{ | |
minDistance = distance; | |
labels[i] = j; | |
} | |
} | |
} | |
return labels; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment