代码之家  ›  专栏  ›  技术社区  ›  william007

管道中的fit vs fit_变换

  •  2
  • william007  · 技术社区  · 7 年前

    在本页 https://www.kaggle.com/baghern/a-deep-dive-into-sklearn-pipelines

    它叫 fit_transfrom 用于转换数据,如下所示:

    from sklearn.pipeline import FeatureUnion
    
    feats = FeatureUnion([('text', text), 
                          ('length', length),
                          ('words', words),
                          ('words_not_stopword', words_not_stopword),
                          ('avg_word_length', avg_word_length),
                          ('commas', commas)])
    
    feature_processing = Pipeline([('feats', feats)])
    feature_processing.fit_transform(X_train)
    

    而在特征处理的训练中,它只使用 fit 然后 predict

    from sklearn.ensemble import RandomForestClassifier
    
    pipeline = Pipeline([
        ('features',feats),
        ('classifier', RandomForestClassifier(random_state = 42)),
    ])
    
    pipeline.fit(X_train, y_train)
    
    preds = pipeline.predict(X_test)
    np.mean(preds == y_test)
    

    问题是 适合 在上进行转换 X_train (如通过 transform ,因为我们没有打电话 fit_transform 这里)第二个病例?

    1 回复  |  直到 7 年前
        1
  •  3
  •   Ankish Bansal    7 年前

    sklearn-pipeline 有一些很好的特点。它以非常干净的方式执行多项任务。我们定义我们的 features 它的 transformation 和 list of classifiers ,我们想执行一个功能。

    第一步

    pipeline = Pipeline([
        ('features',feats),
        ('classifier', RandomForestClassifier(random_state = 42)),
    ])
    

    您已经定义了特征的名称及其转换函数(包含在 feat ),在第二步中,您已经定义了分类器的名称和分类器。

    现在打电话的时候 pipeline.fit 首先对特征进行拟合和变换,然后对变换后的特征进行分类器拟合。所以,它为我们做了一些步骤。你能做的更多 check-here