Search in sources :

Example 6 with DoubleMatrix1D

use of cern.colt.matrix.tdouble.DoubleMatrix1D in project clusterMaker2 by RBVI.

the class KCluster method selectCentroidsRandom.

// Assign centroids to rows at random
private static void selectCentroidsRandom(int nClusters, DoubleMatrix2D data, DoubleMatrix2D cdata) {
    System.out.println("Assigning centroids randomly");
    // number of centroids allready set
    int centroid_count = 0;
    // centroid assigned element == 1 if centroid assigned, zero otherwise
    int[] centroid_assigned = new int[data.rows()];
    for (int i = 0; i < nClusters; i++) {
        centroid_assigned[i] = 0;
    }
    // randomly select first centroid
    Random generator = new Random();
    int centroid_id = generator.nextInt(data.rows());
    for (int i = 0; i < nClusters; i++) {
        System.out.println("Centroid selection iteration " + i + " Centroid id " + centroid_id);
        // update centroid assinged array
        centroid_assigned[centroid_id] = 1;
        DoubleMatrix1D centroid = data.viewRow(centroid_id);
        // copy newly selected centroid from data matrix
        for (int j = 0; j < centroid.size(); j++) cdata.set(centroid_count, j, centroid.get(j));
        centroid_count++;
        if (centroid_count == nClusters)
            break;
        while (centroid_assigned[centroid_id] == 1) {
            centroid_id = generator.nextInt(data.rows());
        }
    }
}
Also used : Random(java.util.Random) DoubleMatrix1D(cern.colt.matrix.tdouble.DoubleMatrix1D)

Example 7 with DoubleMatrix1D

use of cern.colt.matrix.tdouble.DoubleMatrix1D in project clusterMaker2 by RBVI.

the class KCluster method selectCentroidsOrthogonally.

// Assign centroids to rows with maximal orthogonality
private static void selectCentroidsOrthogonally(int nClusters, DoubleMatrix2D data, DoubleMatrix2D cdata) {
    System.out.println("Assigning centroids orthogonolly");
    // number of centroids allready set
    int centroid_count = 0;
    // centroid assigned element == 1 if centroid assigned, zero otherwise
    int[] centroid_assigned = new int[data.rows()];
    for (int i = 0; i < nClusters; i++) {
        centroid_assigned[i] = 0;
    }
    // array of cosine sums to centroids allreay chosen
    double[] cosines = new double[data.rows()];
    for (int i = 0; i < data.rows(); i++) {
        cosines[i] = 0;
    }
    // randomly select first centroid
    Random generator = new Random();
    int centroid_id = generator.nextInt(data.rows());
    for (int i = 0; i < nClusters; i++) {
        System.out.println("Centroid selection iteration " + i + " Centroid id " + centroid_id);
        // update centroid assinged array
        centroid_assigned[centroid_id] = 1;
        DoubleMatrix1D centroid = data.viewRow(centroid_id);
        // copy newly selected centroid from data matrix
        for (int j = 0; j < centroid.size(); j++) cdata.set(centroid_count, j, centroid.get(j));
        centroid_count++;
        if (centroid_count == nClusters)
            break;
        double min_cosine = 10000;
        int new_centroid_id = -1;
        // loop through rows of data matrix, seach for next centroid, which will minimize the cosine angle (dot product) with all current centroids
        for (int j = 0; j < data.rows(); j++) {
            // ignore centroids that allready have been set
            if (centroid_assigned[j] == 1)
                continue;
            cosines[j] += centroid.zDotProduct(data.viewRow(j));
            // if new cosine sum value < min cosine value, update min_cosine and new_centroid_id to reflect that
            if (min_cosine > cosines[j]) {
                min_cosine = cosines[j];
                new_centroid_id = j;
            }
        }
        centroid_id = new_centroid_id;
    }
}
Also used : Random(java.util.Random) DoubleMatrix1D(cern.colt.matrix.tdouble.DoubleMatrix1D)

Example 8 with DoubleMatrix1D

use of cern.colt.matrix.tdouble.DoubleMatrix1D in project clusterMaker2 by RBVI.

the class RunSCPS method run.

public List<NodeCluster> run(CyNetwork network, TaskMonitor monitor) {
    int k;
    monitor.showMessage(TaskMonitor.Level.INFO, "Formatting Matrix Data");
    DoubleMatrix2D sMat = getSMat(this.distanceMatrix);
    DoubleMatrix2D LMat = getLMat(sMat);
    monitor.showMessage(TaskMonitor.Level.INFO, "Calculating Eigenvalues");
    DenseDoubleEigenvalueDecomposition decomp = new DenseDoubleEigenvalueDecomposition(LMat);
    DoubleMatrix2D eigenVect = decomp.getV();
    DoubleMatrix1D eigenVal = decomp.getRealEigenvalues();
    monitor.showMessage(TaskMonitor.Level.INFO, "Calculating K value");
    if (this.kvalue > -1)
        k = this.kvalue;
    else
        k = getK(eigenVal, .3);
    System.out.println("K is " + k);
    if (numComponents > k) {
        doComponentClustering();
        return new ArrayList<NodeCluster>(this.clusterMap.values());
    }
    monitor.showMessage(TaskMonitor.Level.INFO, "Creating uMatrix for kMeans");
    DoubleMatrix2D uMat = getUMat(eigenVect, k);
    monitor.showMessage(TaskMonitor.Level.INFO, "Running kmeans clustering");
    doKMeansClustering(uMat, sMat);
    // clusterMap calculated in getSMat and doKMeansClustering steps. Simply return the results
    return new ArrayList<NodeCluster>(this.clusterMap.values());
}
Also used : DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D) DenseDoubleEigenvalueDecomposition(cern.colt.matrix.tdouble.algo.decomposition.DenseDoubleEigenvalueDecomposition) DoubleMatrix1D(cern.colt.matrix.tdouble.DoubleMatrix1D) DoubleArrayList(cern.colt.list.tdouble.DoubleArrayList) IntArrayList(cern.colt.list.tint.IntArrayList) ArrayList(java.util.ArrayList)

Example 9 with DoubleMatrix1D

use of cern.colt.matrix.tdouble.DoubleMatrix1D in project clusterMaker2 by RBVI.

the class ColtOps method multiplyMatrix.

// For some reason, the parallelcolt version of zMult doesn't
// really take advantage of the available cores.  This version does, but
// it seems like it only works for multiplying matrices of the same
// size.
public Matrix multiplyMatrix(Matrix matrix2) {
    // return mult(matrix);
    // if (matrix2.nRows() != matrix.nRows() || matrix2.nColumns() != matrix.nColumns()())
    // return mult(matrix);
    DoubleMatrix2D A = getData();
    DoubleMatrix2D B = matrix2.getColtMatrix();
    int m = A.rows();
    int n = A.columns();
    int p = B.columns();
    // Create views into B
    final DoubleMatrix1D[] Brows = new DoubleMatrix1D[n];
    for (int i = n; --i >= 0; ) Brows[i] = B.viewRow(i);
    // Create a series of 1D vectors
    final DoubleMatrix1D[] Crows = new DoubleMatrix1D[m];
    for (int i = m; --i >= 0; ) Crows[i] = B.like1D(p);
    // Create the thread pools
    final ExecutorService[] threadPools = new ExecutorService[nThreads];
    for (int pool = 0; pool < threadPools.length; pool++) {
        threadPools[pool] = Executors.newFixedThreadPool(1);
    }
    A.forEachNonZero(new IntIntDoubleFunction() {

        public double apply(int row, int column, double value) {
            Runnable r = new ThreadedDotProduct(value, Brows[column], Crows[row]);
            threadPools[row % nThreads].submit(r);
            return value;
        }
    });
    for (int pool = 0; pool < threadPools.length; pool++) {
        threadPools[pool].shutdown();
        try {
            boolean result = threadPools[pool].awaitTermination(7, TimeUnit.DAYS);
        } catch (Exception e) {
        }
    }
    // Recreate C
    return new ColtMatrix(matrix, create2DMatrix(Crows));
}
Also used : IntIntDoubleFunction(cern.colt.function.tdouble.IntIntDoubleFunction) DoubleMatrix2D(cern.colt.matrix.tdouble.DoubleMatrix2D) DoubleMatrix1D(cern.colt.matrix.tdouble.DoubleMatrix1D) ExecutorService(java.util.concurrent.ExecutorService)

Example 10 with DoubleMatrix1D

use of cern.colt.matrix.tdouble.DoubleMatrix1D in project clusterMaker2 by RBVI.

the class ColtOps method rowSum2.

public double rowSum2(int row) {
    DoubleMatrix1D rowMat = getData().viewRow(row);
    double rSum2 = rowMat.aggregate(DoubleFunctions.plus, DoubleFunctions.square);
    return rSum2;
}
Also used : DoubleMatrix1D(cern.colt.matrix.tdouble.DoubleMatrix1D)

Aggregations

DoubleMatrix1D (cern.colt.matrix.tdouble.DoubleMatrix1D)10 DoubleArrayList (cern.colt.list.tdouble.DoubleArrayList)3 IntArrayList (cern.colt.list.tint.IntArrayList)3 DoubleMatrix2D (cern.colt.matrix.tdouble.DoubleMatrix2D)3 IntIntDoubleFunction (cern.colt.function.tdouble.IntIntDoubleFunction)2 Random (java.util.Random)2 DoubleFunction (cern.colt.function.tdouble.DoubleFunction)1 DenseDoubleEigenvalueDecomposition (cern.colt.matrix.tdouble.algo.decomposition.DenseDoubleEigenvalueDecomposition)1 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1