2016-11-03 3 views

答えて

0

私はそれを解決できました、あなたの助けに感謝@ user6910411。 データに応じて密度の高いベクトルまたは疎ベクトルを使用し、MaxAbsScalerに置き換えて、linalg.VectorsまたはDenseVectorを入力してください。 アイデアは、必要な正中および逆スケールのポイントでデータを分割し、両方のハーフをスケールしてDFをマージします。

import org.apache.spark.mllib.linalg.Vectors 
import org.apache.spark.ml.feature.Normalizer 
import org.apache.spark.ml.feature.MaxAbsScaler 
import org.apache.spark.ml.feature.MinMaxScaler 
import org.apache.spark.ml.feature.VectorAssembler 
import org.apache.spark.ml.linalg.DenseVector 
import org.apache.spark.sql.functions.udf 

val vectorToColumn = udf{ (x: DenseVector, index: Int) => x(index) } 

val gt50 = df.filter("score >= 55").select('id,('score * -1).as("score")) 
val lt50 = df.filter("score < 55") 

val assembler = new VectorAssembler() 
.setInputCols(Array("score")) 
.setOutputCol("features") 

val ass_lt50 = assembler.transform(lt50) 
val ass_gt50 = assembler.transform(gt50) 

val scaler = new MinMaxScaler() 
.setInputCol("features") 
.setOutputCol("featuresScaled") 
.setMax(100) 
.setMin(0) 

val feat_lt50 = scaler.fit(ass_lt50).transform(ass_lt50).drop('score) 
val feat_gt50 = scaler.fit(ass_gt50).transform(ass_gt50).drop('score) 

val scaled_lt50 = feat_lt50.select('id,round(
vectorToColumn(col("featuresScaled"),lit(0))).as("scaled_score")) 

val scaled_gt50 = feat_gt50.select('id,round(
vectorToColumn(col("featuresScaled"),lit(0))).as("scaled_score")) 

val scaled = scaled_lt50.unionAll(scaled_gt50) 
関連する問題