布尔索引与维度上的索引数组不匹配如何删除第一个索引并省略res

2024-05-12 17:39:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我对使用lasso algro显示所选功能有问题

X = df.drop(['Target'], axis=1)
y = df.Target

# We use the base estimator LassoCV
clf = LassoCV(cv=3)

# Set a minimum threshold of 0.70
sfm = SelectFromModel(clf, threshold=0.70)
sfm.fit(X, y)
n_features = sfm.transform(X).shape[1]

# Extracting the index of important features
feature_idx = sfm.get_support()

当我想看到选定的功能时,它会抛出eror

df.columns[feature_idx]

错误

IndexError: boolean index did not match indexed array along dimension 0; dimension is 1075 but corresponding boolean dimension is 1074

我知道df.columns包含目标Y,是否有方法删除第一个索引并保留其余索引以避免错误


Tags: columnsofthe功能targetdfindexthreshold
1条回答
网友
1楼 · 发布于 2024-05-12 17:39:33

这意味着布尔数组(例如,[False, True, True])的大小与您尝试索引的数组的大小不对应。如果你只是

df.drop(['Target'], axis=1).columns[feature_idx]

顺序将保持不变,因为特性选择器适用于df.drop(['Target'], axis=1)

相关问题 更多 >