Assign to a 'protected' property of TreeBagger object

Not an expert on MATLAB OO programing here, and I have a seemingly simple problem. I want to change a property value that is 'protected' on a built-in class (not a class I developed or even know my way around), but there's no obvious method to do so. So how do I change it?
Example time ... here's the property I'm looking at:
load fisheriris
B = TreeBagger(10, meas, species);
T = B.Trees{1};
T.ClassProb
I need to twiddle with the values in that matrix, without breaking the predict method for TreeBagger.

回答 (2 件)

Sean de Wolski
Sean de Wolski 2014 年 3 月 21 日

0 投票

The property is protected to make sure that you can't interfere with the operation of the TreeBagger object. If you need access to it, I suggest contacting us and providing us with a use-case that we could then consider for an enhancement.
Okay, so how do you actually do this? Write your own class that inherits from TreeBagger. Since TreeBagger will be a superclass of your class, your class will be able to overwrite the protected properties.

3 件のコメント

Ian
Ian 2014 年 3 月 21 日
編集済み: Ian 2014 年 3 月 21 日
Thanks for the suggestion, I will attempt writing a class to do this. Its not clear to me whether a new class would let me change the property of an existing instance of class TreeBagger. If you have the time/inclination to provide a simple example, it would help.
Sean de Wolski
Sean de Wolski 2014 年 3 月 21 日
No it won't for an existing object.
In fact you'll have an instance of your new class that has the non-private methods and properties of the treebagger object.
I don't really want to encourage you to do this without more knowledge of why you would want to. Just sayin' it's possible.
Ian
Ian 2014 年 3 月 21 日
Why? Nothing deeply troubling. I just have a different method for calculating scores associated with the random forest predictions. I can implement it one of two ways, by looping through all the trees and eval each one individually or by modifying ClassProb and using the (presumably speedier) predict method. I have a lot of predictions to make.

サインインしてコメントする。

Ilya
Ilya 2014 年 3 月 21 日

0 投票

I suggest that you leave ClassProb alone. If you need a different method for prediction, loop through the trees. The likelihood of making a coding error in this approach would be much lower than that of making a coding error in the derived class.
bagger.Trees{1} is an instance of class CompactClassificationTree. So you would need to derive a new tree class from that class and let the new class set values of the ClassProb property. Then you would need to derive a new class from TreeBagger and make this new class use the new tree class instead of CompactClassificationTree. And even then it wouldn't work because ClassProb is a dependent property, and you need to change its value in the implementation class, which is one more layer down, to affect prediction.

1 件のコメント

Ian
Ian 2014 年 3 月 21 日
Thanks for spelling out all the levels of the problem.

サインインしてコメントする。

カテゴリ

質問済み:

Ian
2014 年 3 月 21 日

コメント済み:

Ian
2014 年 3 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by