Getting the centroids in KNN clasiification

4 Ansichten (letzte 30 Tage)
Mathieu Chêne
Mathieu Chêne am 18 Okt. 2022
Bearbeitet: Divyam am 4 Nov. 2024
Hi,
I am using a knn algorithm to perform supervised classification
My KNNClassification object is created as it follows :
fitcknn(obj.Table,obj.Response,"NSMethod","exhaustive","Distance","euclidean")
I wondered if it was possible to get the centroids coordinates as when I perform unlabelled clustring with kmeans.
Thanks in advance for your futur help.
Best regards
Mathieu
  1 Kommentar
Ayush
Ayush am 23 Mai 2023
The k-Nearest Neighbors (KNN) algorithm is a supervised machine learning algorithm, which means it uses labeled data to make predictions or classify new examples. In contrast, K-means is an unsupervised clustering algorithm that groups similar data points into clusters based on some chosen distance metric.
In K-means, the centroids of the clusters can be obtained as a result of the clustering process. However, in KNN, there are no centroids as the algorithm simply compares the distance between new observations and pre-existing labeled observations.
Therefore, in short, it is not possible to get the centroids coordinates in KNN classification as there are no centroids in the algorithm.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Divyam
Divyam am 4 Nov. 2024
Bearbeitet: Divyam am 4 Nov. 2024
Unlike the K-Means clustering algorithm, that explicitly computes centroids of clusters, the K-Nearest Neighbors (KNN) classification algorithm, does not calculate centroids as the algorithm is based on a distance-based method that classifies data points based on the closest training examples in the feature space.
However, if you want to calculate something akin to centroids for each class in your KNN model, you can compute the mean of the feature values for each class in your training data. This will provide a centroid-like representation for each class in your data derived from the labeled data used for training your KNN model.
Here's some code to help you with the same based on the assumption that "obj.Table" is your feature matrix and "obj.Response" is your class labels:
% Get unique class labels
classes = unique(obj.Response);
% Initialize the centroid matrix
centroids = zeros(length(classes), size(obj.Table, 2));
% Calculate the centroid for each class
for i = 1:length(classes)
% Get indices for the current class
classIdx = obj.Response == classes(i);
% Compute the mean of the features for the current class
centroids(i, :) = mean(obj.Table(classIdx, :), 1);
end

Kategorien

Mehr zu Statistics and Machine Learning Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by