Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How do I change the properties of a model object, when the object is generated by a toolbox?

2 vues (au cours des 30 derniers jours)
Greg
Greg le 24 Avr 2017
Clôturé : MATLAB Answer Bot le 20 Août 2021
I create a model object using 'fitcnb' from the Statistics and Machine Learning toolbox. e.g. 'model = fitcnb(data,labels);', and 'model' has a number of properties. Is it possible to change the properties of 'model'? If so, how?
I want to do this because of incompatibilities between 'fitcnb' and the Matlab Compiler. I'm performing Naive Bayes classification in Matlab. To do this, I call 'fitcnb' and 'predict'. Here's a simplified example (with random data):
XX = rand(10^4,5); % make some random data
yy = rand(10^4,1)>0.5; % make random labels
% partition into training and testing
I_train = randsample(10^4, 0.5*10^4); % half the data for training
I_test = ~ismember(1:10^4, I_train); % the other half for testing
naive_bayes_model = fitcnb(XX(I_train,:),yy(I_train));
[predicted_class, score] = predict(naive_bayes_model,XX(I_test,:))
My problem is that we want to compile our code, which contains calls to 'fitcnb' and 'predict', into a standalone. The executable crashes at runtime if we include the call to 'fitcnb'; the problem's solved if we comment out the line to 'fitcnb'. Therefore it seems like there's a problem including calls to the Statistics and Machine Learning toolbox. So we need to get creative.
If we could save 'naive_bayes_model' to a mat-file and edit the properties, we could eliminate the call to 'fitcnb'. From inspection I think I know what all the properties are, e.g. mean, standard deviation, number of data points, etc. Then I could load 'naive_bayes_model', use it as a template, and change the properties for whatever data set it should operate on. However I'm not used to OOP in Matlab, so I have no idea if this is a crazy idea.

Réponses (1)

Don Mathis
Don Mathis le 25 Avr 2017
I think the properties you want to set are 'protected' which means that only methods of the class or subclasses can set them. That design is often used when setting such properties individually would create an inconsistent state inside the object (because other properties may have values that depend on these, etc.). In some cases you could edit the code to make the desired properties 'public', but that could break things unless you read the code and made sure that there were no dependencies between these properties and others, anywhere. But that's not going to be easy, and most likely there are dependencies.

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by