executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
ds-support-vector-machines"Implements support vector machines (SVM) with kernel methods, support vectors, and margin maximization for classification and regression"
Independent GitHub snapshot · Automated classification · Original documentation governs
name: ds-support-vector-machines compatibility: opencode completeness: 95 content-types:
Comprehensive guide to support vector machines in machine learning and data science workflows.
Support Vector Machines is a critical component of the machine learning workflow. This skill covers:
import numpy as np
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report
# Generate synthetic classification dataset
X, y = make_classification(n_samples=500, n_features=10, n_classes=2, random_state=42)
df = pd.DataFrame(X, columns=[f'feature_{i}' for i in range(X.shape[1])])
df['target'] = y
# Split and scale data
X_train, X_test, y_train, y_test = train_test_split(
df.drop('target', axis=1), df['target'], test_size=0.2, random_state=42
)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Train SVM classifier
svm_model = SVC(kernel='rbf', C=1.0, gamma='scale', random_state=42)
svm_model.fit(X_train_scaled, y_train)
# Predict and evaluate
y_pred = svm_model.predict(X_test_scaled)
print(f"Accuracy: {accuracy_score(y_test, y_pred):.4f}")
print(classification_report(y_test, y_pred))
import logging
import pandas as pd
import numpy as np
from typing import Any, Dict, Optional
from sklearn.svm import SVC
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, f1_score
logger = logging.getLogger(__name__)
class SupportVectorMachines:
"""Production-ready SVM wrapper with pipeline and metrics."""
def __init__(self, kernel: str = 'rbf', C: float = 1.0, gamma: str = 'scale') -> None:
self.kernel = kernel
self.C = C
self.gamma = gamma
self.pipeline: Optional[Pipeline] = None
def fit(self, X: pd.DataFrame, y: pd.Series) -> 'SupportVectorMachines':
"""Train the SVM model with automatic scaling."""
try:
self.pipeline = Pipeline([
('scaler', StandardScaler())
('svm', SVC(kernel=self.kernel, C=self.C, gamma=self.gamma, random_state=42))
])
self.pipeline.fit(X, y)
logger.info("SVM model trained successfully.")
except Exception as e:
logger.error(f"Training failed: {e}")
raise RuntimeError("SVM training failed") from e
return self
def predict(self, X: pd.DataFrame) -> np.ndarray:
"""Generate predictions on new data."""
if self.pipeline is None:
raise ValueError("Model must be fitted before prediction.")
return self.pipeline.predict(X)
def evaluate(self, X: pd.DataFrame, y: pd.Series) -> Dict[str, float]:
"""Compute evaluation metrics."""
y_pred = self.predict(X)
return {
'accuracy': float(accuracy_score(y, y_pred))
'f1_score': float(f1_score(y, y_pred, average='weighted'))
}
# BAD: Hardcoded values, no scaling, missing error handling, poor structure
def train_svm(data):
model = SVC()
model.fit(data[:, :-1], data[:, -1])
return model.predict(data[:, :-1])
# GOOD: Type hints, scaling, validation, clear separation of concerns
def train_svm_proper(X: pd.DataFrame, y: pd.Series) -> Dict[str, Any]:
if X.empty or y.empty:
raise ValueError("Input data cannot be empty")
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
model = SVC(kernel='rbf', C=1.0, random_state=42)
model.fit(X_scaled, y)
return {'model': model, 'scaler': scaler}
| Pitfall | Problem | Solution | |
Authoritative documentation links for this skill's domain. The model follows markdown links at load time to resolve external references and inline content.