|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.examples.ml; |
| 19 | + |
| 20 | +import org.apache.spark.sql.SparkSession; |
| 21 | + |
| 22 | +// $example on$ |
| 23 | +import org.apache.spark.ml.feature.RobustScaler; |
| 24 | +import org.apache.spark.ml.feature.RobustScalerModel; |
| 25 | +import org.apache.spark.sql.Dataset; |
| 26 | +import org.apache.spark.sql.Row; |
| 27 | +// $example off$ |
| 28 | + |
| 29 | +public class JavaRobustScalerExample { |
| 30 | + public static void main(String[] args) { |
| 31 | + SparkSession spark = SparkSession |
| 32 | + .builder() |
| 33 | + .appName("JavaRobustScalerExample") |
| 34 | + .getOrCreate(); |
| 35 | + |
| 36 | + // $example on$ |
| 37 | + Dataset<Row> dataFrame = |
| 38 | + spark.read().format("libsvm").load("data/mllib/sample_libsvm_data.txt"); |
| 39 | + |
| 40 | + RobustScaler scaler = new RobustScaler() |
| 41 | + .setInputCol("features") |
| 42 | + .setOutputCol("scaledFeatures") |
| 43 | + .setWithScaling(true) |
| 44 | + .setWithCentering(false) |
| 45 | + .setLower(0.25) |
| 46 | + .setUpper(0.75); |
| 47 | + |
| 48 | + // Compute summary statistics by fitting the RobustScaler |
| 49 | + RobustScalerModel scalerModel = scaler.fit(dataFrame); |
| 50 | + |
| 51 | + // Transform each feature to have unit quantile range. |
| 52 | + Dataset<Row> scaledData = scalerModel.transform(dataFrame); |
| 53 | + scaledData.show(); |
| 54 | + // $example off$ |
| 55 | + spark.stop(); |
| 56 | + } |
| 57 | +} |
0 commit comments