自定义sklearn变压器类中的矩阵(X)的维数(行)减少

Swepab

给定以下代码,我正在尝试构建的自定义转换器类(旨在添加一些列并通过网格搜索消除残障)单独运行良好,但是通过管道执行时,行的维数下降。也许有人可以解释出了什么问题,我在这里显然遗漏了一些东西。搜索评论“这里发生了什么,维数减少了?” 我在那里打印问题。完整的执行代码可以在下面找到!

import pandas as pd
import numpy as np

from sklearn.datasets import load_breast_cancer
from sklearn import linear_model
from sklearn.base import BaseEstimator
from sklearn.base import ClassifierMixin
from sklearn.base import clone
from sklearn.base import TransformerMixin
from sklearn.model_selection import GridSearchCV

from sklearn.pipeline import Pipeline
from sklearn.pipeline import make_pipeline

dict_breast_c = load_breast_cancer()
X = pd.DataFrame(dict_breast_c.data, columns=dict_breast_c.feature_names)
X.columns = [col.replace(" ", "_").replace("mean", "avg") for col in X.columns]
X_sub = X[[col for col in X.columns if col.find("avg") >= 0]]
X_feat_hldr = X[[col for col in X.columns if col not in (X_sub.columns)]]

y = pd.Series(dict_breast_c.target)

print ("Full X matrix shape: {}".format(X_sub.shape))
print ("Full feature holder shape: {}".format(X_feat_hldr.shape))
print ("Target vector: {}".format(y.shape))

class c_FeatureAdder(BaseEstimator, TransformerMixin):

    def __init__(self, X_feat_hldr, add_error_feat = True, add_worst_feat = True): # no *args or **kargs
        self.add_error_feat = add_error_feat
        self.add_worst_feat = add_worst_feat
        self.list_col_error = list_col_error
        self.list_col_wrst = list_col_wrst
        self.X_feat_hldr = X_feat_hldr

    def fit(self, X, y=None):
        return self  # nothing else to do

    def transform(self, X, y=None):

        if self.add_error_feat and not self.add_worst_feat:
            print ("Adding error (std) features:")
            return np.c_([X, self.X_feat_hldr[self.list_col_error]])

        elif not self.add_error_feat and self.add_worst_feat:
            print ("Adding worst features:")
            return np.c_([X, self.X_feat_hldr[self.list_col_wrst]])

        elif self.add_error_feat and self.add_worst_feat:
            # What happends here, dimensionality reduced in rows?
            print ("Adding error (std) features and worst features")
            print ("Feature: {}".format(self.list_col_error))
            print ("Feature: {}".format(self.list_col_wrst))
            print ("Something happends to number of rows! {}".format(X.shape))
            print (self.X_feat_hldr.shape)
            print (np.c_[X, self.X_feat_hldr[self.list_col_wrst].values, self.X_feat_hldr[self.list_col_error].values])
            return np.c_[X, self.X_feat_hldr[self.list_col_wrst].values, self.X_feat_hldr[self.list_col_error].values]

        else:
            print ("Adding no new features, passing indata:")
            return X




# Set a classifier, start with base form of logistic regression
clf_log_reg = linear_model.LogisticRegression(random_state=1234)

# Input into pipeline for doing feature adding to main data
list_col_error = [col for col in X_feat_hldr[0:2] if col.find("error") >= 0][0:1]
list_col_wrst = [col for col in X_feat_hldr[0:2] if col.find("worst") >= 0][0:2]

print (list_col_error)
print (list_col_wrst)

# Generate a pipeline of wanted transformers on data. End with classifier
pipe_log_reg = Pipeline(
    [('add_feat', c_FeatureAdder(X_feat_hldr))
    ,('clf', clf_log_reg)]
)




# Set the parameter grid to be checked for pipe above. Only thing being changed is the adding of features through c_FeatureAdder() class
param_grid = {
    'add_feat__add_error_feat' : [True, False]
    ,'add_feat__add_worst_feat' : [True, False]
    ,'clf__penalty' : ['l2', 'l1']
    ,'clf__C' : [1]
}



# Initialize GridSearch over parameter spacea
gs_lg_reg = GridSearchCV(
    estimator = pipe_log_reg
    ,param_grid = param_grid
    ,scoring = 'accuracy'
    ,n_jobs = 1
)


# Assign names
X_train = X_sub.values
y_train = y.values

print (X_train.shape)

# Fit data
gs_lg_reg_fit = gs_lg_reg.fit(X_train
                              ,y_train)

# Best estimator from GridSearch
gs_optimal_mdl_lg_reg = gs_lg_reg_fit.best_estimator_
阿德林

你有一些错误。

您在转换器中使用了全局变量:

    self.list_col_error = list_col_error
    self.list_col_wrst = list_col_wrst

如果您的变压器需要输入,则应将其作为构造函数(__init__的参数避免在类中依赖全局变量。

您的转换器应能够转换任意数量的给定样本。

transform函数的思想是转换任何给定的样本或样本集。实际上,您可以保留变压器,以后再使用它来使用它来转换任意数量的新给定样本。您应该使用该fit函数获取所需的任何内容作为输入,并相应地安装变压器。这个想法是,一旦您拥有fit了整个管道,就应该能够为其提供一个样本,并从管道中获得该样本的输出。

GridSearchCV默认情况下进行3倍交叉验证。

文档所述

cv:int,交叉验证生成器或可迭代的,可选的

确定交叉验证拆分策略。简历的可能输入是:

,要使用默认的三折交叉验证

这意味着它在每个阶段都使用输入数据的2/3来适应管道。如果检查输出,则会看到代码抱怨新数据有379行,而旧数据有569行。379 / 569 = 0.666080844这是更改的行数来自的地方。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章