如何使用 Apache Hadoop 执行 K-means?
How to perform K-means using Apache Hadoop?
我是 Apache Hadoop 的初学者,到目前为止,我已经使用 mapReduce 执行了字数统计问题以供学习。我的 objective 是对一组数据执行 K 均值聚类,比如 1.5gig+。
使用 Hadoop 执行 K 均值聚类的最简单方法是什么?我应该根据 K-means 要求修改我的 map 和 reduce 函数还是需要 Mahout(我以前没有用过),或者没有它可以实现 objective 吗?
主机 OS 是 Win7,我在 VirtualBox 上安装了 HortonWorks Sandbox 2.3。任何帮助将不胜感激,因为我对选择哪条路径来实现我的 objective 有点困惑。感谢您的期待。
我认为执行 k 均值的简单方法是 K-MEANS 。 Spark 运行 使用 hadoop hdfs。
Apache Spark
这是您可以从 spark 站点找到的示例和详细信息
public class KMeansExample {
public static void main(String[] args) {
SparkConf conf = new SparkConf().setAppName("K-means Example");
JavaSparkContext sc = new JavaSparkContext(conf);
// Load and parse data
String path = "data/mllib/kmeans_data.txt";
JavaRDD<String> data = sc.textFile(path);
JavaRDD<Vector> parsedData = data.map(
new Function<String, Vector>() {
public Vector call(String s) {
String[] sarray = s.split(" ");
double[] values = new double[sarray.length];
for (int i = 0; i < sarray.length; i++)
values[i] = Double.parseDouble(sarray[i]);
return Vectors.dense(values);
}
}
);
parsedData.cache();
// Cluster the data into two classes using KMeans
int numClusters = 2;
int numIterations = 20;
KMeansModel clusters = KMeans.train(parsedData.rdd(), numClusters, numIterations);
// Evaluate clustering by computing Within Set Sum of Squared Errors
double WSSSE = clusters.computeCost(parsedData.rdd());
System.out.println("Within Set Sum of Squared Errors = " + WSSSE);
// Save and load model
clusters.save(sc.sc(), "myModelPath");
KMeansModel sameModel = KMeansModel.load(sc.sc(), "myModelPath");
}
}
我是 Apache Hadoop 的初学者,到目前为止,我已经使用 mapReduce 执行了字数统计问题以供学习。我的 objective 是对一组数据执行 K 均值聚类,比如 1.5gig+。
使用 Hadoop 执行 K 均值聚类的最简单方法是什么?我应该根据 K-means 要求修改我的 map 和 reduce 函数还是需要 Mahout(我以前没有用过),或者没有它可以实现 objective 吗?
主机 OS 是 Win7,我在 VirtualBox 上安装了 HortonWorks Sandbox 2.3。任何帮助将不胜感激,因为我对选择哪条路径来实现我的 objective 有点困惑。感谢您的期待。
我认为执行 k 均值的简单方法是 K-MEANS 。 Spark 运行 使用 hadoop hdfs。
Apache Spark
这是您可以从 spark 站点找到的示例和详细信息
public class KMeansExample {
public static void main(String[] args) {
SparkConf conf = new SparkConf().setAppName("K-means Example");
JavaSparkContext sc = new JavaSparkContext(conf);
// Load and parse data
String path = "data/mllib/kmeans_data.txt";
JavaRDD<String> data = sc.textFile(path);
JavaRDD<Vector> parsedData = data.map(
new Function<String, Vector>() {
public Vector call(String s) {
String[] sarray = s.split(" ");
double[] values = new double[sarray.length];
for (int i = 0; i < sarray.length; i++)
values[i] = Double.parseDouble(sarray[i]);
return Vectors.dense(values);
}
}
);
parsedData.cache();
// Cluster the data into two classes using KMeans
int numClusters = 2;
int numIterations = 20;
KMeansModel clusters = KMeans.train(parsedData.rdd(), numClusters, numIterations);
// Evaluate clustering by computing Within Set Sum of Squared Errors
double WSSSE = clusters.computeCost(parsedData.rdd());
System.out.println("Within Set Sum of Squared Errors = " + WSSSE);
// Save and load model
clusters.save(sc.sc(), "myModelPath");
KMeansModel sameModel = KMeansModel.load(sc.sc(), "myModelPath");
}
}