k-means

MATLAB kMeans does not always converge to global minima

无人久伴 提交于 2019-12-30 06:24:11
问题 I wrote a k-Means clustering algorithm in MATLAB, and I thought I'd try it against MATLABs built in kmeans(X,k) . However, for the very easy four cluster setup (see picture), MATLAB kMeans does not always converge to the optimum solution (left) but to (right). The one I wrote does not always do that either, but should not the built-in function be able to solve such an easy problem, always finding the optimal solution? 回答1: As @Alexandre C. explained, the K-means algorithm depends on the

How to convert type Row into Vector to feed to the KMeans

泄露秘密 提交于 2019-12-30 00:39:49
问题 when i try to feed df2 to kmeans i get the following error clusters = KMeans.train(df2, 10, maxIterations=30, runs=10, initializationMode="random") The error i get: Cannot convert type <class 'pyspark.sql.types.Row'> into Vector df2 is a dataframe created as follow: df = sqlContext.read.json("data/ALS3.json") df2 = df.select('latitude','longitude') df2.show() latitude| longitude| 60.1643075| 24.9460844| 60.4686748| 22.2774728| how can i convert this two columns to Vector and feed it to KMeans

ML08 -- 聚类算法K-means

老子叫甜甜 提交于 2019-12-29 20:34:47
聚类算法K-means K-means最简单的聚类算法属于无监督算法 聚类算法和分类算法的对比 - 聚类 分类 核心 将数据分成多个组,探索每个组的数据是否有联系 从已经分组的数据中去学习,把新数据放到已经分好的组中去 学习类型 无监督,无需标签进行训练 有监督,需要标签进行训练 典型算法 K-Means,DBSCAN,层次聚类 决策树,贝叶斯,逻辑回归 算法输出 聚类结果是不确定的, 不一定总是能够反映数据的真实分类, 同样的聚类,根据不同的业务需求,可能是一个好结果,也可能是一个坏结果 分类的结果是确定的, 分类的优劣是客观的, 不是根据业务或算法需求决定 关键概念:簇与质心 簇:KMeans算法将一组N个样本的特征矩阵X划分为K个无交集的簇,直观上来看簇是一组一组聚集在一起的数据,在一个簇中的数据就认为是同一类。簇是聚类结果的表现。 质心:簇中所有数据的均值uj通常被称为这个簇的质心,在一个二维平面中,一簇数据点的质心的横坐标就是这一簇数据点横坐标的均值,质心的纵坐标就是这一簇数据点的纵坐标的均值。同理可推广到高维空间。 在KMeans算法中,簇的个数K是一个超参数,需要我们人为输入来确定。KMeans的核心任务就是根据我们设定好的K,找出K个最优质心,并将离这些质心最近的数据分别分配到这些质心代表的的簇中去。具体步骤 1.随机抽取K个样本作为最初的质心 2.开始循环: 2

Matlab: Color-Based Segmentation

最后都变了- 提交于 2019-12-29 07:14:11
问题 Earlier today I was working on this script based on http://www.mathworks.com/products/demos/image/color_seg_k/ipexhistology.html and Matlab answers: clc; clear; close all; input_im=imread('C:\Users\Udell\Desktop\T2.jpg'); sz_im=size(input_im); cform = makecform('srgb2lab'); lab_he = applycform(input_im,cform); ab = double(lab_he(:,:,2:3)); nrows = size(ab,1); ncols = size(ab,2); ab = reshape(ab,nrows*ncols,2); nColors = 3; % repeat the clustering 3 times to avoid local minima [cluster_idx,

无监督机器学习 K-Means算法python测试

微笑、不失礼 提交于 2019-12-28 18:54:34
无监督机器学习 K-Means算法python测试 最近在自学吴恩达老师的机器学习视频,学到无监督机器学习K-means算法,自己用python写一些代码加深对此算法的理解 最近在自学吴恩达老师的机器学习视频,学到无监督机器学习K-means算法,自己用python写一些代码加深对此算法的理解 import numpy as np import matplotlib . pyplot as plt #生成随机的的点坐标 array = np . random . randn ( 2 , 100 ) x = array [ 0 ] y = array [ 1 ] #初始化3个聚类质点 3类 x0 = np . random . randn ( ) y0 = np . random . randn ( ) x1 = np . random . randn ( ) y1 = np . random . randn ( ) x2 = np . random . randn ( ) y2 = np . random . randn ( ) while True : #用于存放每次标记后的点坐标 class0x = [ ] class0y = [ ] class1x = [ ] class1y = [ ] class2x = [ ] class2y = [ ] #用于记录3个聚类质点的数据

K-Means

我是研究僧i 提交于 2019-12-28 16:13:20
本篇随笔是数据科学家学习第八周的内容,主要参考资料为: K-Means: https://www.jianshu.com/p/caef1926adf7 深入理解K-Means聚类算法: https://blog.csdn.net/taoyanqi8932/article/details/53727841 极客时间数据分析 - 聚类学习 用scikit-learn学习K-Means聚类 https://www.cnblogs.com/pinard/p/6169370.html 聚类   聚类试图将数据集中的样本划分为若干个通常是不相交的子集,每个子集成为一个“簇”。通过这样的划分,每个簇可能对应于一些潜在的概念(也就是类别),如“浅色瓜” “深色瓜”,“有籽瓜” “无籽瓜”,甚至“本地瓜” “外地瓜”等;需说明的是,这些概念对聚类算法而言事先是未知的,聚类过程仅能自动形成簇结构,簇对应的概念语义由使用者来把握和命名。   簇内的样本相关性越大,簇间相关性越小,则聚类效果越好。 K-Means的原理和公式   对于给定的样本集,按照样本之间的距离大小,将样本集划分为K个簇。让簇内的点尽量紧密的连在一起,而让簇间的距离尽量的大。   对于距离大小的计算,有:   给定样本集D,K-Means算法针对聚类所得簇划分C最小化平方误差。 最小化上面的公式并不容易

机器学习第八周-K-means聚类

不问归期 提交于 2019-12-26 12:08:23
学习内容:模型原理、收敛过程、代码实现 一、模型原理 聚类的概念 聚类试图将数据集中的样本划分为若干个通常是不想交的子集,每个子集成为簇。通过这样的划分,每个簇可能对应一些潜在的概念(也就是类别),如浅色瓜,深色瓜,有籽瓜,甚至本地瓜,外地瓜;需要说明的事,这些概念对聚类算法而言事先是未知的,聚类过程仅能自动形成簇结构,簇对应的概念语义由使用者来把握和命名 聚类和分类的区别 聚类是无监督的学习算法,分类是有监督的学习算法。所谓有监督就是有已知标签的训练集(也就是说提前知道训练集里的数据属于哪个类别),机器学习算法在训练集上学习到相应的参数,构建模型,然后应用到测试集上。而聚类算法没有标签,聚类的时候,只是把需要实现的目标相似的东西聚到了一起 性能度量 聚类的目的是把相似的样本聚到一起,而将不相似的样本分开,类似物以类聚,很直观的想法是同一个簇中的相似度要尽可能高,而簇与簇之间的相似度要尽可能的低。性能度量大概可以分为两类:一是外部指标,二是内部指标 外部指标:将聚类结果和某个参考模型进行比较 内部指标:不利用任何参考模型,直接考察聚类结果 不同的簇类型 明显分离的簇、基于中心的簇、基于邻近的簇、基于密度的簇、概念簇 基本聚类分析算法 K均值:基于原型的。划分的距离技术,它试图发现用户指定个数K的簇 凝聚的层次距离:思想是开始时,每个点都作为单点簇,然后,重复合并两个最靠近的簇

Understanding kmeans clustering in r [closed]

核能气质少年 提交于 2019-12-25 21:06:43
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . Below code (minus my questions) generates this graph : I have marked 4 areas of confusion with "->" > m <- matrix(c(1,1,1) , ncol=3) > > x <- rbind(matrix(c(1,0,1) , ncol=3), + matrix(c(1,1,1) , ncol=3), + matrix(c(1,1,0) , ncol=3), + matrix(c(0,1,1) , ncol=3), + matrix(c(0,0,1) , ncol=3), + matrix

Understanding kmeans clustering in r [closed]

让人想犯罪 __ 提交于 2019-12-25 21:06:08
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . Below code (minus my questions) generates this graph : I have marked 4 areas of confusion with "->" > m <- matrix(c(1,1,1) , ncol=3) > > x <- rbind(matrix(c(1,0,1) , ncol=3), + matrix(c(1,1,1) , ncol=3), + matrix(c(1,1,0) , ncol=3), + matrix(c(0,1,1) , ncol=3), + matrix(c(0,0,1) , ncol=3), + matrix

Using scipy kmeans for cluster analysis

拟墨画扇 提交于 2019-12-25 17:45:18
问题 I want to understand scipy.cluster.vq.kmeans. Having a number of points distributed in 2D space, the problem is to group them into clusters. This problem came to my attention reading this question and I was thinking that scipy.cluster.vq.kmeans would be way to go. This is the data: Using the following code, the aim would be to get the center point of each of the 25 clusters. import numpy as np import matplotlib.pyplot as plt from scipy.cluster.vq import vq, kmeans, whiten pos = np.arange(0,20