如何格式化 IRIS 数据集以输入到 SVM-Light 库?

How do I format IRIS data set for input to SVM-Light library?

我正在尝试使用 SVM-Light library for training and classification of the IRIS dataset. Here 是我正在使用的 python 包装器。我目前正在按照页面上的示例进行操作,但我不确定如何正确设置 IRIS 数据的格式以便输入。 IRIS 数据集中的示例行看起来像 5.0,3.6,1.4,0.2,Iris-setosa.

我不知道你的图书馆,但我强烈建议使用 scikit-learn,一个功能强大的通用 ML-lib。我想你有充分的理由使用 svmlight,否则,使用 sklearn 时,基于 libsvm 或 liblinear 的使用要容易得多(全自动;不是基于文件和自动 multi-class 和 co.)。

这里是一些简单的例子。请记住,恕我直言,仅支持二进制目标,如果您需要多 class 学习,您可以使用 sklearn 的 multiclass tools.

加载和准备 Iris 的代码

from sklearn.datasets import load_iris
from sklearn.datasets import dump_svmlight_file

iris = load_iris()
X = iris.data
y = iris.target

""" only keep first two classes """
indices = y<=1
X = X[indices]
y = y[indices]

""" transform to +1 / -1 targets (0 -> -1) """
y[y==0] = -1

dump_svmlight_file(X, y, 'my_dataset', zero_based=False)  # 1-based!!!

svmlight 调用

./svm_learn my_dataset my_output -v3
Scanning examples...done
Reading examples into memory...100..OK. (100 examples read)
Setting default regularization parameter C=0.0199
Optimizing...............done. (16 iterations)
Optimization finished (0 misclassified, maxdiff=0.00057).
Runtime in cpu-seconds: 0.00
Number of SV: 32 (including 28 at upper bound)
L1 loss: loss=4.89469
Norm of weight vector: |w|=0.69732
Norm of longest example vector: |x|=9.13674
Estimated VCdim of classifier: VCdim<=31.50739
Computing XiAlpha-estimates...done
Runtime for XiAlpha-estimates in cpu-seconds: 0.00
XiAlpha-estimate of the error: error<=30.00% (rho=1.00,depth=0)
XiAlpha-estimate of the recall: recall=>70.00% (rho=1.00,depth=0)
XiAlpha-estimate of the precision: precision=>70.00% (rho=1.00,depth=0)
Number of kernel evaluations: 1291
Writing model file...done