この大きなコードブロックは事前に申し訳ありません。繰り返し可能な作業例を提供することができる最も簡潔な方法です。コードでSklearn - FeatureUnion - Transformer:TypeError:fit_transform()は2つの位置的な引数をとりますが、3が与えられました
、私は1つの列がテキストデータでとてもTfidfVectorizer
であると私はMultiLabelBinarizer
を使用したいので、他のタグのリストの列であるデータフレームから2つの列を変換するためにFeatureUnion
を使用しようとしています。
ItemSelector
トランスフォーマは、データフレームから右の列を選択するためのものです。
なぜ私はTypeError: fit_transform() takes 2 positional arguments but 3 were given
を取得していますか?
この例を正しく実行するには、コードを変更する必要がありますか?
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.base import TransformerMixin, BaseEstimator
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model import SGDClassifier
import pandas as pd
import numpy as np
d = {'label': ['Help', 'Help', 'Other', 'Sale/Coupon', 'Other', 'Help', 'Help',
'Other', 'Sale/Coupon', 'Other', 'Help', 'Help', 'Other',
'Sale/Coupon', 'Other', 'Help', 'Help', 'Other', 'Sale/Coupon',
'Other', 'Help', 'Help', 'Other', 'Sale/Coupon', 'Other'],
'multilabels': ["['Samples']", "['Deck']", "['Deck', 'Deck Over', 'Stain']",
"['Coupons']", "['Bathroom']", "['Samples']", "['Deck']",
"['Deck', 'Deck Over', 'Stain']", "['Coupons']",
"['Bathroom']", "['Samples']", "['Deck']",
"['Deck', 'Deck Over', 'Stain']", "['Coupons']",
"['Bathroom']", "['Samples']", "['Deck']",
"['Deck', 'Deck Over', 'Stain']", "['Coupons']",
"['Bathroom']", "['Samples']", "['Deck']",
"['Deck', 'Deck Over', 'Stain']", "['Coupons']",
"['Bathroom']"],
'response': ['this is some text', 'this is some more text',
'and here is some more', 'and some more',
'and here we go some more yay done', 'this is some text',
'this is some more text', 'and here is some more',
'and some more', 'and here we go some more yay done',
'this is some text', 'this is some more text',
'and here is some more', 'and some more',
'and here we go some more yay done', 'this is some text',
'this is some more text', 'and here is some more',
'and some more', 'and here we go some more yay done',
'this is some text', 'this is some more text',
'and here is some more', 'and some more',
'and here we go some more yay done']}
class ItemSelector(BaseEstimator, TransformerMixin):
def __init__(self, key):
self.key = key
def fit(self, X, y=None):
return self
def transform(self, df):
return df[self.key]
feature_union = FeatureUnion(
transformer_list=[
('step1', Pipeline([
('selector', ItemSelector(key='response')),
('tfidf', TfidfVectorizer()),
])),
('step2', Pipeline([
('selector', ItemSelector(key='multilabels')),
('multilabel', MultiLabelBinarizer())
]))
])
pipeline = OneVsRestClassifier(
Pipeline([('union', feature_union),('sgd', SGDClassifier())])
)
grid = GridSearchCV(pipeline, {}, verbose=5)
df = pd.DataFrame(d, columns=['response', 'multilabels', 'label'])
X = df[['response', 'multilabels']]
y = df['label']
grid.fit(X, y)
これは完全な誤りである:
Traceback (most recent call last):
File "C:/Users/owner/Documents/my files/Account Tracking/Client/Foresee Analysis/SOQuestion.py", line 72, in <module>
grid.fit(X, y)
File "C:\Python34\lib\site-packages\sklearn\model_selection\_search.py", line 945, in fit
return self._fit(X, y, groups, ParameterGrid(self.param_grid))
File "C:\Python34\lib\site-packages\sklearn\model_selection\_search.py", line 564, in _fit
for parameters in parameter_iterable
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 758, in __call__
while self.dispatch_one_batch(iterator):
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 608, in dispatch_one_batch
self._dispatch(tasks)
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 571, in _dispatch
job = self._backend.apply_async(batch, callback=cb)
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 109, in apply_async
result = ImmediateResult(func)
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 326, in __init__
self.results = batch()
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in __call__
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in <listcomp>
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "C:\Python34\lib\site-packages\sklearn\model_selection\_validation.py", line 238, in _fit_and_score
estimator.fit(X_train, y_train, **fit_params)
File "C:\Python34\lib\site-packages\sklearn\multiclass.py", line 216, in fit
for i, column in enumerate(columns))
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 758, in __call__
while self.dispatch_one_batch(iterator):
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 608, in dispatch_one_batch
self._dispatch(tasks)
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 571, in _dispatch
job = self._backend.apply_async(batch, callback=cb)
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 109, in apply_async
result = ImmediateResult(func)
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 326, in __init__
self.results = batch()
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in __call__
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in <listcomp>
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "C:\Python34\lib\site-packages\sklearn\multiclass.py", line 80, in _fit_binary
estimator.fit(X, y)
File "C:\Python34\lib\site-packages\sklearn\pipeline.py", line 268, in fit
Xt, fit_params = self._fit(X, y, **fit_params)
File "C:\Python34\lib\site-packages\sklearn\pipeline.py", line 234, in _fit
Xt = transform.fit_transform(Xt, y, **fit_params_steps[name])
File "C:\Python34\lib\site-packages\sklearn\pipeline.py", line 734, in fit_transform
for name, trans, weight in self._iter())
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 758, in __call__
while self.dispatch_one_batch(iterator):
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 608, in dispatch_one_batch
self._dispatch(tasks)
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 571, in _dispatch
job = self._backend.apply_async(batch, callback=cb)
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 109, in apply_async
result = ImmediateResult(func)
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 326, in __init__
self.results = batch()
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in __call__
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "C:\Python34\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in <listcomp>
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "C:\Python34\lib\site-packages\sklearn\pipeline.py", line 577, in _fit_transform_one
res = transformer.fit_transform(X, y, **fit_params)
File "C:\Python34\lib\site-packages\sklearn\pipeline.py", line 303, in fit_transform
return last_step.fit_transform(Xt, y, **fit_params)
TypeError: fit_transform() takes 2 positional arguments but 3 were given
注:私は_transform() takes 2 positional arguments but 3 were givenを見てきましたが、それはまだ私には意味がありません。