【问题标题】:LinearSVC Feature Selection returns different coef_ in PythonLinearSVC 特征选择在 Python 中返回不同的 coef_
【发布时间】:2020-02-12 17:29:57
【问题描述】:

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

请参阅下面的代码片段(也许有一个我没有看到的错误):

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]

【问题讨论】:

    标签: python scikit-learn feature-selection


    【解决方案1】:

    既然你设置了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.        ]
    

    【讨论】:

    • 谢谢。当我使用生成的数据集 X,Y 运行时,我得到了与您相同的答案。但是,我还从 excel 文件中读取我正在使用的数据循环了 10 个打印语句,我得到了不同的答案。我也打印了数据框,数据是一致的,并且顺序也相同,但拟合会产生不同的系数。
    • “我还循环了 10 个打印语句,从我得到不同答案的 excel 文件中读取我正在使用的数据”。你的意思是加载数据10次会导致不同的数据?请发布您正在使用的命令。这很奇怪。你能添加一些数据和完整的代码吗?
    • 我看到了这个问题。我将 C 惩罚参数调整到更精细的水平,从而得到一致的结果。如果在前面的示例中将 C 更改为更宽容的值,例如 1,那么每次循环迭代也会得到不同的结果。感谢头脑风暴,我对机器学习还比较陌生
    猜你喜欢
    • 2018-01-21
    • 2021-04-06
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2017-12-10
    • 2019-05-15
    • 1970-01-01
    相关资源
    最近更新 更多