【问题标题】:Using active_features_ and feature_indices_ in OneHotEncoder in scikit-learn version 0.21.2在 scikit-learn 版本 0.21.2 的 OneHotEncoder 中使用 active_features_ 和 feature_indices_
【发布时间】:2020-02-25 18:49:19
【问题描述】:

我对在 Python 中使用 scikit 库非常陌生,我的 scikit-learn 版本是 0.21.2。我使用OneHotEncoder 对数据集中的分类变量进行编码。

现在我正在尝试使用herehere 给出的代码,按照以下 2 个链接将编码列链接回原始变量

import pandas as pd
import numpy as np
results = []

for i in range(enc.active_features_.shape[0]):
    f = enc.active_features_[i]

    index_range = np.extract(enc.feature_indices_ <= f, enc.feature_indices_)
    s = len(index_range) - 1
    f_index = index_range[-1]
    f_label_decoded = f - f_index

    results.append({
            'label_decoded_value': f_label_decoded,
            'coefficient': clf.coef_[0][i]
        })

R = pd.DataFrame.from_records(results)
from sklearn import preprocessing
encoder = preprocessing.OneHotEncoder(categorical_features=[0,1,2])
X_train = encoder.fit_transform(data_train)
print encoder.feature_indices_

不幸的是,它不断抛出这些错误

'OneHotEncoder' object has no attribute '_active_features_'
'OneHotEncoder' object has no attribute '_feature_indices_'

如何解决这些错误并让代码正常工作。

【问题讨论】:

    标签: python pandas scikit-learn one-hot-encoding


    【解决方案1】:

    我认为您所指的解决方案实际上使逻辑更加复杂。

    get_feature_names() 就足够了。

    例子:

    import numpy as np
    import pandas as pd
    from sklearn.preprocessing import OneHotEncoder
    from sklearn.linear_model import LogisticRegression
    
    n_samples = 50
    data = pd.DataFrame({'colors': np.random.choice(['red', 'blue', 'green'], n_samples),
                         'shapes': np.random.choice(['circle', 'square'], n_samples)})
    
    y = np.random.choice(['apples', 'oranges'], n_samples)
    
    enc = OneHotEncoder()
    X = enc.fit_transform(data)
    lr = LogisticRegression().fit(X, y)
    
    pd.DataFrame({'feature_names': enc.get_feature_names(data.columns),
                          'coef': np.squeeze(lr.coef_)})
    
    

    【讨论】:

      猜你喜欢
      • 2016-02-09
      • 2018-11-01
      • 2016-12-28
      • 2020-01-25
      • 2020-02-01
      • 2019-04-09
      • 2018-08-02
      • 2020-11-02
      相关资源
      最近更新 更多