|
17 | 17 |
|
18 | 18 | package org.apache.spark.mllib.pmml.export |
19 | 19 |
|
20 | | -import org.dmg.pmml.Array.Type |
21 | | -import org.dmg.pmml.Cluster |
22 | | -import org.dmg.pmml.ClusteringField |
23 | | -import org.dmg.pmml.ClusteringModel |
24 | | -import org.dmg.pmml.ClusteringModel.ModelClass |
25 | | -import org.dmg.pmml.CompareFunctionType |
26 | | -import org.dmg.pmml.ComparisonMeasure |
27 | | -import org.dmg.pmml.ComparisonMeasure.Kind |
28 | | -import org.dmg.pmml.DataDictionary |
29 | | -import org.dmg.pmml.DataField |
30 | | -import org.dmg.pmml.DataType |
31 | | -import org.dmg.pmml.FieldName |
32 | | -import org.dmg.pmml.FieldUsageType |
33 | | -import org.dmg.pmml.MiningField |
34 | | -import org.dmg.pmml.MiningFunctionType |
35 | | -import org.dmg.pmml.MiningSchema |
36 | | -import org.dmg.pmml.OpType |
37 | | -import org.dmg.pmml.SquaredEuclidean |
| 20 | +import scala.{Array => SArray} |
| 21 | + |
| 22 | +import org.dmg.pmml._ |
| 23 | + |
38 | 24 | import org.apache.spark.mllib.clustering.KMeansModel |
39 | 25 |
|
40 | 26 | /** |
41 | 27 | * PMML Model Export for KMeansModel class |
42 | 28 | */ |
43 | 29 | private[mllib] class KMeansPMMLModelExport(model : KMeansModel) extends PMMLModelExport{ |
44 | 30 |
|
| 31 | + populateKMeansPMML(model) |
| 32 | + |
45 | 33 | /** |
46 | | - * Export the input KMeansModel model to PMML format |
| 34 | + * Export the input KMeansModel model to PMML format. |
47 | 35 | */ |
48 | | - populateKMeansPMML(model) |
49 | | - |
50 | 36 | private def populateKMeansPMML(model : KMeansModel): Unit = { |
51 | | - |
52 | | - pmml.getHeader().setDescription("k-means clustering") |
53 | | - |
54 | | - if(model.clusterCenters.length > 0){ |
55 | | - |
56 | | - val clusterCenter = model.clusterCenters(0) |
57 | | - |
58 | | - val fields = new Array[FieldName](clusterCenter.size) |
59 | | - |
60 | | - val dataDictionary = new DataDictionary() |
61 | | - |
62 | | - val miningSchema = new MiningSchema() |
63 | | - |
64 | | - val comparisonMeasure = new ComparisonMeasure() |
65 | | - .withKind(Kind.DISTANCE) |
66 | | - .withMeasure(new SquaredEuclidean() |
67 | | - ) |
68 | | - |
69 | | - val clusteringModel = new ClusteringModel(miningSchema, comparisonMeasure, |
70 | | - MiningFunctionType.CLUSTERING, ModelClass.CENTER_BASED, model.clusterCenters.length) |
| 37 | + pmml.getHeader.setDescription("k-means clustering") |
| 38 | + |
| 39 | + if (model.clusterCenters.length > 0) { |
| 40 | + val clusterCenter = model.clusterCenters(0) |
| 41 | + val fields = new SArray[FieldName](clusterCenter.size) |
| 42 | + val dataDictionary = new DataDictionary |
| 43 | + val miningSchema = new MiningSchema |
| 44 | + val comparisonMeasure = new ComparisonMeasure() |
| 45 | + .withKind(ComparisonMeasure.Kind.DISTANCE) |
| 46 | + .withMeasure(new SquaredEuclidean()) |
| 47 | + val clusteringModel = new ClusteringModel(miningSchema, comparisonMeasure, |
| 48 | + MiningFunctionType.CLUSTERING, ClusteringModel.ModelClass.CENTER_BASED, |
| 49 | + model.clusterCenters.length) |
71 | 50 | .withModelName("k-means") |
72 | | - |
73 | | - for ( i <- 0 until clusterCenter.size) { |
74 | | - fields(i) = FieldName.create("field_" + i) |
75 | | - dataDictionary |
76 | | - .withDataFields(new DataField(fields(i), OpType.CONTINUOUS, DataType.DOUBLE)) |
77 | | - miningSchema |
78 | | - .withMiningFields(new MiningField(fields(i)) |
79 | | - .withUsageType(FieldUsageType.ACTIVE)) |
80 | | - clusteringModel.withClusteringFields( |
81 | | - new ClusteringField(fields(i)).withCompareFunction(CompareFunctionType.ABS_DIFF) |
82 | | - ) |
83 | | - } |
84 | | - |
85 | | - dataDictionary.withNumberOfFields((dataDictionary.getDataFields()).size()) |
86 | | - |
87 | | - for ( i <- 0 until model.clusterCenters.size ) { |
88 | | - val cluster = new Cluster() |
89 | | - .withName("cluster_" + i) |
90 | | - .withArray(new org.dmg.pmml.Array() |
91 | | - .withType(Type.REAL) |
92 | | - .withN(clusterCenter.size) |
93 | | - .withValue(model.clusterCenters(i).toArray.mkString(" "))) |
94 | | - // we don't have the size of the single cluster but only the centroids (withValue) |
95 | | - // .withSize(value) |
96 | | - clusteringModel.withClusters(cluster) |
97 | | - } |
98 | | - |
99 | | - pmml.setDataDictionary(dataDictionary) |
100 | | - pmml.withModels(clusteringModel) |
101 | | - |
102 | | - } |
103 | | - |
| 51 | + |
| 52 | + for (i <- 0 until clusterCenter.size) { |
| 53 | + fields(i) = FieldName.create("field_" + i) |
| 54 | + dataDictionary.withDataFields(new DataField(fields(i), OpType.CONTINUOUS, DataType.DOUBLE)) |
| 55 | + miningSchema |
| 56 | + .withMiningFields(new MiningField(fields(i)) |
| 57 | + .withUsageType(FieldUsageType.ACTIVE)) |
| 58 | + clusteringModel.withClusteringFields( |
| 59 | + new ClusteringField(fields(i)).withCompareFunction(CompareFunctionType.ABS_DIFF)) |
| 60 | + } |
| 61 | + |
| 62 | + dataDictionary.withNumberOfFields(dataDictionary.getDataFields.size) |
| 63 | + |
| 64 | + for (i <- 0 until model.clusterCenters.length) { |
| 65 | + val cluster = new Cluster() |
| 66 | + .withName("cluster_" + i) |
| 67 | + .withArray(new org.dmg.pmml.Array() |
| 68 | + .withType(Array.Type.REAL) |
| 69 | + .withN(clusterCenter.size) |
| 70 | + .withValue(model.clusterCenters(i).toArray.mkString(" "))) |
| 71 | + // we don't have the size of the single cluster but only the centroids (withValue) |
| 72 | + // .withSize(value) |
| 73 | + clusteringModel.withClusters(cluster) |
| 74 | + } |
| 75 | + |
| 76 | + pmml.setDataDictionary(dataDictionary) |
| 77 | + pmml.withModels(clusteringModel) |
| 78 | + } |
104 | 79 | } |
105 | | - |
106 | 80 | } |
0 commit comments