私はsparklyr
のクラスタリング問題を解決しようとしています。トレーニングセットの変数の多くは、異なるスケールで測定され、したがって、桁違いに異なる。ベストプラクティスでは、私はデータをスケールして中心化しようとしています。mutate_eachを使用してスパークリヤの変数をスケールおよびセンタリングする
Xをランダム変数、μ=平均、σ=標準偏差とする最も伝統的な式(X - μ)/σがあります。私はX =確率変数、x =標本平均、x_max =最大値、x_min =最小値で(X-x)/(x_max-x_min)を使う傾向があります。
dplyr
を使用してこの変換を適用した後、私は奇妙な結果を得ています。この例を考えてみましょう:
#connect to spark
library(sparklyr)
library(SparkR)
library(dplyr)
sc = spark_connect(master = 'yarn-client',
spark_home = '/usr/hdp/current/spark-client',
app_name = 'sparklyr'
# config = list(
# "sparklyr.shell.executor-memory" = "XG",
# "sparklyr.shell.driver-memory" = "XG",
# "spark.driver.maxResultSize" = "XG" # may need to transfer a lot of data into R
)
sparkR.init()
#create a dataframe where variables in the dataset differ by an order of magnitude
mat <- as.data.frame(matrix(data = rnorm(200, mean=100,sd=10), nrow = 1000, ncol = 50))
mat1 <- as.data.frame(matrix(data = rnorm(200, mean=0,sd=1), nrow = 1000, ncol = 50))
colnames(mat1) <- paste('X',1:50,sep='')
mat.final <- cbind(mat,mat1)
#copy to Spark
dat.out <- sdf_copy_to(sc,mat.final,'dat',overwrite=TRUE)
#define centering and scaling function
scale.center <- function(x){
(x-mean(x,na.rm=TRUE)) /(max(x,na.rm = TRUE)-min(x,na.rm = TRUE))
}
#scale data
dat.out1 <-
dat.out %>%
mutate_each(funs(s=scale.center))
コードが実行されますが、私は奇妙な何かを得る:
str(dat.out1)
$ ops:List of 4
..$ name: chr "mutate"
..$ x :List of 4
.. ..$ name: chr "mutate"
.. ..$ x :List of 4
.. .. ..$ name: chr "mutate"
.. .. ..$ x :List of 4
.. .. .. ..$ name: chr "mutate"
.. .. .. ..$ x :List of 4
.. .. .. .. ..$ name: chr "mutate"
.. .. .. .. ..$ x :List of 4
.. .. .. .. .. ..$ name: chr "mutate"
.. .. .. .. .. ..$ x :List of 4
.. .. .. .. .. .. ..$ name: chr "mutate"
.. .. .. .. .. .. ..$ x :List of 4
.. .. .. .. .. .. .. ..$ name: chr "mutate"
.. .. .. .. .. .. .. ..$ x :List of 4
.. .. .. .. .. .. .. .. ..$ name: chr "mutate"
.. .. .. .. .. .. .. .. ..$ x :List of 4
.. .. .. .. .. .. .. .. .. ..$ name: chr "mutate"
.. .. .. .. .. .. .. .. .. ..$ x :List of 4
.. .. .. .. .. .. .. .. .. .. ..$ name: chr "mutate"
.. ..
上記はstr
を実行した後、出力のほんの一部です。ここで何がうまくいかないかについての考え。私はセンタリングとスケーリングのための機能が組み込まれていないことに驚いています。
'?scale'は基底付きで出荷されます – hrabel
SparkRでは実装されていません。 – schristel
ああ、申し訳ありません。したがって、 'scale'はあなたのコードに上記のエラーを生成しますか? 'mutate_each'は' mutate_all'のために廃止されているのではないのですか? – hrabel