#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 8 09:27:08 2018 @author: luogan
""" from pyspark.ml import Pipeline
from pyspark.ml.regression import RandomForestRegressor
from pyspark.ml.feature import VectorIndexer
from pyspark.ml.evaluation import RegressionEvaluator from pyspark.sql import SparkSession spark= SparkSession\
.builder \
.appName("dataFrame") \
.getOrCreate() # Load and parse the data file, converting it to a DataFrame.
data = spark.read.format("libsvm").load("/home/luogan/lg/softinstall/spark-2.2.0-bin-hadoop2.7/data/mllib/sample_libsvm_data.txt") # Automatically identify categorical features, and index them.
# Set 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 RandomForest model.
rf = RandomForestRegressor(featuresCol="indexedFeatures") # Chain indexer and forest in a Pipeline
pipeline = Pipeline(stages=[featureIndexer, rf]) # 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) rfModel = model.stages[1]
print(rfModel) # summary only

 结果:

+----------+-----+--------------------+
|prediction|label| features|
+----------+-----+--------------------+
| 0.0| 0.0|(692,[95,96,97,12...|
| 0.3| 0.0|(692,[100,101,102...|
| 0.0| 0.0|(692,[123,124,125...|
| 0.05| 0.0|(692,[124,125,126...|
| 0.0| 0.0|(692,[124,125,126...|
+----------+-----+--------------------+
only showing top 5 rows Root Mean Squared Error (RMSE) on test data = 0.127949
RandomForestRegressionModel (uid=RandomForestRegressor_4acc9ab165e4f84f7169) with 20 trees

  

原文:https://blog.csdn.net/luoganttcc/article/details/80618336

PySpark 分类模型训练 参考:

https://blog.csdn.net/u013719780/article/details/51792097

pyspark RandomForestRegressor 随机森林回归的更多相关文章

  1. 机器学习之路:python 集成回归模型 随机森林回归RandomForestRegressor 极端随机森林回归ExtraTreesRegressor GradientBoostingRegressor回归 预测波士顿房价

    python3 学习机器学习api 使用了三种集成回归模型 git: https://github.com/linyi0604/MachineLearning 代码: from sklearn.dat ...

  2. 机器学习实战基础(三十八):随机森林 (五)RandomForestRegressor 之 用随机森林回归填补缺失值

    简介 我们从现实中收集的数据,几乎不可能是完美无缺的,往往都会有一些缺失值.面对缺失值,很多人选择的方式是直接将含有缺失值的样本删除,这是一种有效的方法,但是有时候填补缺失值会比直接丢弃样本效果更好, ...

  3. MATLAB随机森林回归模型

    MATLAB随机森林回归模型: 调用matlab自带的TreeBagger.m T=textread('E:\datasets-orreview\discretized-regression\10bi ...

  4. 机器学习实战基础(三十七):随机森林 (四)之 RandomForestRegressor 重要参数,属性与接口

    RandomForestRegressor class sklearn.ensemble.RandomForestRegressor (n_estimators=’warn’, criterion=’ ...

  5. Python机器学习笔记——随机森林算法

    随机森林算法的理论知识 随机森林是一种有监督学习算法,是以决策树为基学习器的集成学习算法.随机森林非常简单,易于实现,计算开销也很小,但是它在分类和回归上表现出非常惊人的性能,因此,随机森林被誉为“代 ...

  6. 随机森林random forest及python实现

    引言想通过随机森林来获取数据的主要特征 1.理论根据个体学习器的生成方式,目前的集成学习方法大致可分为两大类,即个体学习器之间存在强依赖关系,必须串行生成的序列化方法,以及个体学习器间不存在强依赖关系 ...

  7. 100天搞定机器学习|Day56 随机森林工作原理及调参实战(信用卡欺诈预测)

    本文是对100天搞定机器学习|Day33-34 随机森林的补充 前文对随机森林的概念.工作原理.使用方法做了简单介绍,并提供了分类和回归的实例. 本期我们重点讲一下: 1.集成学习.Bagging和随 ...

  8. RandomForest 随机森林算法与模型参数的调优

    公号:码农充电站pro 主页:https://codeshellme.github.io 本篇文章来介绍随机森林(RandomForest)算法. 1,集成算法之 bagging 算法 在前边的文章& ...

  9. [Python] 波士顿房价的7种模型(线性拟合、二次多项式、Ridge、Lasso、SVM、决策树、随机森林)的训练效果对比

    目录 1. 载入数据 列解释Columns: 2. 数据分析 2.1 预处理 2.2 可视化 3. 训练模型 3.1 线性拟合 3.2 多项式回归(二次) 3.3 脊回归(Ridge Regressi ...

随机推荐

  1. VDP

    Today VMware announced a new version on their backup product vSphere Data Protection. They gave it t ...

  2. Linux中Centos7下安装Mysql(更名为Mariadb)

    一.安装: yum install mariadb-server mariadb 二.启动服务: systemctl start mariadb 三.配置大小写敏感问题.和字符为utf8: vim / ...

  3. Python-统计svn变更代码行数

    1 #!/bin/bash/python 2 # -*-coding:utf-8-*- 3 #svn统计不同url代码行数变更脚本,过滤空行,不过滤注释. 4 import subprocess,os ...

  4. vue框架整体流程

    1.整体流程 (1)模板解析成render函数 (2)响应式监听 (3)首次渲染,显示页面,绑定依赖 (4)data属性变化,触发rerender 2.模板解析为render函数 参考上一篇博客. 模 ...

  5. JVM内的守护线程Deamon与用户线程User Thread

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6561771.html  一:守护线程Daemon 守护线程:Daemon在希腊神话中解作“守护神”,顾名思义就 ...

  6. Java多线程之syncrhoized内置互斥锁的用法详解

       转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827547.html    解决并行冲突最有效的方法就是加同步锁,主要有以下几种方法:   1:动态方法 ...

  7. 转:Flash 插件面板 DragonBonesDesignPanel 的绿色安装方法

    最近在cocos2d-js下捣腾Dragonbones.转一个文章,大家可以参考安装Dragonbones.关于这个Dragonbones,5月份的时候还用得好好的,cocos2d-js还能妥妥的加载 ...

  8. 【转载】Java 网络编程

      本文主要是自己在网络编程方面的学习总结,先主要介绍计算机网络方面的相关内容,包括计算机网络基础,OSI参考模型,TCP/IP协议簇,常见的网络协议等等,在此基础上,介绍Java中的网络编程. 一. ...

  9. 三种分布式对象主流技术——COM、Java和COBRA

    既上一遍,看到还有一遍将关于 对象的, 分布式对象, 故摘抄入下: 目前国际上,分布式对象技术有三大流派——COBRA.COM/DCOM和Java.CORBA技术是最早出现的,1991年OMG颁布了C ...

  10. Linux查看GPU使用情况

    watch -n 10 nvidia-smi 一.watch watch命令用来定时执行某个程序 二.nvidia-smi nvdia-smi是英伟达自带的GPU监控命令.