简体   繁体   English

如何保存Accord.Net K-Means的聚类结果以供重用?

[英]How do I save the clustering results of Accord.Net K-Means for reuse?

I am trying to save the clustering results of Accord.Net K-Means so that I don't have to recompute every time I run my program. 我正在尝试保存Accord.Net K-Means的聚类结果,这样我每次运行程序时都不必重新计算。

I also want to assign permanent human-readable labels to each cluster. 我还想为每个群集分配永久的人类可读标签。 This is not possible if I have to compute the clusters every time. 如果我每次都要计算集群,这是不可能的。

In Accord.NET, every object can be saved using standard .NET binary serialization. 在Accord.NET中,可以使用标准的.NET二进制序列化保存每个对象。 This means that you can save your K-Means using: 这意味着您可以使用以下方法保存您的K-Means:

KMeans kmeans = ...

using (FileStream fs = new FileStream(path, FileMode.Create))
{
    new BinaryFormatter().Serialize(fs, kmeans);
}    

and load it back using: 并使用以下方法加载:

KMeans kmeans = null;

using (FileStream fs = new FileStream(path, FileMode.Open))
{
    kmeans = new BinaryFormatter().Deserialize(fs) as KMeans;
}    

Hope it helps! 希望能帮助到你!

Accord.Net has a serializing class. Accord.Net有一个序列化类。 In the package Accord.IO there exists the class Serializer . Accord.IO包中存在类Serializer You can use it like 你可以像使用它一样

KMeans means;
Accord.IO.Serializer.Save(means, filename);

or 要么

means = Accord.IO.Serializer.Load<KMeans>(filename);

Ok after messing around in Accord.Net and doing some research, I figured out how to save the Clustering, so I'm going to add the solution here in case anyone else bumps into the same problem. 好吧,在Accord.Net搞乱并做了一些研究后,我想出了如何保存群集,所以我将在这里添加解决方案以防其他人遇到同样的问题。

Basically all you have to do is save the Cluster Centroids and then use them to initialize KMeans next time before running Compute: 基本上你要做的就是保存Cluster Centroids,然后在运行Compute之前使用它们来下次初始化KMeans:

// Saving the Centroids
int[] clusterIds = kmeans.Compute(observations);
double[][] centroids = kmeans.Clusters.Centroids;

... ...

// Initializing with Centroids
kmeans = new KMeans(numOfClusters);
kmeans.Clusters.Centroids = centroids;
int[] clusterIds = kmeans.Compute(observations);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM