spark:kmeans测试

import org.apache.spark.ml.clustering.KMeans
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkContext, SparkConf}

/**
  * Created by fhqplzj on 16-7-18 at 上午11:02.
  */
object TestKMeans {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[*]").setAppName("TestKMeans")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    val input = "/home/fhqplzj/github/spark-1.6.1/data/mllib/kmeans_data.txt"
    val rowRDD = sc.textFile(input).filter(_.nonEmpty).map(s => Vectors.dense(s.split("\\s+").map(_.toDouble))).zipWithIndex().map(_.swap)
    val dataset = sqlContext.createDataFrame(rowRDD).toDF("id", "features")
    val model = new KMeans().setK(2).setFeaturesCol("features").setPredictionCol("prediction").fit(dataset)
    println("Cluster centers:")
    model.clusterCenters.foreach(println)
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的使用pcl::KMeans实现k-means聚类的示例代码: ```c++ #include <pcl/features/normal_3d.h> #include <pcl/filters/voxel_grid.h> #include <pcl/kdtree/kdtree_flann.h> #include <pcl/segmentation/extract_clusters.h> #include <pcl/features/fpfh.h> #include <pcl/kdtree/kmeans.h> // 定义点云类型 typedef pcl::PointXYZ PointType; typedef pcl::PointCloud<PointType> PointCloud; typedef pcl::Normal NormalType; typedef pcl::PointCloud<NormalType> NormalCloud; typedef pcl::FPFHSignature33 FeatureType; typedef pcl::PointCloud<FeatureType> FeatureCloud; int main() { // 加载点云数据 PointCloud::Ptr cloud(new PointCloud); pcl::io::loadPCDFile("input.pcd", *cloud); // 下采样 pcl::VoxelGrid<PointType> grid; grid.setLeafSize(0.01, 0.01, 0.01); grid.setInputCloud(cloud); PointCloud::Ptr cloud_downsampled(new PointCloud); grid.filter(*cloud_downsampled); // 计算法线 pcl::NormalEstimation<PointType, NormalType> ne; ne.setInputCloud(cloud_downsampled); pcl::search::KdTree<PointType>::Ptr tree(new pcl::search::KdTree<PointType>); ne.setSearchMethod(tree); NormalCloud::Ptr normals(new NormalCloud); ne.setRadiusSearch(0.03); ne.compute(*normals); // 计算特征 pcl::FPFHEstimation<PointType, NormalType, FeatureType> fpfh; fpfh.setInputCloud(cloud_downsampled); fpfh.setInputNormals(normals); fpfh.setSearchMethod(tree); FeatureCloud::Ptr features(new FeatureCloud); fpfh.setRadiusSearch(0.05); fpfh.compute(*features); // k-means聚类 int k = 3; pcl::KMeans<FeatureType> kmeans; kmeans.setClusterSize(k); kmeans.setEuclideanDistance(true); kmeans.setInputCloud(features); std::vector<pcl::PointIndices> cluster_indices; kmeans.extract(cluster_indices); // 输出聚类结果 for (int i = 0; i < cluster_indices.size(); i++) { std::cout << "Cluster " << i << ":\n"; for (int j = 0; j < cluster_indices[i].indices.size(); j++) { std::cout << cluster_indices[i].indices[j] << " "; } std::cout << std::endl; } return 0; } ``` 这段代码首先加载了一个点云数据,然后进行下采样、计算法线和特征等操作。接着使用pcl::KMeans对特征向量进行聚类,得到聚类结果。最后将聚类结果输出到控制台中。需要注意的是这里的特征类型是pcl::FPFHSignature33,如果使用其他类型的特征,需要将pcl::KMeans的模板参数替换成对应的类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值