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 ...
随机推荐
- 更新maven库
删除所在本地仓库的文件夹,例如: 然后,重新下载一个jar包,放到工程的文件夹,注意名字要不同,否则maven库不予更新,替换完成后,点击工程,maven,update project即可,再次查看M ...
- ubuntu一些常用的命令
1.docker里的ubuntu不知道密码,更新密码 sudo passwd 2.解压zip文件 unzip xx.zip 3.安装LAMP (1)sudo apt-get install apach ...
- java:IO流学习小结
可以看以下内容学习一下: http://blog.csdn.net/zzp_403184692/article/details/8057693
- Hibernate ManyToOne, OneToMany的理解
User to Group, Many to One的关系 Test Case 1: session.beginTransaction(); user.setGroup(group); session ...
- 微信小程序-表单组件
button 按钮 注:button-hover 默认为{background-color: rgba(0, 0, 0, 0.1); opacity: 0.7;} 示例代码: /** wxss **/ ...
- 1000【入门】熟悉一下Online Judge的环境
var a,b:longint; begin read(a,b); writeln(a+b); end. #include <stdio.h> int main() { int a,b; ...
- Redis3.20阅读-SDS实现
声明:这是本人参考黄建宏的<redis设计与实现>(源码版本是redis3.0)来学习redis3.20源码的笔记,如果有什么不对的地方,欢迎大家指正,大家一起学习.一起进步,QQ:499 ...
- img和css背景的选择
在什么情况下更适合使用HTML IMG标签来显示一个图像,而不是一个CSS有背景图像,反之亦然? 如下场景使用img标签比较合适: 1.如果图像是等内容的一部分或图表或人(真正的人,而不是股票图人), ...
- rxjava源码中的线程知识
rxjava源码中的线程知识 rx的最精简的总结就是:异步 这里说一下以下的五个类 1.Future2.ConcurrentLinkedQueue3.volatile关键字4.AtomicRefere ...
- 运行Shell脚本的几种方式解析
1 给脚本加上执行权限chmod u+x a.sh, 而后就可以直接用全路径来执行脚本了,比如当前文件夹下用./a.sh, 如果脚本所在目录在PATH环境变量之中, 则直接用a.sh即可 2 sh/b ...