Data Analysis Matlab Code

3 views (last 30 days)
Mustafa Furkan SAHIN
Mustafa Furkan SAHIN on 13 Dec 2022
Answered: Ashutosh Bajpai on 17 Feb 2023
I have a dataset(145 rows, 32 columns without student id and attributes) ). I can read and find the centered data matrix dataset. I even let the user select an attribute and find the mean, median, sum, max, range, skewness, kurtosis, boxplot, and the number of outliers. But I can't implement the naive Bayes Classifier algorithm.
i know how to do this algorithm i can't convert it to matlab code.
Steven Lord
Steven Lord on 13 Dec 2022
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
If you need to use Naive Bayes classification rather than implementing it yourself and you have Statistics and Machine Learning Toolbox installed, take a look at the functions listed on this documentation page.
Mustafa Furkan SAHIN
Mustafa Furkan SAHIN on 13 Dec 2022
Edited: Mustafa Furkan SAHIN on 13 Dec 2022
I was able to use naive bayes algorithm on another dataset(Social Network Ads-
data = readtable('Social_Network_Ads.csv');
stand_age = (data.Age - mean(data.Age))/std(data.Age);
data.Age = stand_age;
stand_estimted_salary=(data.EstimatedSalary - mean(data.EstimatedSalary))/std(data.EstimatedSalary);
classification_model = fitcnb(data,'Purchased~Age+EstimatedSalary'); %I learned to use the naive bayes algorithm
[x1 x2]=meshgrid(e,f);
x=[x1(:) x2(:)];
hold on;
title('Naive_Bayesain Classification Visualization');
Unfortunately, I was not able to apply this formula to my own dataset(Higher Education Students Performance Evaluation Dataset) due to the file contents.

Sign in to comment.

Answers (1)

Ashutosh Bajpai
Ashutosh Bajpai on 17 Feb 2023
To implement the Naive Bayes Classifier algorithm in MATLAB, you can follow these steps:
  1. Load your dataset into a matrix in MATLAB.
  2. Split the dataset into training and testing datasets. You can use the cvpartition function in MATLAB to split the data into a training and testing set, for example:
cv = cvpartition(145,'HoldOut',0.3);
trainingData = data(,:);
testingData = data(cv.test,:);
3. Train the classifier using the training data. In MATLAB, you can use the fitcnb function to fit a Naive Bayes classifier to your data:
nb = fitcnb(trainingData(:,1:end-1), trainingData(:,end));
4. Evaluate the classifier using the testing data. In MATLAB, you can use the predict method to predict the class labels for the testing data:
predictions = predict(nb, testingData(:,1:end-1));
5. Calculate the performance metrics for the classifier. You can use the confusionmat function in MATLAB to calculate the confusion matrix, and then use the confusion matrix to calculate accuracy, precision, recall, and F1-score:
cm = confusionmat(testingData(:,end), predictions);
accuracy = sum(diag(cm))/sum(cm(:));
precision = diag(cm)./sum(cm,2);
recall = diag(cm)./sum(cm,1)';
f1_score = 2*(precision.*recall)./(precision+recall);
These are the basic steps to implement the Naive Bayes Classifier algorithm in MATLAB. You can use these steps as a starting point and modify them as needed for your specific use case.





Community Treasure Hunt

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

Start Hunting!