1
$\begingroup$

I'm new to random forest models and have questions.

When I develop a linear model, I can tell people "Hey kids- here's how to predict the circumference of a circle if you know the radius. Just plug the radius into this handy formula: A = 2 * pi * radius. They don't have to know any stats at all to go start calculating circumferences.

But, if I have a random forest model, how can I communicate to folks how they can predict their own values with their own data? Do I have to distribute my original dataset to them so they can run an R/Python script? Or is there a way to extract foresty-equivalent of coefficients and slopes? I suppose it would be the decision tree somehow. Could it ever get to something like a convoluted Excel formula I could share in a spreadsheet template? How does the average person (not the statistician) actually use these things in the real world?

$\endgroup$
3
$\begingroup$

You have several options:

1) Loading trained model

Frameworks like sklearn make it easy to persist the trained estimator to disk, and then to load it up afterwards. One would distribute a Python script which uses the trained model, as well as doing any data pre and post-processing. The training data is then not needed.

From the sklearn documentation:

Training

from sklearn import ensemble, datasets
from sklearn.externals import joblib
iris = datasets.load_iris()
X, y = iris.data, iris.target
clf = ensemble.RandomForestClassifier()
clf.fit(X, y)
joblib.dump(clf, 'rf-model.pkl') 

Predictions

from sklearn.externals import joblib
clf = joblib.load('rf-model.pkl')
clf.predict(Xnew)

Implementations of Random Forest will have a way to get the trained cofficients, but the details vary a lot. Fundamentally a Random Forest classifier is a number of trees. Each tree does classification by executing a bunch of if-else statements, each checking whether one of the variables is below. At the leaf is the returned class label. The trees are combined using (weighted) majority voting.

2) Generating code

Using emtrees one can export random forests trained with sklearn as C code. A bit simplified, it looks like:

int32_t digits_predict_tree_0(int32_t *features, int32_t features_length)
{
  if (features[36] < 32768) {
      if (features[57] < 65536) {
          return 9;
      } else {
          if (features[10] < 950272) {
              return 5;
  ....
}

int32_t digits_predict(int32_t *fe, int32_t features_length)
{
   _class = digits_predict_tree_1(fe, length); votes[_class] += 1;
   _class = digits_predict_tree_2(fe, length); votes[_class] += 1;
   ....
   return find_majority(votes);
}

The same could be done for other languages, theoretically even an Excel macro.

3) Providing webservice

A modern alternative is to set up a simple webservice where users can drop their input data (as .CSV/.xsl), and then download the returned predictions as a new file.

$\endgroup$
  • $\begingroup$ Didn't know about the compile to C thing. Very cool. $\endgroup$ – Matthew Drury May 24 '18 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.