- {} curly braces refer to the table content
- () parentheses refer to the table itself.
I am attaching the matlab code and input file and request to make it workable.
3 次查看(过去 30 天)
显示 更早的评论
The attached matlab code is giving some errors at end of it. I have tried very hard to fix these issues but fail to resolve them. Therefore, I seek your kind help to kindly make it workable. I am attaching code and input file. This input file is just for testing the code.
Sanchit Singh
sample
1 个评论
Stephen23
2023-7-9
One problem is that you are using the wrong kind of brackets to access table content:
Note that MATLAB does not have a function named MEAN_SQUARED_ERROR: do you have this function saved on your MATLAB search path (e.g. did you write it yourself or obtain it from someone else?)
data = readtable('BreastCancerData.csv')
X = data{:,1:end-1}; % Select all columns except the last one
y = data{:,end}; % Select the last column
numGroundTruth = numel(y)
numTrainingSamples = round(0.8 * numel(y))
trainingIndexes = randsample(numel(y), numTrainingSamples);
testIndexes = setdiff((1 : numGroundTruth)', trainingIndexes);
X_train = X(trainingIndexes, :);
X_test = X(testIndexes, :);
y_train = y(trainingIndexes, :);
y_test = y(testIndexes, :);
% Create a Random Forest classifier
rf_classifier = TreeBagger(100, X_train, y_train, 'OOBPrediction', 'On');
% Print the calculated metrics
oob_error = rf_classifier.oobError
oob_mse = mean_squared_error(y_train, predict(rf_classifier, X_train));
oob_classification_error = 1 - rf_classifier.OOBPermutedPredictorDeltaError(end);
train_accuracy = 1 - oob_error;
test_accuracy = 1 - loss(rf_classifier, X_test, y_test);
disp(['Out-of-Bag Mean Error: ', num2str(oob_error)]);
disp(['Out-of-Bag Mean Squared Error: ', num2str(oob_mse)]);
disp(['Out-of-Bag Classification Error: ', num2str(oob_classification_error)]);
disp(['Training Accuracy: ', num2str(train_accuracy)]);
disp(['Testing Accuracy: ', num2str(test_accuracy)]);
回答(2 个)
Walter Roberson
2023-7-9
You were using tables where you should have used arrays.
I cannot find any function named mean_squared_error, but you might want to look at rmse with the prediction as the first parameter
data = readtable('BreastCancerData.csv');
X = data{:, 1:end-1}; % Select all columns except the last one
y = data{:, end}; % Select the last column
numGroundTruth = numel(y);
numTrainingSamples = round(0.8 * numel(y));
trainingIndexes = randsample(numel(y), numTrainingSamples);
testIndexes = setdiff((1 : numGroundTruth)', trainingIndexes);
X_train = X(trainingIndexes, :);
X_test = X(testIndexes, :);
y_train = y(trainingIndexes, :);
y_test = y(testIndexes, :);
% Create a Random Forest classifier
rf_classifier = TreeBagger(100, X_train, y_train, 'OOBPrediction', 'On');
% Print the calculated metrics
oob_error = rf_classifier.oobError;
oob_mse = mean_squared_error(y_train, predict(rf_classifier, X_train));
oob_classification_error = 1 - rf_classifier.OOBPermutedPredictorDeltaError(end);
train_accuracy = 1 - oob_error;
test_accuracy = 1 - loss(rf_classifier, X_test, y_test);
disp(['Out-of-Bag Mean Error: ', num2str(oob_error)]);
disp(['Out-of-Bag Mean Squared Error: ', num2str(oob_mse)]);
disp(['Out-of-Bag Classification Error: ', num2str(oob_classification_error)]);
disp(['Training Accuracy: ', num2str(train_accuracy)]);
disp(['Testing Accuracy: ', num2str(test_accuracy)]);
0 个评论
Mayur
2023-7-9
编辑:Mayur
2023-7-9
Hi Sanchit!
I understand that your code is not able to read the .csv file. The error message suggests that the code is unable to find or open the file 'BreastCancerData.csv' at the specified path 'c:/matlab/'. Please make sure that the file exists at the correct location and that you have the necessary permissions to access it. If the file is located in a different directory, you can update the file path accordingly.
Additionally, to avoid errors, you can just copy the file path by going to its properties and then paste it directly.
Another workaround is to just have the .csv file in the same folder as the .m file, and then just use:
data = readtable('BreastCancerData.csv')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Classification Trees 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!