TypeError: get_params() missing 1 required positional argument: 'self'
TypeError: get_params() missing 1 required positional argument: 'self'
我正在尝试使用 scikit-learn
包和 python-3.4 进行网格搜索,
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model.logistic import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.grid_search import GridSearchCV
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.metrics import precision_score, recall_score, accuracy_score
from sklearn.preprocessing import LabelBinarizer
import numpy as np
pipeline = Pipeline([
('vect', TfidfVectorizer(stop_words='english')),
('clf', LogisticRegression)
])
parameters = {
'vect__max_df': (0.25, 0.5, 0.75),
'vect__stop_words': ('english', None),
'vect__max_features': (2500, 5000, 10000, None),
'vect__ngram_range': ((1, 1), (1, 2)),
'vect__use_idf': (True, False),
'vect__norm': ('l1', 'l2'),
'clf__penalty': ('l1', 'l2'),
'clf__C': (0.01, 0.1, 1, 10)
}
if __name__ == '__main__':
grid_search = GridSearchCV(pipeline, parameters, n_jobs=-1, verbose=1, scoring='accuracy', cv = 3)
df = pd.read_csv('SMS Spam Collection/SMSSpamCollection', delimiter='\t', header=None)
lb = LabelBinarizer()
X, y = df[1], np.array([number[0] for number in lb.fit_transform(df[0])])
X_train, X_test, y_train, y_test = train_test_split(X, y)
grid_search.fit(X_train, y_train)
print('Best score: ', grid_search.best_score_)
print('Best parameter set:')
best_parameters = grid_search.best_estimator_.get_params()
for param_name in sorted(best_parameters):
print(param_name, best_parameters[param_name])
但是,它没有运行成功,错误消息如下所示:
Fitting 3 folds for each of 1536 candidates, totalling 4608 fits
Traceback (most recent call last):
File "/home/xiangru/PycharmProjects/machine_learning_note_with_sklearn/grid search.py", line 36, in <module>
grid_search.fit(X_train, y_train)
File "/usr/local/lib/python3.4/dist-packages/sklearn/grid_search.py", line 732, in fit
return self._fit(X, y, ParameterGrid(self.param_grid))
File "/usr/local/lib/python3.4/dist-packages/sklearn/grid_search.py", line 493, in _fit
base_estimator = clone(self.estimator)
File "/usr/local/lib/python3.4/dist-packages/sklearn/base.py", line 47, in clone
new_object_params[name] = clone(param, safe=False)
File "/usr/local/lib/python3.4/dist-packages/sklearn/base.py", line 35, in clone
return estimator_type([clone(e, safe=safe) for e in estimator])
File "/usr/local/lib/python3.4/dist-packages/sklearn/base.py", line 35, in <listcomp>
return estimator_type([clone(e, safe=safe) for e in estimator])
File "/usr/local/lib/python3.4/dist-packages/sklearn/base.py", line 35, in clone
return estimator_type([clone(e, safe=safe) for e in estimator])
File "/usr/local/lib/python3.4/dist-packages/sklearn/base.py", line 35, in <listcomp>
return estimator_type([clone(e, safe=safe) for e in estimator])
File "/usr/local/lib/python3.4/dist-packages/sklearn/base.py", line 45, in clone
new_object_params = estimator.get_params(deep=False)
TypeError: get_params() missing 1 required positional argument: 'self'
我也试过只用
if __name__ == '__main__':
pipeline.get_params()
它给出了同样的错误信息。
谁知道如何解决这个问题?
这个错误几乎总是具有误导性,实际上 意味着您在 class 上调用实例方法,而不是实例(如调用 dict.keys()
而不是 dict
上的 d.keys()
命名为 d
).*
这正是这里发生的事情。 The docs 暗示 best_estimator_
属性,就像初始化器的 estimator
参数一样,不是一个估计器 实例 ,它是一个估计器 类型,以及"A object of that type is instantiated for each grid point."
所以,如果你想调用方法,你必须为某个特定的网格点构造一个该类型的对象。
但是,快速浏览一下文档,如果您试图获取用于返回最佳分数的最佳估计器的特定实例的参数,那不就是 best_params_
? (很抱歉,这部分有点猜测......)
对于 Pipeline
调用,您肯定在那里有一个实例。该方法唯一的 documentation 是一个参数规范,表明它采用一个可选参数 deep
。但在幕后,它可能将 get_params()
调用转发给它的属性之一。使用 ('clf', LogisticRegression)
,看起来您正在使用 class LogisticRegression
构建它,而不是 class 的实例,所以如果这就是它最终转发到的目标,那就可以解释问题了。
* 错误显示 "missing 1 required positional argument: 'self'" 而不是 "must be called on an instance" 之类的原因是在 Python 中,d.keys()
有效地变成了 dict.keys(d)
,并且明确地这样称呼它是完全合法的(有时有用),所以 Python 不能真正告诉你 dict.keys()
是非法的,只是它缺少 self
参数。
我终于把问题解决了。原因正如abarnert所说
首先我尝试了:
pipeline = LogisticRegression()
parameters = {
'penalty': ('l1', 'l2'),
'C': (0.01, 0.1, 1, 10)
}
而且效果很好。
凭借这种直觉,我将管道修改为:
pipeline = Pipeline([
('vect', TfidfVectorizer(stop_words='english')),
('clf', LogisticRegression())
])
注意LogisticRegression
后面有个()
。
这次成功了。
我正在尝试使用 scikit-learn
包和 python-3.4 进行网格搜索,
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model.logistic import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.grid_search import GridSearchCV
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.metrics import precision_score, recall_score, accuracy_score
from sklearn.preprocessing import LabelBinarizer
import numpy as np
pipeline = Pipeline([
('vect', TfidfVectorizer(stop_words='english')),
('clf', LogisticRegression)
])
parameters = {
'vect__max_df': (0.25, 0.5, 0.75),
'vect__stop_words': ('english', None),
'vect__max_features': (2500, 5000, 10000, None),
'vect__ngram_range': ((1, 1), (1, 2)),
'vect__use_idf': (True, False),
'vect__norm': ('l1', 'l2'),
'clf__penalty': ('l1', 'l2'),
'clf__C': (0.01, 0.1, 1, 10)
}
if __name__ == '__main__':
grid_search = GridSearchCV(pipeline, parameters, n_jobs=-1, verbose=1, scoring='accuracy', cv = 3)
df = pd.read_csv('SMS Spam Collection/SMSSpamCollection', delimiter='\t', header=None)
lb = LabelBinarizer()
X, y = df[1], np.array([number[0] for number in lb.fit_transform(df[0])])
X_train, X_test, y_train, y_test = train_test_split(X, y)
grid_search.fit(X_train, y_train)
print('Best score: ', grid_search.best_score_)
print('Best parameter set:')
best_parameters = grid_search.best_estimator_.get_params()
for param_name in sorted(best_parameters):
print(param_name, best_parameters[param_name])
但是,它没有运行成功,错误消息如下所示:
Fitting 3 folds for each of 1536 candidates, totalling 4608 fits
Traceback (most recent call last):
File "/home/xiangru/PycharmProjects/machine_learning_note_with_sklearn/grid search.py", line 36, in <module>
grid_search.fit(X_train, y_train)
File "/usr/local/lib/python3.4/dist-packages/sklearn/grid_search.py", line 732, in fit
return self._fit(X, y, ParameterGrid(self.param_grid))
File "/usr/local/lib/python3.4/dist-packages/sklearn/grid_search.py", line 493, in _fit
base_estimator = clone(self.estimator)
File "/usr/local/lib/python3.4/dist-packages/sklearn/base.py", line 47, in clone
new_object_params[name] = clone(param, safe=False)
File "/usr/local/lib/python3.4/dist-packages/sklearn/base.py", line 35, in clone
return estimator_type([clone(e, safe=safe) for e in estimator])
File "/usr/local/lib/python3.4/dist-packages/sklearn/base.py", line 35, in <listcomp>
return estimator_type([clone(e, safe=safe) for e in estimator])
File "/usr/local/lib/python3.4/dist-packages/sklearn/base.py", line 35, in clone
return estimator_type([clone(e, safe=safe) for e in estimator])
File "/usr/local/lib/python3.4/dist-packages/sklearn/base.py", line 35, in <listcomp>
return estimator_type([clone(e, safe=safe) for e in estimator])
File "/usr/local/lib/python3.4/dist-packages/sklearn/base.py", line 45, in clone
new_object_params = estimator.get_params(deep=False)
TypeError: get_params() missing 1 required positional argument: 'self'
我也试过只用
if __name__ == '__main__':
pipeline.get_params()
它给出了同样的错误信息。 谁知道如何解决这个问题?
这个错误几乎总是具有误导性,实际上 意味着您在 class 上调用实例方法,而不是实例(如调用 dict.keys()
而不是 dict
上的 d.keys()
命名为 d
).*
这正是这里发生的事情。 The docs 暗示 best_estimator_
属性,就像初始化器的 estimator
参数一样,不是一个估计器 实例 ,它是一个估计器 类型,以及"A object of that type is instantiated for each grid point."
所以,如果你想调用方法,你必须为某个特定的网格点构造一个该类型的对象。
但是,快速浏览一下文档,如果您试图获取用于返回最佳分数的最佳估计器的特定实例的参数,那不就是 best_params_
? (很抱歉,这部分有点猜测......)
对于 Pipeline
调用,您肯定在那里有一个实例。该方法唯一的 documentation 是一个参数规范,表明它采用一个可选参数 deep
。但在幕后,它可能将 get_params()
调用转发给它的属性之一。使用 ('clf', LogisticRegression)
,看起来您正在使用 class LogisticRegression
构建它,而不是 class 的实例,所以如果这就是它最终转发到的目标,那就可以解释问题了。
* 错误显示 "missing 1 required positional argument: 'self'" 而不是 "must be called on an instance" 之类的原因是在 Python 中,d.keys()
有效地变成了 dict.keys(d)
,并且明确地这样称呼它是完全合法的(有时有用),所以 Python 不能真正告诉你 dict.keys()
是非法的,只是它缺少 self
参数。
我终于把问题解决了。原因正如abarnert所说
首先我尝试了:
pipeline = LogisticRegression()
parameters = {
'penalty': ('l1', 'l2'),
'C': (0.01, 0.1, 1, 10)
}
而且效果很好。
凭借这种直觉,我将管道修改为:
pipeline = Pipeline([
('vect', TfidfVectorizer(stop_words='english')),
('clf', LogisticRegression())
])
注意LogisticRegression
后面有个()
。
这次成功了。