I am attaching the matlab code and input file and request to make it workable.

2 views (last 30 days)
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
Error using readtable
Unable to find or open 'c:/matlab/BreastCancerData.csv'. Check the path and filename or file permissions.

Error in sample (line 2)
data = readtable('c:/matlab/BreastCancerData.csv')
  1 Comment
Stephen23
Stephen23 on 9 Jul 2023
One problem is that you are using the wrong kind of brackets to access table content:
  • {} curly braces refer to the table content
  • () parentheses refer to the table itself.
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')
data = 698×11 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 __________ ____ ____ ____ ____ ____ ____ ____ ____ _____ _____ 1e+06 5 1 1 1 2 1 3 1 1 2 1.0029e+06 5 4 4 5 7 10 3 2 1 2 1.0154e+06 3 1 1 1 2 2 3 1 1 2 1.0163e+06 6 8 8 1 3 4 3 7 1 2 1.017e+06 4 1 1 3 2 1 3 1 1 2 1.0171e+06 8 10 10 8 7 10 9 7 1 4 1.0181e+06 1 1 1 1 2 10 3 1 1 2 1.0186e+06 2 1 2 1 2 1 3 1 1 2 1.0331e+06 2 1 1 1 2 1 1 1 5 2 1.0331e+06 4 2 1 1 2 1 2 1 1 2 1.0353e+06 1 1 1 1 1 1 3 1 1 2 1.0362e+06 2 1 1 1 2 1 2 1 1 2 1.0418e+06 5 3 3 3 2 3 4 4 1 4 1.044e+06 1 1 1 1 2 3 3 1 1 2 1.0446e+06 8 7 5 10 7 9 5 5 4 4 1.0476e+06 7 4 6 4 6 1 4 3 1 4
X = data{:,1:end-1}; % Select all columns except the last one
y = data{:,end}; % Select the last column
numGroundTruth = numel(y)
numGroundTruth = 698
numTrainingSamples = round(0.8 * numel(y))
numTrainingSamples = 558
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_error = 100×1
0.2258 0.1487 0.1254 0.1093 0.0914 0.0806 0.0789 0.0789 0.0717 0.0699
oob_mse = mean_squared_error(y_train, predict(rf_classifier, X_train));
Unrecognized function or variable 'mean_squared_error'.
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)]);

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 9 Jul 2023
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)]);

Mayur
Mayur on 9 Jul 2023
Edited: Mayur on 9 Jul 2023
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')

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!