matlab 中机器学习工具箱中分类器下有SVM工具,其中介绍SVM新函数时有这样一个例子:LoadFisher's iris data set. Remove the sepal lengths and widths, and all observedsetosa 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这个文件到系统里。运行之后matlab工作区生成2个文件。
Meas就是前四列全部数据的矩阵,不含名称这行;species就是最后一列即第五列数据。
strcmp(species,'setosa');就是把species过滤一下,把species中的每个元素和s做比较,如果一致,则对应位置的元素为1,否则,为0。前50行是'setosa'就变成1,后100行不是就是0。inds = ~strcmp(species,'setosa');% 取反后返回值inds是一个和species有相同size的logical array,inds的元素是1或0。数据文件中species列里面是'setosa'的共有50行。再取反,应该就是一列向量,前50项是0,后面100项是1。
X = meas(inds,3:4); 前2列按程序目的被移除,前50行是'setosa',也被移除,剩下X中100项就是需要的数据。
y = species(inds);%y就是剩下100项对应的名字。
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 Command Window shows that SVMModel is atrained ClassificationSVM classifier and a property list. Display theproperties of SVMModel, for example, to determine the class order, by using dotnotation.
用类似classOrder = SVMModel.ClassNames 这样的命令可以看分类器的属性
classOrder =
2×1cell array
'versicolor'
'virginica'
The first class ('versicolor') is thenegative class, and the second ('virginica') is the positive class. You canchange the class order during