Python - 手动预测使用Linear SVM的拟合模型

时间:2017-04-06 15:29:22

标签: python classification svm prediction linear

来自LinearSVC库的scikit.learn函数.predict使用测试样本执行预测。

LinearSVM_cl.fit(X_train , Y_train) 

的预测
Y_pred_LinearSVM = LinearSVM_cl.predict(X_test)  

但是,我需要知道拟合模型中的哪些参数用于预测测试样本,.coef_? 。截距_?

模型的数据集是20000行,8列获得8个类:

.coef - >

 array([[-1.20185887, -0.62510767, -0.92739275, -0.08900084, -1.11164502,
    -0.56442702,  1.92045989, -0.56706939],
   [ 0.75386897,  0.9672828 , -2.10451063,  0.53552943, -0.10476675,
     0.32058617, -0.30133408, -1.01478727],
   [ 0.35032536, -0.38405342,  0.25462054,  0.47577302, -0.55000734,
     0.01134098, -0.14534849,  1.14597475],
   [-0.08888566, -0.08272116,  0.84141105,  0.22040919,  0.27763948,
     0.57907834, -0.70631803, -0.1017982 ],
   [ 0.14319018,  0.03329494,  1.52575489,  0.58355648,  1.24454465,
    -0.92758526,  1.01315744, -0.51935599],
   [-0.33712774, -0.7826993 , -1.00810522,  0.20346304,  3.67215014,
     0.93187058, -0.26441527, -0.5351838 ],
   [-0.70416157, -2.38388785, -1.24720653,  0.43291862,  3.91473792,
     2.7596399 , -0.63503461, -0.43277051],
   [-0.14921538, -0.03871313, -0.19896247,  0.08522851,  0.29347373,
     0.1332059 , -0.10875692, -0.01503476]])

.intercept - >

array([-0.43454897,  0.05659295, -0.95980815, -1.36353241, -3.05042133,
   -2.93684622, -3.35757856, -1.14034588])

测试样本的例子是

   0.7622999 0.514543 0.2195486 0.453202 0.2585706 0.6295224 0.4999675 0.1960128

如何手动预测测试样本(不使用库中的内置.predict函数)。

1 个答案:

答案 0 :(得分:1)

请注意,您的coef为$ W $,intercept为$ b $,新数据为$ x $。你的班级预测很简单:

$ c = \ arg \ max_i {W_i \ cdot x + b} $

因此,您只需应用矩阵乘法,添加偏向量并选择最大条目的索引。