---
# System prepended metadata

title: Support Vector Machine

---

**SVM Classifier Overview:**

Support Vector Machine is a supervised machine learning algorithm used for classification tasks. It works by finding a hyperplane that best separates data points of different classes in the feature space.

We'll use the Iris dataset for this example.

**Example Using Iris Dataset:**

**Step 1: Import Libraries**
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report
```

**Step 2: Load and Explore the Iris Dataset**
```python
# Load the Iris dataset
iris = datasets.load_iris()
X = iris.data  # Feature matrix
y = iris.target  # Target labels
```

**Step 3: Split the Data into Training and Testing Sets**
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```

**Step 4: Create and Train the SVM Classifier Model**
```python
# Create an SVM classifier model
svm_model = SVC(kernel='linear', random_state=42)

# Train the model on the training data
svm_model.fit(X_train, y_train)
```
*Params That Can be Changed*
1.	**kernel** (default=“rbf”):
	- Specifies the kernel type to be used in the algorithm. Common options include “linear,” “poly” (polynomial), “rbf” (radial basis function), “sigmoid,” and more.
    
2.	**C** (default=1.0):
	- Regularization parameter. A smaller C value increases the margin but may allow some training points to be misclassified. A larger C value makes the margin narrower but reduces the number of misclassifications on the training set.
        
3.	**degree** (default=3):
	- Degree of the polynomial kernel function (only relevant if kernel="poly"). It specifies the degree of the polynomial used in the kernel function.
        
4.	**gamma** (default=“scale”):
	- Kernel coefficient for “rbf,” “poly,” and “sigmoid.” If “scale,” it is calculated as 1 / (n_features * X.var()) and if “auto,” it is calculated as 1 / n_features.
        
5.	**coef0** (default=0.0):
	- Independent term in kernel function. It is only significant in “poly” and “sigmoid” kernels.
        
6.	**shrinking** (default=True):
	- Whether to use the shrinking heuristic. It can help speed up the training process for large datasets.
        
7.	**probability** (default=False):
	- Whether to enable probability estimates. If set to True, the predict_proba method becomes available to estimate class probabilities.
        
8.	**random_state** (default=None):
	 -The seed used by the random number generator for randomizing data. Setting this ensures reproducibility.


**Step 5: Make Predictions**
```python
# Make predictions on the test data
y_pred = svm_model.predict(X_test)
```

**Step 6: Evaluate the Model**
```python
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100:.2f}%")

# Generate a classification report
classification_rep = classification_report(y_test, y_pred, target_names=iris.target_names)
print("Classification Report:")
print(classification_rep)
```

**Explanation:**

1. We import the necessary libraries, including NumPy for numerical operations, Matplotlib for visualization, scikit-learn for SVM classification and dataset loading, and more.

2. We load the Iris dataset, which contains features like sepal length and width, and target labels representing different iris species.

3. We split the dataset into training and testing sets. Here, we use 80% of the data for training and 20% for testing.

4. We create an SVM classifier model using `SVC` (Support Vector Classification). We specify the kernel as "linear," which means it will use a linear hyperplane to separate the classes.

5. The model is trained on the training data using `fit`.

6. We use the trained model to make predictions on the test data.

7. We evaluate the model's performance using accuracy and generate a classification report that includes precision, recall, F1-score, and support for each class.

Support Vector Machines are versatile classifiers and can work well for various classification tasks. This example demonstrates its implementation for a classification task using the Iris dataset. You can adapt it for your own classification tasks and datasets.