Soft Computing
  • Soft Computing
  • Introduction
  • Requirement
    • Data Set
  • Applications
    • Matlab
      • Perceptron
        • Little Red Riding Hood
          • Output
      • SVM
        • Code
        • Execution
      • TreeDecision
        • Code
        • Execution
      • Kmeans - Kmedoids
        • Code
        • Execution
      • Dimensionality Reduction
        • Principal component analysis (PAC)
          • Code
          • Execution
    • Python
      • Setup
Powered by GitBook
On this page

Was this helpful?

  1. Applications
  2. Matlab
  3. Kmeans - Kmedoids

Code

This method allow us to run the example of Iris Data with different type of cluster and classify each class of flower, also have the option to select with which type of distances where are going to classify it. This are the relevant part of the code.

% --- Executes on button press in Next.
function Next_Callback(hObject, eventdata, handles)
% hObject    handle to Next (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Cluster = get(handles.Cluster,'Value'); %In this case we select which cluster we are going to execute.
switch Cluster
    case 1
        funcionError(hObject, eventdata, handles,'Cluster');
    case 2
        funcionKmeans(hObject, eventdata, handles);
    case 3
        funcionKmedoids(hObject, eventdata, handles);
end

function funcionError(hObject, eventdata, handles, Msj) %If there is no cluster select, we print and error.
Mensaje=strcat('Error, Elige una opci?n de: ',Msj);
set(handles.Msj, 'String', Mensaje);

function funcionKmeans(hObject, eventdata, handles) %Once we select Kmeans, we need to get the other parameters
cla;
Datas = get(handles.dataset,'Value');
Condicion1=0;
switch Datas %We select what data we want to evaluate
    case 1
        funcionError(hObject, eventdata, handles,'Dataset');
        Condicion1=1;
    case 2
        DataSet=load ('irisdataset');
    case 3
        DataSet=load ('ecolidataset');
    case 4
        DataSet=load ('glassdataset');
end
Dista = get(handles.Distancia,'Value'); %Set type of distances to apply.
Condicion2=0;
switch Dista
    case 1
        funcionError(hObject, eventdata, handles,'Distancia');
        Condicion2=1;
    case 2
        Distancia='sqEuclidean';
    case 3
        Distancia='cityblock';
    case 4
        Distancia='cosine';
    case 5
        Distancia='correlation';

end
 Condicion3=0;
No = str2num(get(handles.No,'String'));
if ~isnan(No) set(handles.Msj, 'String', 'Calculado');% Number of classes to classify the data
else funcionError(hObject, eventdata, handles,'No-grupos');Condicion3=1;
end

if Condicion1==0 && Condicion2==0 && Condicion3==0
    opts = statset('Display','final');
    [idx,ctrs]= kmeans(DataSet.Datos(:,:),No,'distance',Distancia,'Options',opts);
    No_datos=size(idx);

    Colores={'y.','m.','c.','r.','g.','b.','w.','k.','y.','m.','c.','r.','g.','b.','w.','k.'};
    %%%Select to which class is ligated 
    for i=1:No_datos(1)
hold on
plot(DataSet.Datos(i,1),DataSet.Datos(i,2),Colores{idx(i)},'MarkerSize',12);
hold off
    %silhouette(DataSet.Datos,IDX);
    end

hold on
%%%Print the centers
plot(ctrs(:,1),ctrs(:,2),'kx',...
     'MarkerSize',7,'LineWidth',2)
plot(ctrs(:,1),ctrs(:,2),'ko',...
     'MarkerSize',7,'LineWidth',2)
hold off   

end
PreviousKmeans - KmedoidsNextExecution

Last updated 5 years ago

Was this helpful?