我正在尝试构建一个套索回归预测模型。我使用scikit learn中的OneHotEncoder,使用one-hot of-K方案对我所有的分类整数特征进行了编码。根据结果,实际影响预测模型的参数只有51个。我想研究这些参数,但它们的编码方式如上所述。你知道如何提取哪个分类整数特征对应哪个热编码数组吗?谢谢!
发布于 2015-12-30 07:02:10
使用sklearn.preprocessing.OneHotEncoder的active_features_,feature_indices_,和n_values_属性,按分类特征在one-hot数组中的位置排序的向量可以如下所示:
import numpy as np
from sklearn import preprocessing
enc = preprocessing.OneHotEncoder()
enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])
enc.active_features_ - np.repeat(enc.feature_indices_[:-1], enc.n_values_)
# array([0, 1, 0, 1, 2, 0, 1, 2, 3], dtype=int64)同样,原始数据可以从one-hot数组返回,如下所示:
x = enc.transform([[0, 1, 1], [1, 2, 3]]).toarray()
# array([[ 1., 0., 0., 1., 0., 0., 1., 0., 0.],
# [ 0., 1., 0., 0., 1., 0., 0., 0., 1.]])
cond = x > 0
[enc.active_features_[c.ravel()] - enc.feature_indices_[:-1] for c in cond]
# [array([0, 1, 1], dtype=int64), array([1, 2, 3], dtype=int64)]发布于 2021-02-11 07:12:46
这是可行的:
import pickle
with open('model.pickle', 'rb') as handle:
one_hot_categories = pickle.load(handle)
print(one_hot_categories.categories_)发布于 2016-07-25 22:51:25
我设计了ple来增强sklearn的管道和FeatureUnion,通过它们,我们还可以在一次热编码或其他预处理步骤之后回溯分类特征。此外,我们可以通过GraphX“绘制”转换:例如,

你可以在my Github page上找到ple。
https://stackoverflow.com/questions/33968353
复制相似问题