Ajax jQuery
Ajax jQuery

Reputation: 345

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)
...

Upvotes: 0

Views: 781

Answers (3)

BPL
BPL

Reputation: 9863

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))

Upvotes: 2

kindall
kindall

Reputation: 184385

You're not instantiating tree.DecisionTreeClassifier. Therefore, you're calling the fit() method on the class and need to tell it what instance you want to use, just as it says.

Presumably you want to instantiate tree.DecisionTreeClassifier:

self.clf = tree.DecisionTreeClassifier()

Upvotes: 1

Mike
Mike

Reputation: 3950

You need to instantiate your DecisionTreeClassifier

self.clf = tree.DecisionTreeClassifier()

Upvotes: 1

Related Questions