【问题标题】:sklearn: How to get column indexes deleted by model.transform from original dataframesklearn:如何从原始数据框中获取由 model.transform 删除的列索引
【发布时间】:2016-06-29 11:30:27
【问题描述】:

我正在尝试应用未来的选择。问题是使用整个数据帧会引发内存错误。所以我决定削减我的数据框以便能够应用使用下一个未来的选择:

# this is original dataframes
X_full = df_train[df_train.columns[0:size]] # 76000(rows)*300(cols)
y_full = df_train[[len(df_train.columns)-1]] # 76000(rows)*1(col)

y_full 包含 0 和 1,数字 1 低于 5%。所有其他列仅包含数字,但我们不知道它们的意思。

#this is way, I reduce the number of rows to 10%
test_frac = 0.10
count = len(X_full)
X = X_full.iloc[-int(count*test_frac):]
y = y_full.iloc[-int(count*test_frac):]

#Then I use Linear models penalized with the L1 norm to reduce the dimensionality of the data
lsvc = LinearSVC(C=0.01, penalty="l1", dual=False).fit(X, y)
model = SelectFromModel(lsvc, prefit=True)
X_new = model.transform(X)
print "X_new.shape", X_new.shape
print X_new

问题是我需要获取已删除的列列表,以便从原始数据框中删除它们。我该怎么做?

【问题讨论】:

  • 你能告诉我们更多关于 rhe 数据集的信息吗?

标签: python python-2.7 machine-learning scikit-learn dataframe


【解决方案1】:

听起来您正在寻找SelectFromModel.get_support()。根据文档,它可以返回 (1) 长度等于 all 特征数量的布尔数组 (2) 包含特征的整数索引:

从特征向量中选择保留特征的索引。如果 indices 为 False,则这是一个形状为 [# input features] 的布尔数组,其中一个元素为 True,前提是它的相应特征被选中进行保留。如果 indices 为 True,则这是一个形状为 [# output features] 的整数数组,其值是输入特征向量的索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    相关资源
    最近更新 更多