当使用SMILE学习生成模型后,如果需要把模型导出,怎么做?
SMILE的scala API的data package提供了write API能力,但Java没有
Java只能自己使用xStream.
libraryDependencies += "com.thoughtworks.xstream" % "xstream" % "1.4.11.1"
xStream可以轻易的将Java对象和xml文档相互转换,而且可以修改某个特定的属性和节点名称,而且也支持json的转换
XStream的tutorial
http://x-stream.github.io/tutorial.html
public KMeansDemo() {
}
@Override
public JComponent learn() {
long clock = System.currentTimeMillis();
KMeans kmeans = new KMeans(dataset[datasetIndex], clusterNumber, 100, 4);
System.out.format("K-Means clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis()-clock);
PlotCanvas plot = ScatterPlot.plot(dataset[datasetIndex], kmeans.getClusterLabel(), pointLegend, Palette.COLORS);
plot.points(kmeans.centroids(), '@');
XStream xstream = new XStream();
//Write model to file
try {
FileOutputStream fs = new FileOutputStream("~/playground/model.xml");
xstream.toXML(kmeans, fs);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
return plot;
}