-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-7578] [ml] [doc] User guide for spark.ml Normalizer, IDF, StandardScaler #6127
Changes from all commits
a21c2d6
0a862f9
f16bcec
cd47f4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,17 +63,22 @@ public void hashingTF() { | |
new StructField("label", DataTypes.DoubleType, false, Metadata.empty()), | ||
new StructField("sentence", DataTypes.StringType, false, Metadata.empty()) | ||
}); | ||
DataFrame sentenceDataFrame = jsql.createDataFrame(jrdd, schema); | ||
|
||
Tokenizer tokenizer = new Tokenizer().setInputCol("sentence").setOutputCol("words"); | ||
DataFrame wordsDataFrame = tokenizer.transform(sentenceDataFrame); | ||
DataFrame sentenceData = jsql.createDataFrame(jrdd, schema); | ||
Tokenizer tokenizer = new Tokenizer() | ||
.setInputCol("sentence") | ||
.setOutputCol("words"); | ||
DataFrame wordsData = tokenizer.transform(sentenceData); | ||
int numFeatures = 20; | ||
HashingTF hashingTF = new HashingTF() | ||
.setInputCol("words") | ||
.setOutputCol("features") | ||
.setOutputCol("rawFeatures") | ||
.setNumFeatures(numFeatures); | ||
DataFrame featurized = hashingTF.transform(wordsDataFrame); | ||
for (Row r : featurized.select("features", "words", "label").take(3)) { | ||
DataFrame featurizedData = hashingTF.transform(wordsData); | ||
IDF idf = new IDF().setInputCol("rawFeatures").setOutputCol("features"); | ||
IDFModel idfModel = idf.fit(featurizedData); | ||
DataFrame rescaledData = idfModel.transform(featurizedData); | ||
for (Row r : rescaledData.select("features", "label").take(3)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Vector features = r.getAs(0); | ||
Assert.assertEquals(features.size(), numFeatures); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.ml.feature; | ||
|
||
import java.util.List; | ||
|
||
import com.google.common.collect.Lists; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import org.apache.spark.api.java.JavaSparkContext; | ||
import org.apache.spark.mllib.linalg.Vectors; | ||
import org.apache.spark.sql.DataFrame; | ||
import org.apache.spark.sql.SQLContext; | ||
|
||
public class JavaNormalizerSuite { | ||
private transient JavaSparkContext jsc; | ||
private transient SQLContext jsql; | ||
|
||
@Before | ||
public void setUp() { | ||
jsc = new JavaSparkContext("local", "JavaNormalizerSuite"); | ||
jsql = new SQLContext(jsc); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
jsc.stop(); | ||
jsc = null; | ||
} | ||
|
||
@Test | ||
public void normalizer() { | ||
// The tests are to check Java compatibility. | ||
List<VectorIndexerSuite.FeatureData> points = Lists.newArrayList( | ||
new VectorIndexerSuite.FeatureData(Vectors.dense(0.0, -2.0)), | ||
new VectorIndexerSuite.FeatureData(Vectors.dense(1.0, 3.0)), | ||
new VectorIndexerSuite.FeatureData(Vectors.dense(1.0, 4.0)) | ||
); | ||
DataFrame dataFrame = jsql.createDataFrame(jsc.parallelize(points, 2), | ||
VectorIndexerSuite.FeatureData.class); | ||
Normalizer normalizer = new Normalizer() | ||
.setInputCol("features") | ||
.setOutputCol("normFeatures"); | ||
|
||
// Normalize each Vector using $L^2$ norm. | ||
DataFrame l2NormData = normalizer.transform(dataFrame, normalizer.p().w(2)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Call |
||
l2NormData.count(); | ||
|
||
// Normalize each Vector using $L^\infty$ norm. | ||
DataFrame lInfNormData = | ||
normalizer.transform(dataFrame, normalizer.p().w(Double.POSITIVE_INFINITY)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
lInfNormData.count(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor:
take(3)
->collect()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that, but was worried users would copy this into their own code and try to print out a huge dataset. What do you think?