Skip to main content
 首页 » 编程设计

python之Scikit-学习 TransformerMixin : 'numpy.ndarray' object has no attribute 'fit'

2024年12月31日7lexus

我想构建一个 sklearn 管道(更大管道的一部分),它:

  1. 编码分类列(OneHotEncoder)
  2. 降维(SVD)
  3. 添加数字列(无需转换)
  4. 聚合行(pandas groupby)

我用了this管道示例:

和this自定义 TranformerMixin 的示例:

我在第 4 步出错(如果我评论第 4 步则没有错误):

AttributeError Traceback (most recent call last) in () ----> 1 X_train_transformed = pipe.fit_transform(X_train) .... AttributeError: 'numpy.ndarray' object has no attribute 'fit'

我的代码:

from sklearn.pipeline import Pipeline 
from sklearn.preprocessing import OneHotEncoder 
from sklearn.decomposition import TruncatedSVD 
from sklearn.base import BaseEstimator, TransformerMixin 
from sklearn.compose import ColumnTransformer 
 
# does nothing, but is here to collect numerical columns 
class nothing(BaseEstimator, TransformerMixin): 
 
    def fit(self, X, y=None):        
 
        return self 
 
    def transform(self, X):           
 
        return X 
 
 
class Aggregator(BaseEstimator, TransformerMixin): 
 
    def fit(self, X, y=None): 
        return self 
 
    def transform(self, X): 
        X = pd.DataFrame(X) 
        X = X.rename(columns = {0 :'InvoiceNo', 1 : 'amount', 2:'Quantity',  
                                3:'UnitPrice',4:'CustomerID' }) 
        X['InvoiceNo'] =  X['InvoiceNo'].astype('int') 
        X['Quantity'] = X['Quantity'].astype('float64') 
        X['UnitPrice'] = X['UnitPrice'].astype('float64') 
        aggregations = dict() 
        for col in range(5, X.shape[1]-1) : 
            aggregations[col] = 'max' 
 
        aggregations.update({ 'CustomerID' : 'first', 
                            'amount' : "sum",'Quantity' : 'mean', 'UnitPrice' : 'mean'}) 
 
        # aggregating all basket lines 
        result = X.groupby('InvoiceNo').agg(aggregations) 
 
        # add number of lines in the basket 
        result['lines_nb'] = X.groupby('InvoiceNo').size() 
        return result 
 
        numeric_features = ['InvoiceNo','amount', 'Quantity', 'UnitPrice',  
                           'CustomerID'] 
        numeric_transformer = Pipeline(steps=[('nothing', nothing())]) 
 
        categorical_features = ['StockCode', 'Country']    
 
        preprocessor =  ColumnTransformer( 
        [ 
        # 'num' transformer does nothing, but is here to   
        # collect numerical columns 
        ('num', numeric_transformer ,numeric_features ), 
        ('cat', Pipeline([ 
            ('onehot', OneHotEncoder(handle_unknown='ignore')), 
            ('best', TruncatedSVD(n_components=100)), 
         ]), categorical_features)         
          ] 
          ) 
 
# edit with Artem solution 
# aggregator = ('agg', Aggregator()) 
 
pipe = Pipeline(steps=[ 
                      ('preprocessor', preprocessor), 
                      # edit with Artem solution 
                      # ('aggregator', aggregator), 
                      ('aggregator', Aggregator()) 
                     ]) 
 
X_train_transformed = pipe.fit_transform(X_train) 

请您参考如下方法:

流水线步骤在 from ('name', Class) 中,但原始任务本质上有:

aggregator = ('agg', Aggregator())` 
 
pipe = Pipeline(steps=[ 
                      ('preprocessor', preprocessor), 
                      ('aggregator', aggregator), 
]) 

这使得 ('aggregator', ('agg', Aggregator()))