Spark.ML之PipeLine学习笔记
地址:
#导入向量和模型from pyspark.ml.linalg importVectorsfrom pyspark.ml.classification importLogisticRegression#准备训练数据# Prepare training data from a list of (label, features) tuples.training = spark.createDataFrame([(1.0,Vectors.dense([0.0,1.1,0.1])),(0.0,Vectors.dense([2.0,1.0,-1.0])),(0.0,Vectors.dense([2.0,1.3,1.0])),(1.0,Vectors.dense([0.0,1.2,-0.5]))],["label","features"])#创建回归实例,这个实例是Estimator# Create a LogisticRegression instance. This instance is an Estimator.lr =LogisticRegression(maxIter=10, regParam=0.01)#打印出参数和文档# Print out the parameters, documentation, and any default values.print"LogisticRegression parameters:\n"+ lr.explainParams()+"\n"#使用Ir中的参数训练出Model1# Learn a LogisticRegression model. This uses the parameters stored in lr.model1 = lr.fit(training)# Since model1 is a Model (i.e., a transformer produced by an Estimator),# we can view the parameters it used during fit().# This prints the parameter (name: value) pairs, where names are unique IDs for this# LogisticRegression instance.#查看model1在fit()中使用的参数print"Model 1 was fit using parameters: "print model1.extractParamMap()#修改其中的一个参数# We may alternatively specify parameters using a Python dictionary as a paramMapparamMap ={lr.maxIter:20}#覆盖掉paramMap[lr.maxIter]=30# Specify 1 Param, overwriting the original maxIter.#更新参数对paramMap.update({lr.regParam:0.1, lr.threshold:0.55})# Specify multiple Params.# You can combine paramMaps, which are python dictionaries.#新的参数,合并为两组参数对paramMap2 ={lr.probabilityCol:"myProbability"}# Change output column nameparamMapCombined = paramMap.copy()paramMapCombined.update(paramMap2)#重新得到model2并拿出来参数看看# Now learn a new model using the paramMapCombined parameters.# paramMapCombined overrides all parameters set earlier via lr.set* methods.model2 = lr.fit(training, paramMapCombined)print"Model 2 was fit using parameters: "print model2.extractParamMap()#准备测试的数据# Prepare test datatest = spark.createDataFrame([(1.0,Vectors.dense([-1.0,1.5,1.3])),(0.0,Vectors.dense([3.0,2.0,-0.1])),(1.0,Vectors.dense([0.0,2.2,-1.5]))],["label","features"])# Make predictions on test data using the Transformer.transform() method.# LogisticRegression.transform will only use the 'features' column.# Note that model2.transform() outputs a "myProbability" column instead of the usual# 'probability' column since we renamed the lr.probabilityCol parameter previously.prediction = model2.transform(test)#得到预测的DataFrame打印出预测中的选中列selected = prediction.select("features","label","myProbability","prediction")for row in selected.collect():print row
from pyspark.ml importPipelinefrom pyspark.ml.classification importLogisticRegressionfrom pyspark.ml.feature importHashingTF,Tokenizer#准备测试数据# Prepare training documents from a list of (id, text, label) tuples.training = spark.createDataFrame([(0L,"a b c d e spark",1.0),(1L,"b d",0.0),(2L,"spark f g h",1.0),(3L,"hadoop mapreduce",0.0)],["id","text","label"])#构建机器学习流水线# Configure an ML pipeline, which consists of three stages: tokenizer, hashingTF, and lr.tokenizer =Tokenizer(inputCol="text", outputCol="words")hashingTF =HashingTF(inputCol=tokenizer.getOutputCol(), outputCol="features")lr =LogisticRegression(maxIter=10, regParam=0.01)pipeline =Pipeline(stages=[tokenizer, hashingTF, lr])#训练出model# Fit the pipeline to training documents.model = pipeline.fit(training)#测试数据# Prepare test documents, which are unlabeled (id, text) tuples.test = spark.createDataFrame([(4L,"spark i j k"),(5L,"l m n"),(6L,"mapreduce spark"),(7L,"apache hadoop")],["id","text"])#预测,打印出想要的结果# Make predictions on test documents and print columns of interest.prediction = model.transform(test)selected = prediction.select("id","text","prediction")for row in selected.collect():print(row)
Spark.ML之PipeLine学习笔记的更多相关文章
- ML机器学习导论学习笔记
机器学习的定义: 机器学习(Machine Learning, ML)是一门多领域交叉学科,涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多门学科.专门研究计算机怎样模拟或实现人类的学习行为,以 ...
- Spark ML下实现的多分类adaboost+naivebayes算法在文本分类上的应用
1. Naive Bayes算法 朴素贝叶斯算法算是生成模型中一个最经典的分类算法之一了,常用的有Bernoulli和Multinomial两种.在文本分类上经常会用到这两种方法.在词袋模型中,对于一 ...
- spark ML pipeline 学习
一.pipeline 一个典型的机器学习过程从数据收集开始,要经历多个步骤,才能得到需要的输出.这非常类似于流水线式工作,即通常会包含源数据ETL(抽取.转化.加载),数据预处理,指标提取,模型训练与 ...
- 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL
周末的任务是更新Learning Spark系列第三篇,以为自己写不完了,但为了改正拖延症,还是得完成给自己定的任务啊 = =.这三章主要讲Spark的运行过程(本地+集群),性能调优以及Spark ...
- spark学习笔记总结-spark入门资料精化
Spark学习笔记 Spark简介 spark 可以很容易和yarn结合,直接调用HDFS.Hbase上面的数据,和hadoop结合.配置很容易. spark发展迅猛,框架比hadoop更加灵活实用. ...
- 使用spark ml pipeline进行机器学习
一.关于spark ml pipeline与机器学习 一个典型的机器学习构建包含若干个过程 1.源数据ETL 2.数据预处理 3.特征选取 4.模型训练与验证 以上四个步骤可以抽象为一个包括多个步骤的 ...
- Spark ML Pipeline简介
Spark ML Pipeline基于DataFrame构建了一套High-level API,我们可以使用MLPipeline构建机器学习应用,它能够将一个机器学习应用的多个处理过程组织起来,通过在 ...
- 机器学习框架ML.NET学习笔记【6】TensorFlow图片分类
一.概述 通过之前两篇文章的学习,我们应该已经了解了多元分类的工作原理,图片的分类其流程和之前完全一致,其中最核心的问题就是特征的提取,只要完成特征提取,分类算法就很好处理了,具体流程如下: 之前介绍 ...
- 机器学习框架ML.NET学习笔记【7】人物图片颜值判断
一.概述 这次要解决的问题是输入一张照片,输出人物的颜值数据. 学习样本来源于华南理工大学发布的SCUT-FBP5500数据集,数据集包括 5500 人,每人按颜值魅力打分,分值在 1 到 5 分之间 ...
随机推荐
- Windows cmd 颜色,字体,color font set up
windows的cmds默认的字体很丑,丑的不认直视,『如花』一般. 但是总有用到的时候 这是我有优化的一种结果,怎么来弄呢 要字体颜色漂亮,先要在注册表的Console中注册你要使用的字体,这个至关 ...
- 使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录
使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录 C#进阶系列——WebApi 异常处理解决方案 [ASP.NET Web API教程]4.3 AS ...
- Dijkstra 算法、Kruskal 算法、Prim算法、floyd算法
1.dijkstra算法 算最短路径的,算法解决的是有向图中单个源点到其他顶点的最短路径问题. 初始化n*n的数组. 2.kruskal算法 算最小生成树的,按权值加入 3.Prim算法 类似dijk ...
- 探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法
前言相信这样一个问题,大家都不会陌生,“有什么的方法可以使Android的程序APK不用安装,而能够直接启动”.发现最后的结局都是不能实现这个美好的愿望,而腾讯Android手机游戏平台却又能实现这个 ...
- okhttp封装
对这玩意并不熟,网上有很多大神封装好的,但是懒得看里面的封装逻辑,索性自己简单做个封装,方便使用,出现bug也好查找: get请求: /** * get请求 * @param url * @param ...
- 【转】MYSQL入门学习之二:使用正则表达式搜索
转载地址:http://www.2cto.com/database/201212/173869.html 一.正则表达式介绍 www.2cto.com 正则表达式是用来匹配文本的特殊的串(字符 ...
- syntactically incorrect() 404
遇到这个错误就来记录一下吧. 这个错误是springMVC报出的错误.大致的意思就是form传值的类型和controller中的参数不符. 有可能是名字错误,有可能是类型不对.比如前面你传来的是 p ...
- 磕磕碰碰的Chrome之plugin开发
前言 在Firefox下可用的npapi插件,在chrome下调用时遇到问题,于是尝试研究chrome下的ppapi插件,一路上真是磕磕碰碰,波折不断啊. 阶段一.复用npapi 尝试将npapi直接 ...
- CentOS 5/6.X 使用 EPEL YUM源
参考:http://www.linuxidc.com/Linux/2013-08/88523.htm 大纲 一.什么是EPEL? 二.与163 YUM源比较 三.CentOS 5.X 安装使用EPEL ...
- Linux平台下线程池的原理及实现
转自:http://blog.csdn.net/lmh12506/article/details/7753952 前段时间在github上开了个库,准备实现自己的线程池的,因为换工作的事,一直也没有实 ...