private async Task GetAllTrails() { trails = new List <Trail>(); var query = ParseObject.GetQuery("Trail").WhereNotEqualTo("objectId", "toto"); IEnumerable <ParseObject> results = await query.FindAsync(); foreach (var item in results) { Trail trail = new Trail(); trail.Distance = item.Get <float>("distance"); trail.Duration = (int)item.Get <double>("duration"); trail.Id = item.ObjectId; trails.Add(trail); } Kmeans.Init(trails); int[] clusters = Kmeans.Cluster(Kmeans.rawData, 3); List <ParseObject> listResult = new List <ParseObject>(); for (int i = 0; i < results.Count(); i++) { ParseObject pObject = results.ElementAt(i); pObject["clusterId"] = clusters[i]; listResult.Add(pObject); await pObject.SaveAsync(); } GetX(listResult); }
/// <summary> /// Create clustering based on the density /// </summary> /// <param name="numOfClusters">Needed by <see cref="Kmeans"/></param> /// <param name="initialMeans">Needed by <see cref="Kmeans"/></param> /// <returns> /// Clustering as <see cref="MultidimensionalArray"/> /// [0]: x, [1]: y, [2]: data, [3]: cellToCluster (e.g. cell 0 is in cluster 1), [4]: local cell index /// </returns> public MultidimensionalArray CreateClustering_Density(int numOfClusters, double[] initialMeans) { Console.WriteLine("CreateClustering_Density: START"); double[] data = ShockFindingExtensions.GetFinalFunctionValues(input, inputExtended.ExtractSubArrayShallow(-1, 0)); Kmeans kmeans = new Kmeans(data, numOfClusters, initialMeans); int[] cellToCluster = kmeans.Cluster(); MultidimensionalArray clustering = MultidimensionalArray.Create(data.Length, 5); for (int i = 0; i < input.Lengths[0]; i++) { clustering[i, 0] = input[i, (int)inputExtended[i, 0] - 1, 0]; // x clustering[i, 1] = input[i, (int)inputExtended[i, 0] - 1, 1]; // y clustering[i, 2] = data[i]; // data value clustering[i, 3] = cellToCluster[i]; // cellToCluster (e.g. cell 0 is in cluster 1) clustering[i, 4] = inputExtended[i, 2]; // local cell index } _clusterings.Add(clustering); Console.WriteLine("CreateClustering_Density: END"); return(clustering); }
protected void Page_Load(object sender, EventArgs e) { Response.Write("\n开始 k-means 聚类(clustering)\n"); // real data likely to come from a text file or SQL // 需要聚类的数据是两个维度,具体指某个人的身高体重 //集合中每项描述了一个人的身高(Height: inches)和体重(Weight: kilograms) double[][] rawData = new double[20][]; rawData[0] = new double[] { 65.0, 220.0 }; rawData[1] = new double[] { 73.0, 160.0 }; rawData[2] = new double[] { 59.0, 110.0 }; rawData[3] = new double[] { 61.0, 120.0 }; rawData[4] = new double[] { 75.0, 150.0 }; rawData[5] = new double[] { 67.0, 240.0 }; rawData[6] = new double[] { 68.0, 230.0 }; rawData[7] = new double[] { 70.0, 220.0 }; rawData[8] = new double[] { 62.0, 130.0 }; rawData[9] = new double[] { 66.0, 210.0 }; rawData[10] = new double[] { 77.0, 190.0 }; rawData[11] = new double[] { 75.0, 180.0 }; rawData[12] = new double[] { 74.0, 170.0 }; rawData[13] = new double[] { 70.0, 210.0 }; rawData[14] = new double[] { 61.0, 110.0 }; rawData[15] = new double[] { 58.0, 100.0 }; rawData[16] = new double[] { 66.0, 230.0 }; rawData[17] = new double[] { 59.0, 120.0 }; rawData[18] = new double[] { 68.0, 210.0 }; rawData[19] = new double[] { 61.0, 130.0 }; Response.Write("需要聚类的数据如下:</br>"); Response.Write("    身高    体重</br>"); Response.Write("-------------------</br>"); ShowData(rawData, 2, true); //首先需要确定划分的簇的数量 int numClusters = 3; Response.Write("需要聚类的目标簇数: " + numClusters + "<br>"); int[] clustering = Kmeans.Cluster(rawData, numClusters); // this is it Response.Write("K-means 聚类结束e<br>"); //Response.Write("最终划分的结果:"+"<br>"); ///ShowVector(clustering, true); Response.Write("原始数据被聚类之后的结果:<br>"); ShowClustered(rawData, clustering, numClusters, 1); //Response.Write("\nEnd k-means clustering demo\n"); }
/// <summary> /// Create clustering based on the artificial viscosity (mean values) /// </summary> /// <param name="inputClustering">Input data which has to be a previous clustering</param> /// <param name="numOfClusters">Needed by <see cref="Kmeans"/></param> /// <param name="initialMeans">Needed by <see cref="Kmeans"/></param> /// <returns> /// Clustering as <see cref="MultidimensionalArray"/> /// [0]: x, [1]: y, [2]: data, [3]: cellToCluster (e.g. cell 0 is in cluster 1), [4]: local cell index /// </returns> public MultidimensionalArray CreateClustering_AV(MultidimensionalArray inputClustering, int numOfClusters, double[] initialMeans) { Console.WriteLine("CreateClustering_AV: START"); // Get AV values var avField = this.Session.Timesteps.Last().Fields.Where(f => f.Identification == "artificialViscosity").SingleOrDefault(); int numOfPoints = inputClustering.Lengths[0]; double[] data = new double[numOfPoints]; for (int i = 0; i < data.Length; i++) { data[i] = avField.GetMeanValue((int)inputClustering[i, 4]); } // Kmeans Kmeans kmeans = new Kmeans(data, numOfClusters, initialMeans); int[] cellToCluster = kmeans.Cluster(); // Store values MultidimensionalArray clustering = MultidimensionalArray.Create(data.Length, inputClustering.Lengths[1]); for (int i = 0; i < numOfPoints; i++) { clustering[i, 0] = inputClustering[i, 0]; // x clustering[i, 1] = inputClustering[i, 1]; // y clustering[i, 2] = data[i]; // data value clustering[i, 3] = cellToCluster[i]; // cellToCluster (e.g. cell 0 is in cluster 1) clustering[i, 4] = inputClustering[i, 4]; // local cell index } _clusterings.Add(clustering); Console.WriteLine("CreateClustering_AV: END"); return(clustering); }