LinearSVC Feature Selection在Python中返回不同的coef

2024-10-01 00:30:50 发布

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

我在训练数据集上使用SelectFromModel和LinearSVC。培训和测试集已被拆分,并保存在单独的文件中。当我在训练集上拟合LinearSVC时,我得到一组coef[0],我试图找到最重要的特征。当我重新运行这个脚本时,我得到了不同的coef[0]值,即使它在相同的训练数据上。为什么会这样?你知道吗

请参见下面的代码片段(可能有一个我看不到的bug):

fig = plt.figure()

#SelectFromModel
lsvc = LinearSVC(C=.01, penalty="l1", dual= False).fit(X_train, Y_train.values.ravel())
X_trainPro = SelectFromModel(lsvc,prefit=True)
sscores = lsvc.coef_[0]
print(sscores)
ax = fig.add_subplot(1, 1, 1)

for i in range(len(sscores)):
    sscores[i] = np.abs(sscores[i])

sscores_sum = 0
for i in range(len(sscores)):
    sscores_sum = sscores_sum + sscores[i]

for i in range(len(sscores)):
    sscores[i] = sscores[i] / sscores_sum

stemp = sscores.copy()
total_weight = 0
feature_numbers = 0
while (total_weight <= .9):
    total_weight = total_weight + stemp.max()
    stemp[np.nonzero(stemp == stemp.max())[0][0]] = 0
    feature_numbers += 1

print(total_weight, feature_numbers)

stemp = sscores.copy()
sfeaturenames = np.array([])
orderScore = np.array([])
for i in range(len(sscores)):
    sfeaturenames = np.append(sfeaturenames, X_train.columns[np.nonzero(stemp == stemp.max())[0][0]])
    orderScore = np.append(orderScore, stemp.max())
    stemp[np.nonzero(stemp == stemp.max())[0][0]] = -1

lowscore = orderScore[feature_numbers]
smask1 = orderScore <= lowscore
smask2 = orderScore > lowscore
ax.bar(sfeaturenames[smask2],orderScore[smask2], align = "center", color = "green")
ax.bar(sfeaturenames[smask1],orderScore[smask1], align = "center", color = "blue")
ax.set_title("SelectFromModel")
ax.tick_params(labelrotation=90)

plt.subplots_adjust(hspace=2, bottom=.2, top= .85)
plt.show()

#selection of the top values to use
Top_Rank = np.array([])
scores = sscores

for i in range(feature_numbers):
    Top_item = scores.max()
    Top_item_loc = np.where(scores == np.max(scores))
    Top_Rank = np.append(Top_Rank,X_train.columns[Top_item_loc])
    scores[Top_item_loc] = 0
print(Top_Rank)
X_train = X_train[Top_Rank]
X_test = X_test[Top_Rank]

Tags: infortopnprangetrainaxmax
1条回答
网友
1楼 · 发布于 2024-10-01 00:30:50

既然设置了dual=False,就应该得到相同的系数。你的sklearn版本是什么?你知道吗

运行此命令并检查是否得到相同的输出:

from sklearn.svm import LinearSVC
from sklearn.datasets import make_classification

X, y = make_classification(n_features=4, random_state=0)
for i in range(10):
    lsvc = LinearSVC(C=.01, penalty="l1", dual= False).fit(X, y)
    sscores = lsvc.coef_[0]
    print(sscores)

输出应该完全相同。你知道吗

[0.         0.         0.27073732 0.        ]
[0.         0.         0.27073732 0.        ]
[0.         0.         0.27073732 0.        ]
[0.         0.         0.27073732 0.        ]
[0.         0.         0.27073732 0.        ]
[0.         0.         0.27073732 0.        ]
[0.         0.         0.27073732 0.        ]
[0.         0.         0.27073732 0.        ]
[0.         0.         0.27073732 0.        ]
[0.         0.         0.27073732 0.        ]

相关问题 更多 >