Commit 2eb7e07c authored by Sascha Niklas Schneiders's avatar Sascha Niklas Schneiders
Browse files

kmeans clustering now outputs clustered point ids

parent 770a4179
...@@ -26,10 +26,60 @@ public class ArmadilloHelperSource { ...@@ -26,10 +26,60 @@ public class ArmadilloHelperSource {
"eig_sym(eigenValues,eigenVectors,A);\n" + "eig_sym(eigenValues,eigenVectors,A);\n" +
"return eigenValues;\n" + "return eigenValues;\n" +
"}\n" + "}\n" +
"\n" +
"static mat getKMeansClusters(mat A, int k){\n" + "static mat getKMeansClusters(mat A, int k){\n" +
"mat clusters;\n" + "mat clusters;\n" +
"kmeans(clusters,A,k,random_subset,10,true);\n" + "kmeans(clusters,A.t(),k,random_subset,20,true);\n" +
"return clusters;\n" + "printf(\"cluster centroid calculation done\\n\");\n" +
"std::ofstream myfile;\n" +
" myfile.open(\"data after cluster.txt\");\n" +
" myfile << A;\n" +
" myfile.close();\n" +
"\t \n" +
"\t std::ofstream myfile2;\n" +
" myfile2.open(\"cluster centroids.txt\");\n" +
" myfile2 << clusters;\n" +
" myfile2.close();\n" +
"mat indexedData=getKMeansClustersIndexData(A.t(), clusters);\n" +
"\n" +
"std::ofstream myfile3;\n" +
" myfile3.open(\"data after index.txt\");\n" +
" myfile3 << indexedData;\n" +
" myfile3.close();\n" +
"\t \n" +
"return indexedData;\n" +
"}\n" +
"\n" +
"static mat getKMeansClustersIndexData(mat A, mat centroids){\n" +
"\tmat result=mat(A.n_cols, 1);\n" +
"\tfor(int i=0;i<A.n_cols;++i){\n" +
"\t\tresult(i, 0) = getIndexForClusterCentroids(A, i, centroids);\n" +
"\t}\n" +
"\treturn result;\n" +
"}\n" +
"\n" +
"static int getIndexForClusterCentroids(mat A, int colIndex, mat centroids){\n" +
"\tint index=1;\n" +
"\tdouble lowestDistance=getEuclideanDistance(A, colIndex, centroids, 0);\n" +
"\tfor(int i=1;i<centroids.n_cols;++i){\n" +
"\t\tdouble curDistance=getEuclideanDistance(A, colIndex, centroids, i);\n" +
"\t\tif(curDistance<lowestDistance){\n" +
"\t\t\tlowestDistance=curDistance;\n" +
"\t\t\tindex=i+1;\n" +
"\t\t}\n" +
"\t}\n" +
"\treturn index;\n" +
"}\n" +
"\n" +
"static double getEuclideanDistance(mat A, int colIndexA, mat B, int colIndexB){\n" +
"\tdouble distance=0;\n" +
"\tfor(int i=0;i<A.n_rows;++i){\n" +
"\t\tdouble elementA=A(i,colIndexA);\n" +
"\t\tdouble elementB=B(i,colIndexB);\n" +
"\t\tdouble diff=elementA-elementB;\n" +
"\t\tdistance+=diff*diff;\n" +
"\t}\n" +
"\treturn sqrt(distance);\n" +
"}\n" + "}\n" +
"\n" + "\n" +
"static mat getSqrtMat(mat A){\n" + "static mat getSqrtMat(mat A){\n" +
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment