Spark机器学习示例
1. Java代码
/*
* 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.
*/ package com.XXX.YYY.hello; import java.util.regex.Pattern; import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function; import org.apache.spark.mllib.classification.LogisticRegressionWithSGD;
import org.apache.spark.mllib.classification.LogisticRegressionModel;
import org.apache.spark.mllib.linalg.Vectors;
import org.apache.spark.mllib.regression.LabeledPoint;
import org.apache.spark.mllib.linalg.DenseVector;
/**
* Logistic regression based classification using ML Lib.
*/
public final class JavaLR { static class ParsePoint implements Function<String, LabeledPoint> {
private static final Pattern COMMA = Pattern.compile(",");
private static final Pattern SPACE = Pattern.compile(" "); @Override
public LabeledPoint call(String line) {
String[] parts = COMMA.split(line);
double y = Double.parseDouble(parts[0]);
String[] tok = SPACE.split(parts[1]);
double[] x = new double[tok.length];
for (int i = 0; i < tok.length; ++i) {
x[i] = Double.parseDouble(tok[i]);
}
return new LabeledPoint(y, Vectors.dense(x));
}
} public static void main(String[] args) {
if (args.length != 3) {
System.err.println("Usage: JavaLR <input_dir> <step_size> <niters>");
System.exit(1);
}
SparkConf sparkConf = new SparkConf().setAppName("JavaLR");
JavaSparkContext sc = new JavaSparkContext(sparkConf);
JavaRDD<String> lines = sc.textFile(args[0]);
JavaRDD<LabeledPoint> points = lines.map(new ParsePoint()).cache();
double stepSize = Double.parseDouble(args[1]);
int iterations = Integer.parseInt(args[2]); // Another way to configure LogisticRegression
//
// LogisticRegressionWithSGD lr = new LogisticRegressionWithSGD();
// lr.optimizer().setNumIterations(iterations)
// .setStepSize(stepSize)
// .setMiniBatchFraction(1.0);
// lr.setIntercept(true);
// LogisticRegressionModel model = lr.train(points.rdd()); LogisticRegressionModel model = LogisticRegressionWithSGD.train(points.rdd(),
iterations, stepSize); System.out.print("Final w: " + model.weights() + "and intercept is " + model.intercept() + "\n");
double[] point = new double[2];
point[0] = 8;
point[1] = 8;
double label = model.predictPoint(new DenseVector(point), model.weights(), model.intercept());
System.out.print("label for [" + point[0] + " " + point[1] + "] is " + label + "\n");
sc.stop();
}
}
2. 数据文件
0,0 0
0,1 2
0,1 3
0,2 1
0,3 1
0,2 2
1,6 5
1,7 6
1,8 6
1,6 7
3. 执行命令
# spark-submit --class com.XXX.YYY.hello.JavaLR --master yarn --deploy-mode cluster ./hello-1.0-SNAPSHOT-jar-with-dependencies.jar /lr.training.txt 0.2 100
/lr.training.txt放在hadoop的根目录
4. 执行结果
Final w: [0.1618320065279109,0.03974871803971457]and intercept is 0.0
label for [8.0 8.0] is 1.0
Spark机器学习示例的更多相关文章
- Spark机器学习· 实时机器学习
Spark机器学习 1 在线学习 模型随着接收的新消息,不断更新自己:而不是像离线训练一次次重新训练. 2 Spark Streaming 离散化流(DStream) 输入源:Akka actors. ...
- Spark机器学习 Day2 快速理解机器学习
Spark机器学习 Day2 快速理解机器学习 有两个问题: 机器学习到底是什么. 大数据机器学习到底是什么. 机器学习到底是什么 人正常思维的过程是根据历史经验得出一定的规律,然后在当前情况下根据这 ...
- Spark机器学习 Day1 机器学习概述
Spark机器学习 Day1 机器学习概述 今天主要讨论个问题:Spark机器学习的本质是什么,其内部构成到底是什么. 简单来说,机器学习是数据+算法. 数据 在Spark中做机器学习,肯定有数据来源 ...
- Spark机器学习笔记一
Spark机器学习库现支持两种接口的API:RDD-based和DataFrame-based,Spark官方网站上说,RDD-based APIs在2.0后进入维护模式,主要的机器学习API是spa ...
- Spark机器学习之协同过滤算法
Spark机器学习之协同过滤算法 一).协同过滤 1.1 概念 协同过滤是一种借助"集体计算"的途径.它利用大量已有的用户偏好来估计用户对其未接触过的物品的喜好程度.其内在思想是相 ...
- 2019-1-18 Spark 机器学习
2019-1-18 Spark 机器学习 机器学习 模MLib板 预测 //有视频 后续会补充 1547822490122.jpg 1547822525716.jpg 1547822330358.jp ...
- Spark机器学习解析下集
上次我们讲过<Spark机器学习(上)>,本文是Spark机器学习的下部分,请点击回顾上部分,再更好地理解本文. 1.机器学习的常见算法 常见的机器学习算法有:l 构造条件概率:回归分 ...
- Spark机器学习8· 文本处理(spark-shell)
Spark机器学习 自然语言处理(NLP,Natural Language Processing) 提取特征 建模 机器学习 TF-IDF(词频 term frequency–逆向文件频率 inver ...
- Spark机器学习7·降维模型(scala&python)
PCA(主成分分析法,Principal Components Analysis) SVD(奇异值分解法,Singular Value Decomposition) http://vis-www.cs ...
随机推荐
- Mac > MacBook Pro的移动硬盘方案
灵感来自:http://cone.cc/2012/12/30/Macbook-Air-HD/ 主要为了解决:如果让移动硬盘,无缝地,安全地,同时应用在苹果电脑系统和微软的系统. 除了储存文件,我还想用 ...
- Mybatis映射文件
Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...
- 删除github仓库中的某个文件夹
最近在做一个项目,由于前期文件夹名是中文,如下: |---Repository |--- React单页面音乐播放器 并且git push到了github上. 后来在本地把文件夹re ...
- dsp28377控制DM9000收发数据——第二版程序,能够实现手术功能,但是容易掉帧;使用读取中断寄存器的方式判断中断
G:\controlSUITE\device_support\F2837xD\v180\F2837xD_examples_Cpu1\emif1_16bit_asram\cpu01\emif1_16bi ...
- JS对象深刻理解 - 2
JavaScript prototype 用过JavaScript的同学们肯定都对prototype如雷贯耳,但是这究竟是个什么东西却让初学者莫衷一是,只知道函数都会有一个prototype属性, ...
- kafka使用getOffsetsBefore()获取获取offset异常分析
根据时间戳获取kafka的topic的偏移量,结果获取的偏移量量数据组的长度为0,就会出现如下的数组下标越界的异常,实现的原理是使用了kafka的getOffsetsBefore()方法: Excep ...
- logback 配置详解(二)——appender
1.appender <appender>是<configuration>的子节点,是负责写日志的组件. <appender>有两个必要属性name和class.n ...
- iOS - Safe iOS 加密安全
1.Base64 编码 简介: Base64 是一种基于64个可打印字符来表示二进制数据的表示方法,可打印字符包括字母 A-Z.a-z.0-9,共 62 个字符,另外两个符号在不同的系统不同 +,/. ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- 转:java怎么用一行代码初始化ArrayList
java怎么用一行代码初始化ArrayList 您可以创建一个工厂方法: public static ArrayList<String> createArrayList(String .. ...