#!/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. 神器phpstorm功能具体解释

    phpstorm包括了webstorm的所有功能,更可以支持php代码. PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提供用户效率,可深刻理解用户的编码,提供智能代码补全,高速导航以及即 ...

  2. 算法笔记_231:网格中移动字母(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 2x3=6个方格中放入ABCDE五个字母,右下角的那个格空着.如图[1.jpg]所示. 和空格子相邻的格子中的字母可以移动到空格中,比如,图中的C和 ...

  3. html中文显示乱码的处理方法

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 1. ht ...

  4. Aerospike系列:4:简单的增删改查aql

    [root@localhost bin]# aql --help Usage: aql OPTIONS OPTIONS -h <host> The hostname to the serv ...

  5. js setTimeout setInterval 第三个参数说明

    1.api setTimeout: https://developer.mozilla.org/zh-CN/docs/Web/API/Window/setTimeout var timeoutID = ...

  6. 阿里JAVA开发面试常问问题总结2

    线程与进程 进程是可并发运行的程序在某个数据集合上的一次计算活动.也是操作系统进行资源分配和调度的基本单位. 线程是操作系统进程中能够并发运行的实体,是处理器调度和分派的基本单位. 每一个进程内可包括 ...

  7. CentOS6.x 图形界面(gnome)安装

    CentOS6默认的安装选项 Desktop :基本的桌面系统,包括常用的桌面软件,如文档查看工具. Minimal Desktop :基本的桌面系统,包含的软件更少. Minimal :基本的系统, ...

  8. spring mvc简单的demo(注解版)

    tomcat配置文件:web.xml <?xml version="1.0" encoding="UTF-8"? > <web-app ver ...

  9. 亲历H5移动端游戏微信支付接入及那些坑(一)——支付方式与坑

    最近项目进入中后期,开始接入支付.要求是使用微信支付,呵呵,好笑的是不知老板从哪里听来的,居然和我说只要是熟手,接个微信支付两小时搞定,我只能再次呵呵.先不说支付处理逻辑,而且公司本来也没现成的接入模 ...

  10. python模块之imghdr(识别不同格式的图片文件)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python模块之imghdr(识别不同格式的图片文件) import imghdr '''>> ...