aboutsummaryrefslogtreecommitdiff
path: root/examples/src/main/python
diff options
context:
space:
mode:
authorZheng RuiFeng <ruifengz@foxmail.com>2016-05-11 12:49:41 +0200
committerNick Pentreath <nickp@za.ibm.com>2016-05-11 12:49:41 +0200
commitd88afabdfa83be47f36d833105aadd6b818ceeee (patch)
tree224f8a6fc5268e30bf200ab30f4e20f5c0164b34 /examples/src/main/python
parentfafc95af79fa34f82964a86407c2ee046eda3814 (diff)
downloadspark-d88afabdfa83be47f36d833105aadd6b818ceeee.tar.gz
spark-d88afabdfa83be47f36d833105aadd6b818ceeee.tar.bz2
spark-d88afabdfa83be47f36d833105aadd6b818ceeee.zip
[SPARK-15150][EXAMPLE][DOC] Update LDA examples
## What changes were proposed in this pull request? 1,create a libsvm-type dataset for lda: `data/mllib/sample_lda_libsvm_data.txt` 2,add python example 3,directly read the datafile in examples 4,BTW, change to `SparkSession` in `aft_survival_regression.py` ## How was this patch tested? manual tests `./bin/spark-submit examples/src/main/python/ml/lda_example.py` Author: Zheng RuiFeng <ruifengz@foxmail.com> Closes #12927 from zhengruifeng/lda_pe.
Diffstat (limited to 'examples/src/main/python')
-rw-r--r--examples/src/main/python/ml/aft_survival_regression.py19
-rw-r--r--examples/src/main/python/ml/lda_example.py64
2 files changed, 77 insertions, 6 deletions
diff --git a/examples/src/main/python/ml/aft_survival_regression.py b/examples/src/main/python/ml/aft_survival_regression.py
index 0ee01fd825..9879679829 100644
--- a/examples/src/main/python/ml/aft_survival_regression.py
+++ b/examples/src/main/python/ml/aft_survival_regression.py
@@ -17,19 +17,26 @@
from __future__ import print_function
-from pyspark import SparkContext
-from pyspark.sql import SQLContext
# $example on$
from pyspark.ml.regression import AFTSurvivalRegression
from pyspark.mllib.linalg import Vectors
# $example off$
+from pyspark.sql import SparkSession
+
+"""
+An example demonstrating aft survival regression.
+Run with:
+ bin/spark-submit examples/src/main/python/ml/aft_survival_regression.py
+"""
if __name__ == "__main__":
- sc = SparkContext(appName="AFTSurvivalRegressionExample")
- sqlContext = SQLContext(sc)
+ spark = SparkSession \
+ .builder \
+ .appName("PythonAFTSurvivalRegressionExample") \
+ .getOrCreate()
# $example on$
- training = sqlContext.createDataFrame([
+ training = spark.createDataFrame([
(1.218, 1.0, Vectors.dense(1.560, -0.605)),
(2.949, 0.0, Vectors.dense(0.346, 2.158)),
(3.627, 0.0, Vectors.dense(1.380, 0.231)),
@@ -48,4 +55,4 @@ if __name__ == "__main__":
model.transform(training).show(truncate=False)
# $example off$
- sc.stop()
+ spark.stop()
diff --git a/examples/src/main/python/ml/lda_example.py b/examples/src/main/python/ml/lda_example.py
new file mode 100644
index 0000000000..6ca56adf3c
--- /dev/null
+++ b/examples/src/main/python/ml/lda_example.py
@@ -0,0 +1,64 @@
+#
+# 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.
+#
+
+
+from __future__ import print_function
+
+# $example on$
+from pyspark.ml.clustering import LDA
+# $example off$
+from pyspark.sql import SparkSession
+
+
+"""
+An example demonstrating LDA.
+Run with:
+ bin/spark-submit examples/src/main/python/ml/lda_example.py
+"""
+
+
+if __name__ == "__main__":
+ # Creates a SparkSession
+ spark = SparkSession \
+ .builder \
+ .appName("PythonKMeansExample") \
+ .getOrCreate()
+
+ # $example on$
+ # Loads data.
+ dataset = spark.read.format("libsvm").load("data/mllib/sample_lda_libsvm_data.txt")
+
+ # Trains a LDA model.
+ lda = LDA(k=10, maxIter=10)
+ model = lda.fit(dataset)
+
+ ll = model.logLikelihood(dataset)
+ lp = model.logPerplexity(dataset)
+ print("The lower bound on the log likelihood of the entire corpus: " + str(ll))
+ print("The upper bound bound on perplexity: " + str(lp))
+
+ # Describe topics.
+ topics = model.describeTopics(3)
+ print("The topics described by their top-weighted terms:")
+ topics.show(truncate=False)
+
+ # Shows the result
+ transformed = model.transform(dataset)
+ transformed.show(truncate=False)
+ # $example off$
+
+ spark.stop()