- 阅读权限
- 255
- 威望
- 1 级
- 论坛币
- 49437 个
- 通用积分
- 52.4104
- 学术水平
- 370 点
- 热心指数
- 273 点
- 信用等级
- 335 点
- 经验
- 57815 点
- 帖子
- 4006
- 精华
- 21
- 在线时间
- 582 小时
- 注册时间
- 2005-5-8
- 最后登录
- 2023-11-26
学术权威
还不是VIP/贵宾
TA的文库 其他... R资源总汇
Panel Data Analysis
Experimental Design
- 威望
- 1 级
- 论坛币
![](https://bbs-cdn.datacourse.cn/static/pinggu/images/forumcoin_new.png) - 49437 个
- 通用积分
- 52.4104
- 学术水平
- 370 点
- 热心指数
- 273 点
- 信用等级
- 335 点
- 经验
- 57815 点
- 帖子
- 4006
- 精华
- 21
- 在线时间
- 582 小时
- 注册时间
- 2005-5-8
- 最后登录
- 2023-11-26
![](source/plugin/dsu_paulsign/img/emot/kx.gif) | 开心 2017-10-21 10:25:33 |
---|
签到天数: 1 天 连续签到: 1 天 [LV.1]初来乍到
|
经管之家送您一份
应届毕业生专属福利!
求职就业群
感谢您参与论坛问题回答
经管之家送您两个论坛币!
+2 论坛币
- from __future__ import print_function
- import sys
- from pyspark import SparkContext
- from pyspark.ml.classification import GBTClassifier
- from pyspark.ml.feature import StringIndexer
- from pyspark.ml.regression import GBTRegressor
- from pyspark.mllib.evaluation import BinaryClassificationMetrics, RegressionMetrics
- from pyspark.sql import Row, SQLContext
- """
- A simple example demonstrating a Gradient Boosted Trees Classification/Regression Pipeline.
- Note: GBTClassifier only supports binary classification currently
- Run with:
- bin/spark-submit examples/src/main/python/ml/gradient_boosted_trees.py
- """
- def testClassification(train, test):
- # Train a GradientBoostedTrees model.
- rf = GBTClassifier(maxIter=30, maxDepth=4, labelCol="indexedLabel")
- model = rf.fit(train)
- predictionAndLabels = model.transform(test).select("prediction", "indexedLabel") \
- .map(lambda x: (x.prediction, x.indexedLabel))
- metrics = BinaryClassificationMetrics(predictionAndLabels)
- print("AUC %.3f" % metrics.areaUnderROC)
- def testRegression(train, test):
- # Train a GradientBoostedTrees model.
- rf = GBTRegressor(maxIter=30, maxDepth=4, labelCol="indexedLabel")
- model = rf.fit(train)
- predictionAndLabels = model.transform(test).select("prediction", "indexedLabel") \
- .map(lambda x: (x.prediction, x.indexedLabel))
- metrics = RegressionMetrics(predictionAndLabels)
- print("rmse %.3f" % metrics.rootMeanSquaredError)
- print("r2 %.3f" % metrics.r2)
- print("mae %.3f" % metrics.meanAbsoluteError)
- if __name__ == "__main__":
- if len(sys.argv) > 1:
- print("Usage: gradient_boosted_trees", file=sys.stderr)
- exit(1)
- sc = SparkContext(appName="PythonGBTExample")
- sqlContext = SQLContext(sc)
- # Load the data stored in LIBSVM format as a DataFrame.
- df = sqlContext.read.format("libsvm").load("data/mllib/sample_libsvm_data.txt")
- # Map labels into an indexed column of labels in [0, numLabels)
- stringIndexer = StringIndexer(inputCol="label", outputCol="indexedLabel")
- si_model = stringIndexer.fit(df)
- td = si_model.transform(df)
- [train, test] = td.randomSplit([0.7, 0.3])
- testClassification(train, test)
- testRegression(train, test)
- sc.stop()
复制代码
扫码加我 拉你入群
请注明:姓名-公司-职位
以便审核进群资格,未注明则拒绝
|
|
|