We start by loading the Iris dataset, which contains measurements of sepal length and width for three species of iris flowers.
We select only the Setosa and Versicolor classes for simplicity.
We extract the first two features (sepal length and width) from the dataset for training our classifier.
The species labels are converted to numeric values for training the SVM model.
We use the fitcsvm function to train a Support Vector Machine (SVM) classifier on the selected features and labels.
We use hold on to ensure that subsequent plots are added to the existing figure.
We create a grid of points (XGrid) to represent the entire feature space.
We use the trained SVM model to predict the class scores for each point in the grid.
The contour plot is used to visualize the decision boundary of the SVM classifier.
- The decision boundary is the line where the class score is 0.5 (as in this example, 0 is for class 1, 1 is for class 2 so mid-point is for hyperplane), separating Setosa and Versicolor classes.
- The contour plot is created using contour with a level of 0.5 and displayed in black ('k').
(Dig-deep In the context of SVMs, the decision boundary is the line (for 2D data) or hyperplane (for higher-dimensional data) that separates the classes. The contour function is used to plot the decision boundary because the decision function of the SVM is a continuous function that assigns a score to each point in the input space.
The decision boundary is where the score is exactly 0. In a binary classification setting like the Iris dataset (with classes 0 and 1), the decision boundary is where the score is 0.5 because values below 0.5 are classified as one class (e.g., 0) and values above 0.5 are classified as the other class (e.g., 1).
The contour function is used to plot the region where the score is close to 0.5, which corresponds to the decision boundary. The [0.5 0.5] argument specifies the level at which the contour should be drawn. Since we're interested in the decision boundary where the score is 0.5, we specify [0.5 0.5] as the contour level.
)
引用格式
Ayesha Sohail (2025). Visulizing SVM with Iris Data for Binary Classes (https://ww2.mathworks.cn/matlabcentral/fileexchange/165566-visulizing-svm-with-iris-data-for-binary-classes), MATLAB Central File Exchange. 检索时间: .
MATLAB 版本兼容性
创建方式
R2024a
兼容任何版本
平台兼容性
Windows macOS Linux标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!版本 | 已发布 | 发行说明 | |
---|---|---|---|
1.0.0 |