Hello World聚类代码(一)
来源:互联网 发布:化妆学校 知乎 编辑:程序博客网 时间:2024/06/07 02:21
使用Mahout 聚类 如下
package org.test.mymahout.chapter07;
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.WeightedVectorWritable;
import org.apache.mahout.clustering.kmeans.Cluster;
import org.apache.mahout.clustering.kmeans.KMeansDriver;
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;
/**
*
* <p>Title:SimpleKMeansClustering </p>
* <p>Description:hellworld 聚类 </p>、
* @date 2016年3月4日上午10:54:08
*/
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("testdata");
if (!testData.exists()) {
testData.mkdir();
}
testData = new File("testdata/points");
if (!testData.exists()) {
testData.mkdir();
}
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
writePointsToFile(vectors, "testdata/points/file1", fs, conf);
Path path = new Path("testdata/clusters/part-00000");
SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,
path, Text.class, Cluster.class);
for (int i = 0; i < k; i++) {
Vector vec = vectors.get(i);
Cluster cluster = new Cluster(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);
SequenceFile.Reader reader = new SequenceFile.Reader(fs,
new Path("output/" + Cluster.CLUSTERED_POINTS_DIR
+ "/part-m-00000"), conf);
IntWritable key = new IntWritable();
//WeightedPropertyVectorWritable value = new WeightedPropertyVectorWritable();
WeightedVectorWritable value = new WeightedVectorWritable();
while (reader.next(key, value)) {
System.out.println(value.toString() + " belongs to cluster "
+ key.toString());
}
reader.close();
}
}
- Hello World聚类代码(一)
- Hello World聚类代码(二)
- 01:Hello, World!【代码】
- 一、Hello World
- 一、Hello World
- hello world解析一
- rabbitmq(一)Hello world
- opengles(一) hello world
- hello , world --Tkinter代码描述
- 填充代码,显示hello world !
- IOS代码实现Hello World
- IOS代码实现Hello World
- hello world(ruby 代码生成器)
- IOS代码实现Hello World
- Wicket用户手册一:Hello World
- redis-lua(一):Hello,World!
- spring & aop- hello world (一)
- rabbitmq 教程 一 hello world
- STL算法之集合
- DSP汇编"ZERO.L2 B0"机器码0427,如何解析?
- PopupWindow
- C++中引用
- 数值转换为String(std::to_string)
- Hello World聚类代码(一)
- Android Studio 与 NDK
- ECSHOP的订单状态在数据库中的表现
- JavaScript动态添加文本框
- MFC非模态对话框
- Android - 文件读写操作 总结
- iOS代理实现tableViewCell中的button在viewController中响应
- Hibernate映射关系(注解方式)
- 线性代数复习四——矩阵的维数和秩