christophM / rulefit

Python implementation of the rulefit algorithm
MIT License
406 stars 111 forks source link

Getting error : unsupported operand type(s) for /: 'int' and 'RandomForestClassifier' #44

Open sunnytholar opened 3 years ago

sunnytholar commented 3 years ago

While running rf.fit(X, y, feature_names=features) in your github code I am getting below error,

: unsupported operand type(s) for /: 'int' and 'RandomForestClassifier'

zhaoyanpeng208 commented 2 years ago

+1

csetzkorn commented 2 years ago

+1

chriswbartley commented 2 years ago

I can't replicate this, how is your RuleFit(...) instantiated?

danielwalltu commented 1 year ago

I know it's probably way too late, but I found what is causing the problem. The arguments in the initiator of the class RuleFit are in the wrong order:

def __init__(self,tree_size=4,sample_fract='default',max_rules=2000,
                 memory_par=0.01,
                 tree_generator=None,
                rfmode='regress',lin_trim_quantile=0.025,
                lin_standardise=True, exp_rand_tree_size=True,
                model_type='rl',Cs=None,cv=3,random_state=None):

If you create an instance of RuleFit, the first argument (your chosen classifier) is written into self.tree_size, which is supposed to be an integer, hence the the error message.

A fix to your problem could look like this:

clf = RandomForestClassifier(n_estimators=500, max_depth=10)
rf = RuleFit(tree_generator=clf, rfmode="helloworld")
rf.fit(X.to_numpy(), y, feature_names=features)

where X is an instance of pd.DataFrame and rfmode has to be set to something other than "regress", as otherwise a regressor is expected. Took me some time to read through the source code and trace it down. Overall, the package does not seem to be polished, but I will keep experimenting.