My script can run but every time it is published it is a blank
% Load Dry Bean Dataset
dryBeanData = readtable(‘D:MATLABFinal ProjectDry_Bean_Dataset.csv’);
% Extract features and labels from the dryBeanData table
data = table2array(dryBeanData(:, 1:16)); % Adjust column indices if needed
labels = dryBeanData{:, 17}; % Assuming the 17th column contains labels
labels = categorical(labels);
% Ensure the data and labels match in size
if size(data, 1) ~= numel(labels)
error(‘Mismatch between the number of rows in data and the number of labels.’);
end
% Set the number of nearest neighbors and the target dimensionality
k = 10; % Increase k to improve graph connectivity
num_dims = 2; % Target dimensionality (2D)
% Apply Isomap and measure time
tic;
mappedIsomap = compute_mapping(data, ‘Isomap’, num_dims, k);
timeElapsedIsomap = toc;
fprintf(‘Isomap computation took %.2f secondsn’, timeElapsedIsomap);
% Verify the size of the mapped data matches the original labels
if size(mappedIsomap, 1) ~= numel(labels)
error(‘Mismatch between reduced data rows and number of labels. Try increasing the value of k.’);
end
% Plot the Isomap result
figure;
gscatter(mappedIsomap(:, 1), mappedIsomap(:, 2), labels);
title(‘Isomap 2D Projection’);
xlabel(‘Dimension 1’);
ylabel(‘Dimension 2’);
grid on;
% Setup kNN Classifier using the Isomap-reduced data
Mdl_kNN_Isomap = fitcknn(mappedIsomap, labels, ‘NumNeighbors’, k);
% Cross-validation to evaluate the kNN model
cvmodelIsomap = crossval(Mdl_kNN_Isomap, ‘KFold’, 10); % 10-fold cross-validation
classLossIsomap = kfoldLoss(cvmodelIsomap);
predictedLabelsIsomap = kfoldPredict(cvmodelIsomap);
confMatIsomap = confusionmat(labels, predictedLabelsIsomap);
% Calculate and display classification metrics
fprintf(‘Classification loss for Isomap data using kNN: %.4fn’, classLossIsomap);
[f1Isomap, precisionIsomap, recallIsomap] = f1score(confMatIsomap);
fprintf(‘F1 Score: %.2f, Precision: %.2f, Recall: %.2fn’, f1Isomap, precisionIsomap, recallIsomap);
% Display Confusion Matrix
figure;
imagesc(confMatIsomap);
colormap(‘hot’);
colorbar;
title(‘Confusion Matrix for Isomap’);
xlabel(‘Predicted Labels’);
ylabel(‘Actual Labels’);
xticks(1:length(categories(labels)));
yticks(1:length(categories(labels)));
xticklabels(categories(labels));
yticklabels(categories(labels));
% Function to calculate F1 Score, Precision, and Recall from the Confusion Matrix
function [f1, precision, recall] = f1score(confMat)
numClasses = size(confMat, 1);
f1 = zeros(numClasses, 1);
precision = zeros(numClasses, 1);
recall = zeros(numClasses, 1);
for i = 1:numClasses
TP = confMat(i, i);
FP = sum(confMat(:, i)) – TP;
FN = sum(confMat(i, 🙂 – TP);
if (TP + FP) > 0
precision(i) = TP / (TP + FP);
else
precision(i) = 0;
end
if (TP + FN) > 0
recall(i) = TP / (TP + FN);
else
recall(i) = 0;
end
if (precision(i) + recall(i)) > 0
f1(i) = 2 * ((precision(i) * recall(i)) / (precision(i) + recall(i)));
else
f1(i) = 0;
end
end
f1 = mean(f1); % Calculating mean F1 score for all classes
precision = mean(precision);
recall = mean(recall);
end
Other scripts can be published normally but this script is blank every time it is published. But I can get the results and images when the post is finished.Just publish the page without anything% Load Dry Bean Dataset
dryBeanData = readtable(‘D:MATLABFinal ProjectDry_Bean_Dataset.csv’);
% Extract features and labels from the dryBeanData table
data = table2array(dryBeanData(:, 1:16)); % Adjust column indices if needed
labels = dryBeanData{:, 17}; % Assuming the 17th column contains labels
labels = categorical(labels);
% Ensure the data and labels match in size
if size(data, 1) ~= numel(labels)
error(‘Mismatch between the number of rows in data and the number of labels.’);
end
% Set the number of nearest neighbors and the target dimensionality
k = 10; % Increase k to improve graph connectivity
num_dims = 2; % Target dimensionality (2D)
% Apply Isomap and measure time
tic;
mappedIsomap = compute_mapping(data, ‘Isomap’, num_dims, k);
timeElapsedIsomap = toc;
fprintf(‘Isomap computation took %.2f secondsn’, timeElapsedIsomap);
% Verify the size of the mapped data matches the original labels
if size(mappedIsomap, 1) ~= numel(labels)
error(‘Mismatch between reduced data rows and number of labels. Try increasing the value of k.’);
end
% Plot the Isomap result
figure;
gscatter(mappedIsomap(:, 1), mappedIsomap(:, 2), labels);
title(‘Isomap 2D Projection’);
xlabel(‘Dimension 1’);
ylabel(‘Dimension 2’);
grid on;
% Setup kNN Classifier using the Isomap-reduced data
Mdl_kNN_Isomap = fitcknn(mappedIsomap, labels, ‘NumNeighbors’, k);
% Cross-validation to evaluate the kNN model
cvmodelIsomap = crossval(Mdl_kNN_Isomap, ‘KFold’, 10); % 10-fold cross-validation
classLossIsomap = kfoldLoss(cvmodelIsomap);
predictedLabelsIsomap = kfoldPredict(cvmodelIsomap);
confMatIsomap = confusionmat(labels, predictedLabelsIsomap);
% Calculate and display classification metrics
fprintf(‘Classification loss for Isomap data using kNN: %.4fn’, classLossIsomap);
[f1Isomap, precisionIsomap, recallIsomap] = f1score(confMatIsomap);
fprintf(‘F1 Score: %.2f, Precision: %.2f, Recall: %.2fn’, f1Isomap, precisionIsomap, recallIsomap);
% Display Confusion Matrix
figure;
imagesc(confMatIsomap);
colormap(‘hot’);
colorbar;
title(‘Confusion Matrix for Isomap’);
xlabel(‘Predicted Labels’);
ylabel(‘Actual Labels’);
xticks(1:length(categories(labels)));
yticks(1:length(categories(labels)));
xticklabels(categories(labels));
yticklabels(categories(labels));
% Function to calculate F1 Score, Precision, and Recall from the Confusion Matrix
function [f1, precision, recall] = f1score(confMat)
numClasses = size(confMat, 1);
f1 = zeros(numClasses, 1);
precision = zeros(numClasses, 1);
recall = zeros(numClasses, 1);
for i = 1:numClasses
TP = confMat(i, i);
FP = sum(confMat(:, i)) – TP;
FN = sum(confMat(i, 🙂 – TP);
if (TP + FP) > 0
precision(i) = TP / (TP + FP);
else
precision(i) = 0;
end
if (TP + FN) > 0
recall(i) = TP / (TP + FN);
else
recall(i) = 0;
end
if (precision(i) + recall(i)) > 0
f1(i) = 2 * ((precision(i) * recall(i)) / (precision(i) + recall(i)));
else
f1(i) = 0;
end
end
f1 = mean(f1); % Calculating mean F1 score for all classes
precision = mean(precision);
recall = mean(recall);
end
Other scripts can be published normally but this script is blank every time it is published. But I can get the results and images when the post is finished.Just publish the page without anything % Load Dry Bean Dataset
dryBeanData = readtable(‘D:MATLABFinal ProjectDry_Bean_Dataset.csv’);
% Extract features and labels from the dryBeanData table
data = table2array(dryBeanData(:, 1:16)); % Adjust column indices if needed
labels = dryBeanData{:, 17}; % Assuming the 17th column contains labels
labels = categorical(labels);
% Ensure the data and labels match in size
if size(data, 1) ~= numel(labels)
error(‘Mismatch between the number of rows in data and the number of labels.’);
end
% Set the number of nearest neighbors and the target dimensionality
k = 10; % Increase k to improve graph connectivity
num_dims = 2; % Target dimensionality (2D)
% Apply Isomap and measure time
tic;
mappedIsomap = compute_mapping(data, ‘Isomap’, num_dims, k);
timeElapsedIsomap = toc;
fprintf(‘Isomap computation took %.2f secondsn’, timeElapsedIsomap);
% Verify the size of the mapped data matches the original labels
if size(mappedIsomap, 1) ~= numel(labels)
error(‘Mismatch between reduced data rows and number of labels. Try increasing the value of k.’);
end
% Plot the Isomap result
figure;
gscatter(mappedIsomap(:, 1), mappedIsomap(:, 2), labels);
title(‘Isomap 2D Projection’);
xlabel(‘Dimension 1’);
ylabel(‘Dimension 2’);
grid on;
% Setup kNN Classifier using the Isomap-reduced data
Mdl_kNN_Isomap = fitcknn(mappedIsomap, labels, ‘NumNeighbors’, k);
% Cross-validation to evaluate the kNN model
cvmodelIsomap = crossval(Mdl_kNN_Isomap, ‘KFold’, 10); % 10-fold cross-validation
classLossIsomap = kfoldLoss(cvmodelIsomap);
predictedLabelsIsomap = kfoldPredict(cvmodelIsomap);
confMatIsomap = confusionmat(labels, predictedLabelsIsomap);
% Calculate and display classification metrics
fprintf(‘Classification loss for Isomap data using kNN: %.4fn’, classLossIsomap);
[f1Isomap, precisionIsomap, recallIsomap] = f1score(confMatIsomap);
fprintf(‘F1 Score: %.2f, Precision: %.2f, Recall: %.2fn’, f1Isomap, precisionIsomap, recallIsomap);
% Display Confusion Matrix
figure;
imagesc(confMatIsomap);
colormap(‘hot’);
colorbar;
title(‘Confusion Matrix for Isomap’);
xlabel(‘Predicted Labels’);
ylabel(‘Actual Labels’);
xticks(1:length(categories(labels)));
yticks(1:length(categories(labels)));
xticklabels(categories(labels));
yticklabels(categories(labels));
% Function to calculate F1 Score, Precision, and Recall from the Confusion Matrix
function [f1, precision, recall] = f1score(confMat)
numClasses = size(confMat, 1);
f1 = zeros(numClasses, 1);
precision = zeros(numClasses, 1);
recall = zeros(numClasses, 1);
for i = 1:numClasses
TP = confMat(i, i);
FP = sum(confMat(:, i)) – TP;
FN = sum(confMat(i, 🙂 – TP);
if (TP + FP) > 0
precision(i) = TP / (TP + FP);
else
precision(i) = 0;
end
if (TP + FN) > 0
recall(i) = TP / (TP + FN);
else
recall(i) = 0;
end
if (precision(i) + recall(i)) > 0
f1(i) = 2 * ((precision(i) * recall(i)) / (precision(i) + recall(i)));
else
f1(i) = 0;
end
end
f1 = mean(f1); % Calculating mean F1 score for all classes
precision = mean(precision);
recall = mean(recall);
end
Other scripts can be published normally but this script is blank every time it is published. But I can get the results and images when the post is finished.Just publish the page without anything pubilsh MATLAB Answers — New Questions