小编Res*_*per的帖子

Sklearn Pipeline:在ColumnTransformer中的OneHotEncode之后获取功能名称

我准备好管道后要获取功能名称。

categorical_features = ['brand', 'category_name', 'sub_category']
categorical_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
    ('onehot', OneHotEncoder(handle_unknown='ignore'))])

numeric_features = ['num1', 'num2', 'num3', 'num4']
numeric_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='median')),
    ('scaler', StandardScaler())])

preprocessor = ColumnTransformer(
    transformers=[
        ('num', numeric_transformer, numeric_features),
        ('cat', categorical_transformer, categorical_features)])
Run Code Online (Sandbox Code Playgroud)

然后

clf = Pipeline(steps=[('preprocessor', preprocessor),
                      ('regressor', GradientBoostingRegressor())])
Run Code Online (Sandbox Code Playgroud)

拟合熊猫数据框后,我可以从中获得功能重要性

clf.steps[1][1].feature_importances_

我尝试了clf.steps[0][1].get_feature_names()但是出现了一个错误

AttributeError: Transformer num (type Pipeline) does not provide get_feature_names.
Run Code Online (Sandbox Code Playgroud)

如何从中获取功能名称?

python scikit-learn

12
推荐指数
4
解决办法
2477
查看次数

Python,Pandas:获得列表中第一个None位置的最佳方法,该位置给出最大的连续None计数

我有包含None如下列表的列表。

l1 = [None, 1, None, None, 2, None, None]
l2 = [None, 1, 1, None, None, None, 2, None, None]
Run Code Online (Sandbox Code Playgroud)

我想None在此列表中获得第一个位置,该位置给出最大的连续 None计数。

get_start_None_pos(l1) # should return 2
get_start_None_pos(l2) # should return 3
Run Code Online (Sandbox Code Playgroud)

我目前使用Pandas的方法效果很好,但是当我要处理的列表太多时,速度太慢。

def get_start_None_pos(l: list) -> int:
    s = pd.Series(l)
    s = s.isna()
    s = s.cumsum() - s.cumsum().where(~s).ffill().fillna(0)
    return int(s.idxmax() - s.max() + 1)
Run Code Online (Sandbox Code Playgroud)

我想知道,有没有更好的办法来解决这样的问题?

python numpy pandas python-3.6

3
推荐指数
1
解决办法
64
查看次数

标签 统计

python ×2

numpy ×1

pandas ×1

python-3.6 ×1

scikit-learn ×1