Ajax jQuery Ajax jQuery - 3 months ago 8
Python Question

unbound method fit() must be called with DecisionTreeClassifier instance as first argument (got Stock instance instead)

I'm trying to access the

fit
method on the
clf
object in my
Stock
class, I get this error:

unbound method fit() must be called with DecisionTreeClassifier instance as first argument (got Stock instance instead)


Stock class:

class Stock():
def __init__(self,equity, history):
self.equity = equity
self.history = history
self.clf = tree.DecisionTreeClassifier

# Couldn't use built-in comparable method
# This method is a workaround.
def exists(self, allCompanies):
exists = False;
for other in allCompanies:
if self.equity.sid == other.equity.sid:
exists = True

return exists


Where I'm instantiating the class:

....
arr.append(Stock(equity, history))


Where the error is thrown:

...
if current > prev:
Stock.clf.fit(Stock, 1)
else:
Stock.clf.fit(Stock, 0)
...

BPL BPL
Answer

To complete the other correct answers, here's a little example which will help you to understand what your buggy line self.clf = tree.DecisionTreeClassifier means:

class f(object):

    def __init__(self):
        pass

print(isinstance(f, f))
print(isinstance(f(), f))