public static Dictionary <string, int> Cluster(IEnumerable <string> itemIds, double[,] features, int numClusters, string centerPath = "", ClusteringAlgorithm algorithm = ClusteringAlgorithm.KMeans) { Console.WriteLine("Clustering..."); //features = Normalized(features); Console.WriteLine("Features normalized."); var dm = new DoubleMatrix(features); ClusterSet clusters = null; if (algorithm == ClusteringAlgorithm.KMeans) { var km = new KMeansClustering(dm); km.Cluster(numClusters); Console.WriteLine("Num Clusters: {0}, Num Items: {1}, Num Iterations: {2}", km.K, km.N, km.Iterations); if (centerPath != "") { var cWriter = new StreamWriter(centerPath); km.FinalCenters.WriteAsCSV(cWriter); cWriter.Close(); } clusters = km.Clusters; } else { var nmf = new NMFClustering <NMFDivergenceUpdate>(); nmf.Factor(dm, numClusters); if (nmf.Converged) { var uWriter = new StreamWriter(Paths.AmazonBooksUsersCluster + ".nmf"); var iWriter = new StreamWriter(Paths.AmazonBooksItemsCluster + ".nmf"); nmf.W.WriteAsCSV(uWriter); nmf.H.WriteAsCSV(iWriter); uWriter.Flush(); iWriter.Flush(); uWriter.Close(); iWriter.Close(); File.WriteAllLines(Paths.AmazonBooksUsersCluster + ".con", nmf.Connectivity.ToTabDelimited().Split('\n')); clusters = nmf.ClusterSet; File.WriteAllLines(Paths.AmazonBooksUsersCluster + ".cluster", clusters.Clusters.Select(c => c.ToString())); Console.WriteLine("Successfully wrote decompose matrixes."); } else { Console.WriteLine("Factorization failed to converge in {0} iterations.", nmf.MaxFactorizationIterations); } } return(itemIds.Zip(clusters.Clusters, (i, c) => new { ItemId = i, Cluster = c }).ToDictionary(i => i.ItemId, i => i.Cluster)); }
public static Dictionary<string, int> Cluster(IEnumerable<string> itemIds, double[,] features, int numClusters, string centerPath = "", ClusteringAlgorithm algorithm = ClusteringAlgorithm.KMeans) { Console.WriteLine("Clustering..."); //features = Normalized(features); Console.WriteLine("Features normalized."); var dm = new DoubleMatrix(features); ClusterSet clusters = null; if (algorithm == ClusteringAlgorithm.KMeans) { var km = new KMeansClustering(dm); km.Cluster(numClusters); Console.WriteLine("Num Clusters: {0}, Num Items: {1}, Num Iterations: {2}", km.K, km.N, km.Iterations); if (centerPath != "") { var cWriter = new StreamWriter(centerPath); km.FinalCenters.WriteAsCSV(cWriter); cWriter.Close(); } clusters = km.Clusters; } else { var nmf = new NMFClustering<NMFDivergenceUpdate>(); nmf.Factor(dm, numClusters); if (nmf.Converged) { var uWriter = new StreamWriter(Paths.AmazonBooksUsersCluster + ".nmf"); var iWriter = new StreamWriter(Paths.AmazonBooksItemsCluster + ".nmf"); nmf.W.WriteAsCSV(uWriter); nmf.H.WriteAsCSV(iWriter); uWriter.Flush(); iWriter.Flush(); uWriter.Close(); iWriter.Close(); File.WriteAllLines(Paths.AmazonBooksUsersCluster + ".con", nmf.Connectivity.ToTabDelimited().Split('\n')); clusters = nmf.ClusterSet; File.WriteAllLines(Paths.AmazonBooksUsersCluster + ".cluster", clusters.Clusters.Select(c => c.ToString())); Console.WriteLine("Successfully wrote decompose matrixes."); } else { Console.WriteLine("Factorization failed to converge in {0} iterations.", nmf.MaxFactorizationIterations); } } return itemIds.Zip(clusters.Clusters, (i, c) => new { ItemId = i, Cluster = c }).ToDictionary(i => i.ItemId, i => i.Cluster); }