'How to use fitcnb navie Bayes in matlab with discrete values?

This is the data of malignant.

first column is for the gender and second is for the age and the third is for place of the melanoma and the fourth is for the diagnosis this the image of the dataset

%male = 1; female = 2
% head/neck = 1   upper extremity = 2   lower extremity = 3    torso = 4
% unknown = 1   nevus = 2    melanoma = 3


d = [1 45 1 1;
     2 45 2 1;
     2 50 3 2;
     2 55 2 3;
     2 50 2 3;
     1 55 4 3];
c = {'benign';
    'benign';
    'benign';
    'malignant';
    'malignant';
    'malignant';};

Mdl = fitcnb(d,c,'CategoricalPredictors',[1 2])

this is the output

A normal distribution cannot be fit for the combination of class malignant and predictor x4. The data has zero variance.


Solution 1:[1]

You had to make the response vector a categorical vector and create some variance in the data:

clear;
d = [1 45 1 1;
     2 45 2 1;
     2 50 3 2;
     2 55 2 3;
     2 50 2 3;
     1 55 4 4]; % Change 3 -> 4 to create variance
c = {'benign';
    'benign';
    'benign';
    'malignant';
    'malignant';
    'malignant'};

X = d;
Y = [0; 0; 0; 1; 1; 1];
Y = categorical(Y, [0; 1], {'benign', 'malignant'});

Mdl = fitcnb(X, Y, 'CategoricalPredictors', [1, 2])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Royi