SVM
Support Vector Machine (SVM) classification implementation for SciREX.
This module provides a comprehensive SVM implementation using scikit-learn, supporting multiple kernel types with automatic parameter tuning.
Mathematical Background
SVM solves the optimization problem: min_{w,b} 1/2||w||² + C∑max(0, 1 - yᵢ(w·xᵢ + b))
Kernel functions supported: 1. Linear: K(x,y) = x·y 2. RBF: K(x,y) = exp(-γ||x-y||²) 3. Polynomial: K(x,y) = (γx·y + r)^d 4. Sigmoid: K(x,y) = tanh(γx·y + r)
The dual formulation solves: max_α ∑αᵢ - 1/2∑∑αᵢαⱼyᵢyⱼK(xᵢ,xⱼ) subject to: 0 ≤ αᵢ ≤ C, ∑αᵢyᵢ = 0
Key Features
- Multiple kernel functions
- Automatic parameter optimization
- Probability estimation support
- Efficient optimization for large datasets
References
[1] Vapnik, V. (1998). Statistical Learning Theory [2] Scholkopf, B., & Smola, A. J. (2002). Learning with Kernels [3] Platt, J. (1999). Probabilistic Outputs for SVMs
SVMClassifier
Bases: Classification
SVM classifier with automatic parameter tuning.
This implementation supports different kernel types and includes automatic parameter optimization using grid search with cross-validation. Each kernel is optimized for its specific characteristics and use cases.
Attributes:
Name | Type | Description |
---|---|---|
kernel |
Type of kernel function |
|
cv |
Number of cross-validation folds |
|
best_params |
Optional[Dict[str, Any]]
|
Best parameters found by grid search |
Example
classifier = SVMClassifier(kernel="rbf", cv=5) X_train = np.array([[1, 2], [2, 3], [3, 4]]) y_train = np.array([0, 0, 1]) classifier.fit(X_train, y_train) print(classifier.best_params)
Source code in scirex/core/ml/supervised/classification/svm.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
__init__(kernel='rbf', cv=5, **kwargs)
Initialize SVM classifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kernel
|
Literal['linear', 'rbf', 'poly', 'sigmoid']
|
Kernel function type. Options: "linear": Linear kernel for linearly separable data "rbf": Radial basis function for non-linear patterns "poly": Polynomial kernel for non-linear patterns "sigmoid": Sigmoid kernel for neural network-like behavior |
'rbf'
|
cv
|
int
|
Number of cross-validation folds. Defaults to 5. |
5
|
**kwargs
|
Any
|
Additional keyword arguments passed to parent class. |
{}
|
Source code in scirex/core/ml/supervised/classification/svm.py
evaluate(X_test, y_test)
Evaluate model performance on test data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X_test
|
ndarray
|
Test features of shape (n_samples, n_features) |
required |
y_test
|
ndarray
|
True labels of shape (n_samples,) |
required |
Returns:
Type | Description |
---|---|
Dict[str, float]
|
Dictionary containing evaluation metrics: - accuracy: Overall classification accuracy - precision: Precision score (micro-averaged) - recall: Recall score (micro-averaged) - f1_score: F1 score (micro-averaged) |
Source code in scirex/core/ml/supervised/classification/svm.py
fit(X, y)
Fit SVM model with parameter tuning.
Performs grid search to find optimal parameters for the chosen kernel type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
ndarray
|
Training feature matrix of shape (n_samples, n_features) |
required |
y
|
ndarray
|
Training labels of shape (n_samples,) |
required |
Notes
- Uses probability estimation for better prediction granularity
- Employs parallel processing for faster grid search
- May take longer for larger datasets due to quadratic complexity
Source code in scirex/core/ml/supervised/classification/svm.py
get_model_params()
Get parameters of the fitted model.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary containing: - model_type: Type of classifier - kernel: Kernel function used - best_params: Best parameters found by grid search - cv: Number of cross-validation folds used |
Source code in scirex/core/ml/supervised/classification/svm.py
predict(X)
Predict class labels for samples in X.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
ndarray
|
Test samples of shape (n_samples, n_features) |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Array of predicted class labels |
Raises:
Type | Description |
---|---|
ValueError
|
If model hasn't been fitted yet |
Source code in scirex/core/ml/supervised/classification/svm.py
predict_proba(X)
Predict class probabilities for samples in X.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
ndarray
|
Test samples of shape (n_samples, n_features) |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Array of shape (n_samples, n_classes) with class probabilities |
Raises:
Type | Description |
---|---|
ValueError
|
If model hasn't been fitted yet |