您可以使用rJava
將java連接到R。它比較簡單。我將在下面展示一個我將使用的場景。
首先我寫一個R代碼來聚類數據,並使用R函數繪製它們。例如,你可以這樣做:
x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
(cl <- kmeans(x, 2)) ## you replace kmeans by your call to java function
plot(x, col = cl$cluster)
points(cl$centers, col = 1:2, pch = 8, cex = 2)
然後你通過調用替換調用kmeans
到你的java功能:
library(rJava)
.jinit(PATH-TO_YOUR_CLASS_BIN_OR_JAR) # this starts the JVM
## I call a the Cluster constructor giving it the imput data
## Obvsiouly you should create this constructor
javaCluster <- .jnew("Cluster",.jarray(x,dispatch=TRUE))
## call th clustering function which returns a vector of integers
cl <- .jcall(javaCluster ,"[I",method="doClustering")
並不清楚你是什麼意思'這裏attributes'?屬性是你的輸入數據? – agstudy
@agstudy屬性是我的輸入數據 – Abhilash