@@ -18,7 +18,7 @@ Add the following dependency to your POM file:
18
18
<dependency >
19
19
<groupId >com.github.chen0040</groupId >
20
20
<artifactId >java-clustering</artifactId >
21
- <version >1.0.2 </version >
21
+ <version >1.0.3 </version >
22
22
</dependency >
23
23
```
24
24
@@ -65,6 +65,49 @@ for(int i = 0; i < learnedData.rowCount(); ++i){
65
65
}
66
66
```
67
67
68
+ ### Spatial Segmentation using EM Clustering
69
+
70
+ The following sample code shows how to use EM clustering to separate two clusters:
71
+
72
+ ``` java
73
+ DataQuery . DataFrameQueryBuilder schema = DataQuery . blank()
74
+ .newInput(" c1" )
75
+ .newInput(" c2" )
76
+ .newOutput(" designed" )
77
+ .end();
78
+
79
+ Sampler . DataSampleBuilder negativeSampler = new Sampler ()
80
+ .forColumn(" c1" ). generate((name, index) - > randn() * 0.3 + (index % 2 == 0 ? 2 : 4 ))
81
+ .forColumn(" c2" ). generate((name, index) - > randn() * 0.3 + (index % 2 == 0 ? 2 : 4 ))
82
+ .forColumn(" designed" ). generate((name, index) - > 0.0 )
83
+ .end();
84
+
85
+ Sampler . DataSampleBuilder positiveSampler = new Sampler ()
86
+ .forColumn(" c1" ). generate((name, index) - > rand(- 4 , - 2 ))
87
+ .forColumn(" c2" ). generate((name, index) - > rand(- 2 , - 4 ))
88
+ .forColumn(" designed" ). generate((name, index) - > 1.0 )
89
+ .end();
90
+
91
+ DataFrame data = schema. build();
92
+
93
+ data = negativeSampler. sample(data, 50 );
94
+ data = positiveSampler. sample(data, 50 );
95
+
96
+ System . out. println(data. head(10 ));
97
+
98
+ EMClustering algorithm = new EMClustering ();
99
+ algorithm. setSigma0(1.5 );
100
+ algorithm. setClusterCount(2 );
101
+
102
+ DataFrame learnedData = algorithm. fitAndTransform(data);
103
+
104
+ for (int i = 0 ; i < learnedData. rowCount(); ++ i){
105
+ DataRow tuple = learnedData. row(i);
106
+ String clusterId = tuple. getCategoricalTargetCell(" cluster" );
107
+ System . out. println(" learned: " + clusterId + " \t known: " + tuple. target());
108
+ }
109
+ ```
110
+
68
111
### Spatial Segmentation using Single Linkage Clustering
69
112
70
113
The following sample code shows how to use single linkage clustering to separate two clusters:
0 commit comments