我做错了什么? (python /数组声明)

时间:2019-01-04 21:08:59

标签: arrays python-3.x syntax-error declaration

我正在写这个(4个值)

clf2 = LogisticRegression()
scores2 = cross_val_score (clf2, X, y, cv=10)

...

clf5 = ExtraTreesClassifier(n_estimators=100, max_depth=None,
    min_samples_split=5, random_state=0)
scores5 = cross_val_score(clf5, X, y, cv=5)

然后这个

class myEnsemble:
    def __init_(self, models):
        self.models = model
    def fit(self, X, y):
        for model in self.models:
            model.fit(X, y)
    def predict (self, X):
        preds = [model. predict(X) for model in self.models]
        res = []
        for i in range(len(X)):
            line = [preds[j][i] for j in range (len(preds))]
            res.append(round(sum(line) / len(line)))
        return res
model = myEnsemble([clf2, clf3, clf4, clf5])
model.fit (X_train, y_train)`

但是我收到此错误

myEnsemble() takes no arguments

为什么会出现此错误

1 个答案:

答案 0 :(得分:4)

__init__应该有两个下划线。您只给出了一个下划线。因此,python将其解释为只是另一个常规函数,而不是构造函数。使用默认构造函数,因此期望使用0个参数。要解决此问题,只需在def __init_(self, models):的末尾添加另一个下划线“ _”使其变为def __init__(self, models):