mapreduce:Kmeans算法处理西瓜数据集代码

时间: 2023-11-28 20:08:22 浏览: 42
以下是使用MapReduce实现K-Means算法处理西瓜数据集的代码示例: Map函数: ``` public static class KMeansMap extends Mapper<LongWritable, Text, IntWritable, Text> { private List<Vector> centers = new ArrayList<Vector>(); @Override protected void setup(Context context) throws IOException, InterruptedException { Configuration conf = context.getConfiguration(); String centerFilePath = conf.get("centerFilePath"); FileSystem fs = FileSystem.get(conf); Path centerPath = new Path(centerFilePath); BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(centerPath))); String line; while ((line = br.readLine()) != null) { String[] centerStr = line.split(","); double[] center = new double[centerStr.length]; for (int i = 0; i < centerStr.length; i++) { center[i] = Double.parseDouble(centerStr[i]); } centers.add(new DenseVector(center)); } br.close(); } @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] dataStr = value.toString().split(","); double[] data = new double[dataStr.length]; for (int i = 0; i < dataStr.length; i++) { data[i] = Double.parseDouble(dataStr[i]); } Vector vector = new DenseVector(data); int nearestCenterIndex = 0; double minDistance = Double.MAX_VALUE; for (int i = 0; i < centers.size(); i++) { double distance = DistanceMeasure.cosine(centers.get(i), vector); if (distance < minDistance) { minDistance = distance; nearestCenterIndex = i; } } context.write(new IntWritable(nearestCenterIndex), value); } } ``` Reduce函数: ``` public static class KMeansReduce extends Reducer<IntWritable, Text, IntWritable, Text> { @Override protected void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException { List<Vector> vectors = new ArrayList<Vector>(); for (Text value : values) { String[] dataStr = value.toString().split(","); double[] data = new double[dataStr.length]; for (int i = 0; i < dataStr.length; i++) { data[i] = Double.parseDouble(dataStr[i]); } Vector vector = new DenseVector(data); vectors.add(vector); } Vector newCenter = KMeansUtil.calculateCenter(vectors); context.write(key, new Text(KMeansUtil.vectorToString(newCenter))); } } ``` Driver函数: ``` public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); conf.set("centerFilePath", args[2]); Job job = Job.getInstance(conf); job.setJobName("KMeans"); job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(Text.class); job.setMapperClass(KMeansMap.class); job.setReducerClass(KMeansReduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); } ``` 这里假设KMeansUtil类中已经实现了calculateCenter和vectorToString两个方法,分别用于计算新的聚类中心和将向量转换为字符串。

相关推荐

最新推荐

recommend-type

基于MapReduce的Apriori算法代码

mapreduce实现apriori算法,亲测可行!需要自行下载数据集。数据集链接如下http://fimi.ua.ac.be/data/
recommend-type

基于MapReduce实现决策树算法

主要为大家详细介绍了基于MapReduce实现决策树算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

MapReduce: Simplified Data Processing on Large Clusters中文版

来自于GOOGLE的mapreduce的开山之作,此文是原英文的中文版本,希望能互相参照,加深理解
recommend-type

MapReduce下的k-means算法实验报告广工(附源码)

实验内容:给定国际通用UCI数据库中FISHERIRIS数据集,其meas集包含150个样本数据,每个数据含有莺尾属植物的4个属性,即萼片长度、萼片宽度...要求在该数据集上用MapReduce结构实现k-means聚类算法,得到的聚类结果。
recommend-type

java大数据作业_5Mapreduce、数据挖掘

课后作业 1.请找出日志中的访问者ip,访问时间,来源地址,访问 地址 日志内容如下: 192.168.170.111—[20/Jan/2014:16:35:...需要使用二次排序算法,得到如下处理结果: 1 2 2 1 2 3 3 1 3 2 3 8 4 6 请简述处理过程
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

如何用python编写api接口

在Python中编写API接口可以使用多种框架,其中比较流行的有Flask和Django。这里以Flask框架为例,简单介绍如何编写API接口。 1. 安装Flask框架 使用pip命令安装Flask框架: ``` pip install flask ``` 2. 编写API接口 创建一个Python文件,例如app.py,编写以下代码: ```python from flask import Flask, jsonify app = Flask(__name__) @app.route('/api/hello', methods=['GET']) def hello():
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。