matlab 介绍SVM新函数时有这样一个例子:Load Fisher's iris data set. Remove the sepal lengths and widths, and all observed setosa irises.
程序:
load fisheriris %我看了一下,fisheriris是matlab系统自带的fisheriris.csv文件,里面有5列数据,151行,
第1行是项目名称:SepalLength,SepalWidth,PetalLength,PetalWidth,Species,第2行以后是5.1,3.5,1.4,0.2,setosa;。。。。等等这样的数据。
load的意思是读入fisheriris这个文件到系统里。
inds = ~strcmp(species,'setosa');% 返回值inds是一个和species有相同size的logical array,ind的元素是1或0。把species中的每个元素和s做比较,如果一致,则对应位置的元素为1,否则,为0。
数据文件中species列下面是'setosa'的共有50行。再取反,应该就是一列向量,前50项是0,后面100项是1。
X = meas(inds,3:4);y = species(inds);%处理好X,y
SVMModel = fitcsvm(X,y)%生成一个分类器SVMModel = ClassificationSVM ResponseName: 'Y' CategoricalPredictors: [] ClassNames: {'versicolor' 'virginica'} ScoreTransform: 'none' NumObservations: 100 Alpha: [24×1 double] Bias: -14.4149 KernelParameters: [1×1 struct] BoxConstraints: [100×1 double] ConvergenceInfo: [1×1 struct] IsSupportVector: [100×1 logical] Solver: 'SMO'The first class ('versicolor'
) is the negative class, and the second ('virginica'
) is the positive class. You can change the class order duringtraining by using the'ClassNames'
name-value pair argument.