`

Mahout: Introduction to clustering

 
阅读更多

Clustering a collection involves three things:

  • An algorithm
  • A notion of both similarity and dissimilarity
  • A stopping condition



 

Measuring the similarity of items

 

The most important issue in clustering is finding a function that quantifies the similarity between any two data points as a number.

Euclidean distance

TF-IDF

 

Hello World: running a simple clustering example

There are three steps involved in inputting data for the Mahout clustering algorithms:

  1. you need to preprocess the data,
  2. use that data to create vectors,
  3. and save the vectors in SequenceFile format as input for the algorithm.
package mia.clustering.ch07;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.apache.mahout.clustering.Cluster;
import org.apache.mahout.clustering.classify.WeightedPropertyVectorWritable;
import org.apache.mahout.clustering.kmeans.KMeansDriver;
import org.apache.mahout.clustering.kmeans.Kluster;
import org.apache.mahout.common.distance.EuclideanDistanceMeasure;
import org.apache.mahout.math.RandomAccessSparseVector;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;

public class SimpleKMeansClustering {
	public static final double[][] points = { { 1, 1 }, { 2, 1 }, { 1, 2 },
			{ 2, 2 }, { 3, 3 }, { 8, 8 }, { 9, 8 }, { 8, 9 }, { 9, 9 } };

	public static void writePointsToFile(List<Vector> points, String fileName,
			FileSystem fs, Configuration conf) throws IOException {
		Path path = new Path(fileName);
		SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, path,
				LongWritable.class, VectorWritable.class);
		long recNum = 0;
		VectorWritable vec = new VectorWritable();
		for (Vector point : points) {
			vec.set(point);
			writer.append(new LongWritable(recNum++), vec);
		}
		writer.close();
	}

	public static List<Vector> getPoints(double[][] raw) {
		List<Vector> points = new ArrayList<Vector>();
		for (int i = 0; i < raw.length; i++) {
			double[] fr = raw[i];
			Vector vec = new RandomAccessSparseVector(fr.length);
			vec.assign(fr);
			points.add(vec);
		}
		return points;
	}

	public static void main(String args[]) throws Exception {

		int k = 2;

		List<Vector> vectors = getPoints(points);

		File testData = new File("/home/zhaohj/hadoop/testdata/mahout/testdata");
		if (!testData.exists()) {
			testData.mkdir();
		}
		testData = new File("/home/zhaohj/hadoop/testdata/mahout/testdata/points");
		if (!testData.exists()) {
			testData.mkdir();
		}

		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);
		writePointsToFile(vectors, "/home/zhaohj/hadoop/testdata/mahout/testdata/points/file1", fs, conf);

		Path path = new Path("/home/zhaohj/hadoop/testdata/mahout/testdata/clusters/part-00000");
		SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, path,
				Text.class, Kluster.class);

		for (int i = 0; i < k; i++) {
			Vector vec = vectors.get(i);
			Kluster cluster = new Kluster(vec, i,
					new EuclideanDistanceMeasure());
			writer.append(new Text(cluster.getIdentifier()), cluster);
		}
		writer.close();

		// KMeansDriver.run(conf, new Path("testdata/points"), new
		// Path("testdata/clusters"),
		// new Path("output"), new EuclideanDistanceMeasure(), 0.001, 10,
		// true, false);

		KMeansDriver.run(conf, 
				new Path("/home/zhaohj/hadoop/testdata/mahout/testdata/points"), 
				new Path("/home/zhaohj/hadoop/testdata/mahout/testdata/clusters"), 
				new Path("/home/zhaohj/hadoop/testdata/mahout/output"), 
				0.2, 
				30, 
				true,
				0.001, 
				false);

		SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path(
				"/home/zhaohj/hadoop/testdata/mahout/output/" + Cluster.CLUSTERED_POINTS_DIR + "/part-m-00000"),
				conf);

		IntWritable key = new IntWritable();
		WeightedPropertyVectorWritable value = new WeightedPropertyVectorWritable();
		while (reader.next(key, value)) {
			System.out.println(value.toString() + " belongs to cluster "
					+ key.toString());
		}
		reader.close();
	}

}

  

 

 

 

 



 

 

Exploring distance measures
 

 Euclidean distance measure

 
Squared Euclidean distance measure


Manhattan distance measure



Cosine distance measure

Note that this measure of distance doesn’t account for the length of the two vectors;all that matters is that the points are in the same direction from the origin.


Tanimoto distance measure/Jaccard’s distance measure


Weighted distance measure
Mahout also provides a WeightedDistanceMeasure class, and implementations of Euclidean and Manhattan distance measures that use it. A weighted distance measure is an advanced feature in Mahout that allows you to give weights to different dimensions in order to either increase or decrease the effect of a dimension on the value of the distance measure. The weights in a WeightedDistanceMeasure need to be serialized to a file in a Vector format.

 

 

 

 

 


 
 
 

 

 

 

 

  • 大小: 67.1 KB
  • 大小: 27.6 KB
  • 大小: 35.3 KB
  • 大小: 36.6 KB
  • 大小: 3.7 KB
  • 大小: 3.1 KB
  • 大小: 2.5 KB
  • 大小: 35.4 KB
  • 大小: 7.3 KB
  • 大小: 7.8 KB
  • 大小: 42.3 KB
分享到:
评论

相关推荐

    apache-mahout-distribution-0.11.0-src.zip

    Apache Mahout是基于Hadoop的数据挖掘库,提供了一套用于实现推荐系统、分类和聚类算法的工具。这个项目的目标是创建易于使用的、高效的机器学习算法,使大数据分析变得更加简单。 2. **源码分析**: 在源码中,...

    mahout聚类算法

    Mahout 聚类算法可以分为多种类型,如 Canopy、KMeans、Fuzzy-KMeans、Spectral Clustering 等,每种算法都有其自己的特点和应用场景。 在 Mahout 聚类算法中,数据模型是数据的基本结构,它可以是 DenseVector、...

    mahout所需jar包

    Mahout的目标是帮助开发人员构建智能应用程序,如推荐系统、分类和聚类算法,这些在大数据分析领域中极为重要。 **K-Means聚类算法** K-Means是一种无监督学习的聚类算法,用于将数据集分成不同的群组或类别。在...

    mahout:Apache Mahout的镜像

    欢迎使用Apache Mahout! Apache Mahout:trade_mark:项目的目标是构建一个环境,以快速创建可扩展的高... 添加以下内容export MAHOUT_HOME=/path/to/mahoutexport MAHOUT_LOCAL=true # for running standalone on yo

    apache-mahout-distribution-0.11.1-src

    Apache Mahout 项目旨在帮助开发人员更加方便快捷地创建智能应用程序。Mahout 的创始者 Grant Ingersoll 介绍了机器学习的基本概念,并演示了如何使用 Mahout 来实现文档集群、提出建议和组织内容。

    如何成功运行Apache Mahout的Taste Webapp-Mahout推荐教程-Maven3.0.5-JDK1.6-Mahout0.5

    Mahout包含了多种机器学习的经典算法,如聚类、分类、协同过滤和进化编程等。此外,Mahout支持在Hadoop集群上运行算法,使得它们能够在云计算环境中高效运行。 2. Mahout的版本及其重要性 文档强调使用特定版本的...

    Apache_Mahout_Cookbook(高清版)

    Apache Mahout是一个高度可扩展的机器学习库,主要用于构建智能推荐系统、聚类分析以及其他数据挖掘任务。该库利用了Apache Hadoop的强大分布式计算能力,使得处理大规模数据集变得高效可行。 #### 二、安装与配置 ...

    Hadoop-Mahout:使用 Mahout 在 Hadoop 上进行推荐、集群和分类

    Mahout提供了多种聚类算法,如K-Means、Fuzzy K-Means、Canopy Clustering等。这些算法可以帮助我们发现数据集中的隐藏结构,比如用户群体、市场细分或文档主题。 三、分类 分类是机器学习中的有监督学习方法,...

    mahout:mahout机器智能推荐系统

    Apache Mahout是一个基于Java的开源项目,专注于开发可扩展的机器学习库,尤其在推荐系统、分类和聚类算法方面表现出色。在大数据领域,Mahout为Hadoop提供了一个理想的平台,用于实现大规模的数据挖掘和分析任务。...

    mahout:mahout-推荐-测试

    Apache Mahout 是一个基于 Apache Hadoop 的开源机器学习库,主要设计用于构建大规模的机器学习算法。在"mahout:mahout-推荐-测试"这个主题中,我们聚焦于 Mahout 的推荐系统部分以及相关的测试过程。Mahout 的推荐...

    Mahout_in_Action

    - **第7章:聚类简介**(Introduction to clustering):解释了聚类的基本概念和技术,以及它在数据分析中的应用。 - **第8章:表示数据**(Representing data):讲解了如何为聚类分析准备数据,包括数据清洗、特征...

    Recommendation-with-mahout:与Maven + hadoop和mahout一起推荐

    Apache Mahout是一个基于Apache Hadoop的机器学习库,它提供了多种推荐、分类和聚类算法。Mahout的核心目标是让数据科学家和开发人员能够轻松地构建智能应用程序,通过大规模分布式计算来处理海量数据。在这个项目中...

    人工智能-推荐系统-新闻推荐-基于Mahout的新闻推荐系统

    Mahout:整体框架,实现了协同过滤 Deeplearning4j,构建VSM Jieba:分词,关键词提取 HanLP:分词,关键词提取 Spring Boot:提供API、ORM 关键实现 基于用户的协同过滤 直接调用Mahout相关接口即可 选择不同...

    play-mahout:一个运行Apache Mahout方法的游乐场

    Mahout也提供了多种分类和聚类算法,例如朴素贝叶斯分类器(Naive Bayes Classifier)、决策树(Decision Trees)、随机森林(Random Forests)和K-means聚类。这些算法可用于文本分类、图像识别、用户群体划分等...

    jruby_mahout:JRuby Mahout是一颗宝石,它在JRuby世界中释放了Apache Mahout的力量

    它大规模地处理了建议,聚类和分类机器学习问题。 到目前为止,在Ruby项目中很难使用它。 您必须自己在JRuby中实现Java接口,这并不是很快,特别是如果您刚刚开始探索机器学习的世界的话。 该库的目的是简化JRuby...

Global site tag (gtag.js) - Google Analytics