1 前言
作为无监督聚类算法中的代表——K均值聚类(Kmeans)算法,该算法的主要作用是将相似的样本自动归到一个类别中。所谓的监督算法,就是输入样本没有对应的输出或标签。聚类(clustering)试图将数据集中的样本划分为若干个通常是不相交的子集,每个子集称为一个“簇(cluster)”,聚类既能作为一个单独过程,用于找寻数据内在的分布结构,也可作为分类等其他学习任务的前去过程。——《Machine Learning》。
聚类算法也许是机器学习中“新算法”出现最多、最快的领域,一个重要的原因是聚类不存在客观标准,给定数据集总能从某个角度找到以往算法未覆盖的某种标准从而设计出新算法。Kmeans算法十分简单易懂而且非常有效,但是合理的确定K值和K个初始类簇中心点对于聚类效果的好坏有很大的影响。众多的论文基于此都提出了各自行之有效的解决方案,新的改进算法仍然不断被提出,此类文章大家可以在Web Of Science中搜索。
尽管Kmeans算法在MATLAB、Python等语言的工具箱函数中都有自带的函数可供调用,但作为机器学习的研究者新来说要设计出新的算法,有时就得“定制”自己的Kmeans函数了。自己动手编写无疑也更加能理解算法的具体过程,接下来就让我们进入正题吧。
2 Kmeans算法的原理与理解
Kmeans算法是最常用的聚类算法,主要思想是:在给定K值和K个初始类簇中心点的情况下,把每个点(亦即数据记录)分到离其最近的类簇中心点所代表的类簇中,所有点分配完毕之后,根据一个类簇内的所有点重新计算该类簇的中心点(取平均值),然后再迭代的进行分配点和更新类簇中心点的步骤,直至类簇中心点的变化很小,或者达到指定的迭代次数。
function varargout = k_means_gui(varargin)
% K_MEANS_GUI M-file for k_means_gui.fig
% K_MEANS_GUI, by itself, creates a new K_MEANS_GUI or raises the existing
% singleton*.
%
% H = K_MEANS_GUI returns the handle to a new K_MEANS_GUI or the handle to
% the existing singleton*.
%
% K_MEANS_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in K_MEANS_GUI.M with the given input arguments.
%
% K_MEANS_GUI('Property','Value',...) creates a new K_MEANS_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before k_means_gui_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to k_means_gui_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help k_means_gui
% Last Modified by GUIDE v2.5 28-Jun-2008 20:30:55
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @k_means_gui_OpeningFcn, ...
'gui_OutputFcn', @k_means_gui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{
1})
gui_State.gui_Callback = str2func(varargin{
1});
end
if nargout
[varargout{
1:nargout}] = gui_mainfcn(gui_State, varargin{
:});
else
gui_mainfcn(gui_State, varargin{
:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before k_means_gui is made visible.
function k_means_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to k_means_gui (see VARARGIN)
global dim;
global means;
global result;
global step;
global processing;
global dist;
global intialization;
result=0;
step=0;
processing=0;
dist='SquaredEuclidean';
intialization='number ';
set(handles.number,'String',num2str(500));
set(handles.dispersion,'String',num2str(2));
set(findobj('tag','dimension2'),'Value',1);
set(findobj('tag','dimension3'),'Value',0);
set(findobj('tag','numberofsort'),'Value',1);
set(findobj('tag','intialization'),'Value',0);
set(handles.sortedit,'String',num2str(4));
set(handles.cenini,'String','1,2;2,4;3,5;4,5;');
set(handles.iterativenumber,'String',num2str(4));
set(handles.dimension,'String','');
set(handles.distancetype,'String','');
set(handles.numbersort,'String','');
set(handles.iterativetime,'String','');
set(handles.sorttype,'String','');
dim='2D';
means='n';
% Choose default command line output for k_means_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes k_means_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = k_means_gui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{
1} = handles.output;
function centroidedit_Callback(hObject, eventdata, handles)
% hObject handle to centroidedit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of centroidedit as text
% str2double(get(hObject,'String')) returns contents of centroidedit as a double
% --- Executes during object creation, after setting all properties.
function centroidedit_CreateFcn(hObject, eventdata, handles)
% hObject handle to centroidedit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function number_Callback(hObject, eventdata, handles)
% hObject handle to number (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of number as text
% str2double(get(hObject,'String')) returns contents of number as a double
% --- Executes during object creation, after setting all properties.
function number_CreateFcn(hObject, eventdata, handles)
% hObject handle to number (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function dispersion_Callback(hObject, eventdata, handles)
% hObject handle to dispersion (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of dispersion as text
% str2double(get(hObject,'String')) returns contents of dispersion as a double
% --- Executes during object creation, after setting all properties.
function dispersion_CreateFcn(hObject, eventdata, handles)
% hObject handle to dispersion (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in dimension2.
function dimension2_Callback(hObject, eventdata, handles)
% hObject handle to dimension2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of dimension2
global dim;
if get(gcbo,'value')==1
set(findobj('tag','dimension3'),'Value',0);
dim='2D';
end
% --- Executes on button press in dimension3.
function dimension3_Callback(hObject, eventdata, handles)
% hObject handle to dimension3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of dimension3
global dim;
if get(gcbo,'value')==1
set(findobj('tag','dimension2'),'Value',0);
dim='3D';
end
% --- Executes on button press in generate.
function generate_Callback(hObject, eventdata, handles)
% hObject handle to generate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global dim;
global X;
number=str2num(get(handles.number,'String'));
dispersion=str2num(get(handles.dispersion,'String'));
if dim=='2D'
版本:2014a
完整代码或代写加QQ912100926