必须使用DecisionTreeClassifier实例作为第一个参数调用unbound方法fit()(获取Stock实例)

时间:2016-08-16 20:09:17

标签: python scikit-learn

我试图访问fit课程中clf对象上的Stock方法,我收到此错误:

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

股票类:

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

我实例化课程的地方:

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

抛出错误的地方:

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

3 个答案:

答案 0 :(得分:2)

要完成其他正确答案,这里有一个小例子可以帮助您了解您的错误行self.clf = tree.DecisionTreeClassifier的含义:

class f(object):

    def __init__(self):
        pass

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

答案 1 :(得分:1)

您需要实例化DecisionTreeClassifier

self.clf = tree.DecisionTreeClassifier()

答案 2 :(得分:1)

您没有实例化[2016-08-16 19:29:37,127] ERROR - APIUsageStatisticsRestClientImpl Error occurred while Connecting to DAS REST API [2016-08-16 19:29:37,129] ERROR - usage:jag org.wso2.carbon.apimgt.usage.client.exception.APIMgtUsageQueryServiceClientException: Error occurred while Connecting to DAS REST API 。因此,您需要在类上调用tree.DecisionTreeClassifier方法,并且需要告诉它您要使用的实例,就像它说的那样。

据推测,您想要实例化fit()

tree.DecisionTreeClassifier