hello all, I want to ask wht is wrong with this below SOM code?
2 次查看(过去 30 天)
显示 更早的评论
I want to genrate SOM map and label it with text on it to define the clusters, for lable i defined 25, 50 , 75 and 100. when i run it it genrate the map , however, it doesnot label it with the values i define, please check the below code. thanks i will appreciate the answers.
load S_data.txt
% Separate the input features (voltage and current) and the target (fault types)
x = S_data(:, 1:6)';
y = S_data(:, 7)';
% Create class labels
t = zeros(size(y));
t(y == 25) = 25; % Set class 1 as 1
t(y == 50) = 50; % Set class 2 as 2
t(y == 75) = 75; % Set class 3 as 3
t(y == 100) = 100; % Set class 3 as 3
% Train the SOM
gridSize = [10, 10]; % Adjust the grid size as desired
epochs = 100;
learningRate = 1;
som = selforgmap(gridSize, epochs, learningRate);
som = train(som, x);
% Map the data points to the SOM grid
mapped = som(x);
% Plot the SOM map with labeled data points
figure;
imagesc(som.IW{1,1}');
colormap('jet');
colorbar;
hold on;
for i = 1:numel(t)
scatter(mapped(2,i), mapped(1,i), 20, t(i), 'filled', 'MarkerEdgeColor', 'k');
end
% Plot cluster centers
cluster_centers = som.IW{1,1}';
for i = 1:numel(t)
cluster_id = t(i);
[r, c] = find(cluster_centers == cluster_id);
scatter(c, r, 50, 'k', 'filled', 'Marker', 's', 'MarkerEdgeColor', 'k', 'LineWidth', 2);
end
hold off;
title('SOM Mapping of Transmission Line Dataset');
xlabel('Neuron X-coordinate');
ylabel('Neuron Y-coordinate');
% Display the plot
0 个评论
采纳的回答
Anavi Somani
2023-7-13
Hi Shazia, I think the issue lies in how you are assigning the labels to the t variable.
Instead of assigning the class labels directly to t, you can use a separate variable to represent the labels as strings. Then, you can use this variable to label the data points and cluster centers on the plot.
load S_data.txt
% Separate the input features (voltage and current) and the target (fault types)
x = S_data(:, 1:6)';
y = S_data(:, 7)';
% Create class labels
labels = cell(size(y));
labels(y == 25) = {'25'}; % Set class 1 as '25'
labels(y == 50) = {'50'}; % Set class 2 as '50'
labels(y == 75) = {'75'}; % Set class 3 as '75'
labels(y == 100) = {'100'}; % Set class 4 as '100'
% Train the SOM
gridSize = [10, 10]; % Adjust the grid size as desired
epochs = 100;
learningRate = 1;
som = selforgmap(gridSize, epochs, learningRate);
som = train(som, x);
% Map the data points to the SOM grid
mapped = som(x);
% Plot the SOM map with labeled data points
figure;
imagesc(som.IW{1,1}');
colormap('jet');
colorbar;
hold on;
for i = 1:numel(labels)
scatter(mapped(2,i), mapped(1,i), 20, 'filled', 'MarkerEdgeColor', 'k');
text(mapped(2,i), mapped(1,i), labels{i}, 'Color', 'k', 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
end
% Plot cluster centers
cluster_centers = som.IW{1,1}';
for i = 1:numel(labels)
cluster_id = labels{i};
[r, c] = find(strcmp(cluster_centers, cluster_id));
scatter(c, r, 50, 'k', 'filled', 'Marker', 's', 'MarkerEdgeColor', 'k', 'LineWidth', 2);
end
hold off;
title('SOM Mapping of Transmission Line Dataset');
xlabel('Neuron X-coordinate');
ylabel('Neuron Y-coordinate');
% Display the plot
I've used a cell array labels to store the string labels corresponding to each data point. Then, when plotting the data points and cluster centers, the text function is used to display the labels on the plot.
Try if that works.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!