aboutsummaryrefslogtreecommitdiff
path: root/examples/src/main/python/ml
diff options
context:
space:
mode:
authorsachin aggarwal <different.sachin@gmail.com>2015-11-09 14:25:42 -0800
committerXiangrui Meng <meng@databricks.com>2015-11-09 14:25:42 -0800
commit51d41e4b1a3a25a3fde3a4345afcfe4766023d23 (patch)
tree1b70dbcd2e113257ded3851c9adb4bc30449915c /examples/src/main/python/ml
parent5039a49b636325f321daa089971107003fae9d4b (diff)
downloadspark-51d41e4b1a3a25a3fde3a4345afcfe4766023d23.tar.gz
spark-51d41e4b1a3a25a3fde3a4345afcfe4766023d23.tar.bz2
spark-51d41e4b1a3a25a3fde3a4345afcfe4766023d23.zip
[SPARK-11552][DOCS][Replaced example code in ml-decision-tree.md using include_example]
I have tested it on my local, it is working fine, please review Author: sachin aggarwal <different.sachin@gmail.com> Closes #9539 from agsachin/SPARK-11552-real.
Diffstat (limited to 'examples/src/main/python/ml')
-rw-r--r--examples/src/main/python/ml/decision_tree_classification_example.py77
-rw-r--r--examples/src/main/python/ml/decision_tree_regression_example.py74
2 files changed, 151 insertions, 0 deletions
diff --git a/examples/src/main/python/ml/decision_tree_classification_example.py b/examples/src/main/python/ml/decision_tree_classification_example.py
new file mode 100644
index 0000000000..0af92050e3
--- /dev/null
+++ b/examples/src/main/python/ml/decision_tree_classification_example.py
@@ -0,0 +1,77 @@
+#
+# 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.
+#
+
+"""
+Decision Tree Classification Example.
+"""
+from __future__ import print_function
+
+import sys
+
+# $example on$
+from pyspark import SparkContext, SQLContext
+from pyspark.ml import Pipeline
+from pyspark.ml.classification import DecisionTreeClassifier
+from pyspark.ml.feature import StringIndexer, VectorIndexer
+from pyspark.ml.evaluation import MulticlassClassificationEvaluator
+from pyspark.mllib.util import MLUtils
+# $example off$
+
+if __name__ == "__main__":
+ sc = SparkContext(appName="decision_tree_classification_example")
+ sqlContext = SQLContext(sc)
+
+ # $example on$
+ # Load and parse the data file, converting it to a DataFrame.
+ data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt").toDF()
+
+ # Index labels, adding metadata to the label column.
+ # Fit on whole dataset to include all labels in index.
+ labelIndexer = StringIndexer(inputCol="label", outputCol="indexedLabel").fit(data)
+ # Automatically identify categorical features, and index them.
+ # We specify maxCategories so features with > 4 distinct values are treated as continuous.
+ featureIndexer =\
+ VectorIndexer(inputCol="features", outputCol="indexedFeatures", maxCategories=4).fit(data)
+
+ # Split the data into training and test sets (30% held out for testing)
+ (trainingData, testData) = data.randomSplit([0.7, 0.3])
+
+ # Train a DecisionTree model.
+ dt = DecisionTreeClassifier(labelCol="indexedLabel", featuresCol="indexedFeatures")
+
+ # Chain indexers and tree in a Pipeline
+ pipeline = Pipeline(stages=[labelIndexer, featureIndexer, dt])
+
+ # Train model. This also runs the indexers.
+ model = pipeline.fit(trainingData)
+
+ # Make predictions.
+ predictions = model.transform(testData)
+
+ # Select example rows to display.
+ predictions.select("prediction", "indexedLabel", "features").show(5)
+
+ # Select (prediction, true label) and compute test error
+ evaluator = MulticlassClassificationEvaluator(
+ labelCol="indexedLabel", predictionCol="prediction", metricName="precision")
+ accuracy = evaluator.evaluate(predictions)
+ print("Test Error = %g " % (1.0 - accuracy))
+
+ treeModel = model.stages[2]
+ # summary only
+ print(treeModel)
+ # $example off$
diff --git a/examples/src/main/python/ml/decision_tree_regression_example.py b/examples/src/main/python/ml/decision_tree_regression_example.py
new file mode 100644
index 0000000000..3857aed538
--- /dev/null
+++ b/examples/src/main/python/ml/decision_tree_regression_example.py
@@ -0,0 +1,74 @@
+#
+# 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.
+#
+
+"""
+Decision Tree Regression Example.
+"""
+from __future__ import print_function
+
+import sys
+
+from pyspark import SparkContext, SQLContext
+# $example on$
+from pyspark.ml import Pipeline
+from pyspark.ml.regression import DecisionTreeRegressor
+from pyspark.ml.feature import VectorIndexer
+from pyspark.ml.evaluation import RegressionEvaluator
+from pyspark.mllib.util import MLUtils
+# $example off$
+
+if __name__ == "__main__":
+ sc = SparkContext(appName="decision_tree_classification_example")
+ sqlContext = SQLContext(sc)
+
+ # $example on$
+ # Load and parse the data file, converting it to a DataFrame.
+ data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt").toDF()
+
+ # Automatically identify categorical features, and index them.
+ # We specify maxCategories so features with > 4 distinct values are treated as continuous.
+ featureIndexer =\
+ VectorIndexer(inputCol="features", outputCol="indexedFeatures", maxCategories=4).fit(data)
+
+ # Split the data into training and test sets (30% held out for testing)
+ (trainingData, testData) = data.randomSplit([0.7, 0.3])
+
+ # Train a DecisionTree model.
+ dt = DecisionTreeRegressor(featuresCol="indexedFeatures")
+
+ # Chain indexer and tree in a Pipeline
+ pipeline = Pipeline(stages=[featureIndexer, dt])
+
+ # Train model. This also runs the indexer.
+ model = pipeline.fit(trainingData)
+
+ # Make predictions.
+ predictions = model.transform(testData)
+
+ # Select example rows to display.
+ predictions.select("prediction", "label", "features").show(5)
+
+ # Select (prediction, true label) and compute test error
+ evaluator = RegressionEvaluator(
+ labelCol="label", predictionCol="prediction", metricName="rmse")
+ rmse = evaluator.evaluate(predictions)
+ print("Root Mean Squared Error (RMSE) on test data = %g" % rmse)
+
+ treeModel = model.stages[1]
+ # summary only
+ print(treeModel)
+ # $example off$