Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python NotFitteError:使用管道对数据进行拟合和转换,但仍会出现未拟合错误_Python_Scikit Learn - Fatal编程技术网

Python NotFitteError:使用管道对数据进行拟合和转换,但仍会出现未拟合错误

Python NotFitteError:使用管道对数据进行拟合和转换,但仍会出现未拟合错误,python,scikit-learn,Python,Scikit Learn,在使用管道进行一次热编码之后,我正在尝试获取功能名称。我可以看到我的数据按照指定进行了转换,但是当我试图获取特征名称时,我得到了这个NotFitteError。这些是我采取的步骤 # full pipeline for data preprocessing # seperate the fetures and the target X_train = train.drop("target", axis=1).copy() y_train = train["targ

在使用管道进行一次热编码之后,我正在尝试获取功能名称。我可以看到我的数据按照指定进行了转换,但是当我试图获取特征名称时,我得到了这个NotFitteError。这些是我采取的步骤

# full pipeline for data preprocessing

# seperate the fetures and the target
X_train = train.drop("target", axis=1).copy()
y_train = train["target"].copy()
X_test = test.copy()

# create preprocessing pipeline
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer

# select numerical and categorical columns
num_cols = X_train.select_dtypes(exclude="object").columns.tolist()
cat_cols = X_train.select_dtypes(include="object").columns.tolist()

# numerical pipeline
num_pipe = make_pipeline(SimpleImputer(strategy="mean"), StandardScaler())

# categorical pipeline
cat_pipe = make_pipeline(
    SimpleImputer(strategy="constant", fill_value="NA"),
    OneHotEncoder(handle_unknown='ignore', sparse=False),
)

# full pipeline for data preprocessing
full_pipe = ColumnTransformer(
    [("num", num_pipe, num_cols), ("cat", cat_pipe, cat_cols)]
)
满管输出-

当我尝试获取特征名称时,在装配管道之后,我得到了这个NotFitteError

full_pipe.transformers[1][1].steps[1][1]
output - OneHotEncoder(handle_unknown='ignore', sparse=False)
# get one-hot encoded feature names
full_pipe.transformers[1][1].steps[1][1].get_feature_names()

NotFittedError: This OneHotEncoder instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

有人能告诉我为什么会这样吗?

这能回答你的问题吗?
full_pipe.transformers[1][1].steps[1][1]
output - OneHotEncoder(handle_unknown='ignore', sparse=False)
# get one-hot encoded feature names
full_pipe.transformers[1][1].steps[1][1].get_feature_names()

NotFittedError: This OneHotEncoder instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.