svdynamics 0.0.0a0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- svdynamics/__init__.py +61 -0
- svdynamics/kernels.py +148 -0
- svdynamics/logo.py +29 -0
- svdynamics/svm.py +135 -0
- svdynamics/utils.py +53 -0
- svdynamics-0.0.0a0.dist-info/METADATA +110 -0
- svdynamics-0.0.0a0.dist-info/RECORD +10 -0
- svdynamics-0.0.0a0.dist-info/WHEEL +5 -0
- svdynamics-0.0.0a0.dist-info/licenses/LICENSE +21 -0
- svdynamics-0.0.0a0.dist-info/top_level.txt +1 -0
svdynamics/__init__.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import builtins
|
|
2
|
+
import sys
|
|
3
|
+
from .logo import *
|
|
4
|
+
from .kernels import CompositeKernel
|
|
5
|
+
from .svm import SVDClassifier, SVDRegressor
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"CompositeKernel",
|
|
9
|
+
"SVDClassifier",
|
|
10
|
+
"SVDRegressor",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# Detailed Documentation
|
|
15
|
+
|
|
16
|
+
detailed_doc = """
|
|
17
|
+
Welcome to svdynamics! Support Vector Dynamics is a lightweight, scikit-learn
|
|
18
|
+
compatible Python library for building and using mixed (composite) kernels for
|
|
19
|
+
support vector machines. It provides a simple and extensible interface for
|
|
20
|
+
combining multiple kernel functions into a single weighted kernel, while
|
|
21
|
+
remaining fully compatible with existing sklearn pipelines, cross-validation,
|
|
22
|
+
and calibration workflows.
|
|
23
|
+
|
|
24
|
+
svdynamics focuses on making kernel composition a first-class modeling primitive
|
|
25
|
+
for both classification and regression, without requiring any changes to the
|
|
26
|
+
underlying scikit-learn API.
|
|
27
|
+
|
|
28
|
+
PyPI: https://pypi.org/project/svdynamics/
|
|
29
|
+
Documentation: https://lshpaner.github.io/svdynamics/
|
|
30
|
+
|
|
31
|
+
Version: 0.0.0a0
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
# Assign only the detailed documentation to __doc__
|
|
35
|
+
__doc__ = detailed_doc
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
__version__ = "0.0.0a0"
|
|
39
|
+
__author__ = "Leonid Shpaner"
|
|
40
|
+
__email__ = "lshpaner@ucla.edu"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# Define the custom help function
|
|
44
|
+
def custom_help(obj=None):
|
|
45
|
+
"""
|
|
46
|
+
Custom help function to dynamically include ASCII art in help() output.
|
|
47
|
+
"""
|
|
48
|
+
if (
|
|
49
|
+
obj is None or obj is sys.modules[__name__]
|
|
50
|
+
): # When `help()` is called for this module
|
|
51
|
+
print(svdynamics_logo) # Print ASCII art first
|
|
52
|
+
print(detailed_doc) # Print the detailed documentation
|
|
53
|
+
else:
|
|
54
|
+
original_help(obj) # Use the original help for other objects
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# Backup the original help function
|
|
58
|
+
original_help = builtins.help
|
|
59
|
+
|
|
60
|
+
# Override the global help function in builtins
|
|
61
|
+
builtins.help = custom_help
|
svdynamics/kernels.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
from sklearn.metrics.pairwise import linear_kernel, polynomial_kernel, rbf_kernel, sigmoid_kernel
|
|
8
|
+
|
|
9
|
+
from .utils import ParsedKernel, KernelSpec, _validate_weights
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
SUPPORTED_KERNELS = {"linear", "rbf", "poly", "sigmoid"}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _parse_kernel_spec(spec: KernelSpec) -> ParsedKernel:
|
|
16
|
+
# Callable kernel
|
|
17
|
+
if callable(spec):
|
|
18
|
+
return ParsedKernel(kind="callable", params={}, fn=spec)
|
|
19
|
+
|
|
20
|
+
# String kernel
|
|
21
|
+
if isinstance(spec, str):
|
|
22
|
+
k = spec.lower().strip()
|
|
23
|
+
if k not in SUPPORTED_KERNELS:
|
|
24
|
+
raise ValueError(f"Unsupported kernel '{spec}'. Supported: {sorted(SUPPORTED_KERNELS)}")
|
|
25
|
+
return ParsedKernel(kind=k, params={}, fn=None)
|
|
26
|
+
|
|
27
|
+
# Tuple kernel ("rbf", {...})
|
|
28
|
+
if isinstance(spec, tuple) and len(spec) == 2 and isinstance(spec[0], str) and isinstance(spec[1], dict):
|
|
29
|
+
k = spec[0].lower().strip()
|
|
30
|
+
if k not in SUPPORTED_KERNELS:
|
|
31
|
+
raise ValueError(f"Unsupported kernel '{spec[0]}'. Supported: {sorted(SUPPORTED_KERNELS)}")
|
|
32
|
+
return ParsedKernel(kind=k, params=dict(spec[1]), fn=None)
|
|
33
|
+
|
|
34
|
+
raise TypeError(
|
|
35
|
+
"Kernel spec must be one of: "
|
|
36
|
+
"a string ('rbf', 'linear', 'poly', 'sigmoid'), "
|
|
37
|
+
"a tuple like ('rbf', {'gamma': 0.1}), "
|
|
38
|
+
"or a callable kernel(X, Y)->ndarray."
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _compute_kernel(parsed: ParsedKernel, X: np.ndarray, Y: np.ndarray) -> np.ndarray:
|
|
43
|
+
if parsed.kind == "callable":
|
|
44
|
+
assert parsed.fn is not None
|
|
45
|
+
K = parsed.fn(X, Y)
|
|
46
|
+
return np.asarray(K)
|
|
47
|
+
|
|
48
|
+
if parsed.kind == "linear":
|
|
49
|
+
return linear_kernel(X, Y)
|
|
50
|
+
|
|
51
|
+
if parsed.kind == "rbf":
|
|
52
|
+
return rbf_kernel(X, Y, **parsed.params)
|
|
53
|
+
|
|
54
|
+
if parsed.kind == "poly":
|
|
55
|
+
return polynomial_kernel(X, Y, **parsed.params)
|
|
56
|
+
|
|
57
|
+
if parsed.kind == "sigmoid":
|
|
58
|
+
return sigmoid_kernel(X, Y, **parsed.params)
|
|
59
|
+
|
|
60
|
+
raise RuntimeError(f"Unexpected kernel kind: {parsed.kind}")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _kernel_scale_from_diag(Kxx: np.ndarray, eps: float = 1e-12) -> float:
|
|
64
|
+
# Kxx is square kernel matrix
|
|
65
|
+
d = np.diag(Kxx)
|
|
66
|
+
# Use mean of diagonal as a stable scalar, avoid zeros
|
|
67
|
+
s = float(np.sqrt(max(float(np.mean(d * d)), eps)))
|
|
68
|
+
if not np.isfinite(s) or s <= 0.0:
|
|
69
|
+
return 1.0
|
|
70
|
+
return s
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@dataclass
|
|
74
|
+
class CompositeKernel:
|
|
75
|
+
"""
|
|
76
|
+
CompositeKernel builds a callable kernel that is a weighted sum of base kernels.
|
|
77
|
+
|
|
78
|
+
K(X, Y) = sum_m w_m * K_m(X, Y)
|
|
79
|
+
|
|
80
|
+
Parameters
|
|
81
|
+
----------
|
|
82
|
+
kernels:
|
|
83
|
+
List of kernel specs. Each element can be:
|
|
84
|
+
- "rbf" | "linear" | "poly" | "sigmoid"
|
|
85
|
+
- ("rbf", {"gamma": 0.1}) etc.
|
|
86
|
+
- callable kernel(X, Y) -> ndarray
|
|
87
|
+
weights:
|
|
88
|
+
Optional weights for kernels. If None, uniform weights are used.
|
|
89
|
+
Weights are normalized to sum to 1.
|
|
90
|
+
normalize:
|
|
91
|
+
If True, each base kernel is rescaled by a scalar derived from K(X, X)
|
|
92
|
+
to reduce dominance due to scale.
|
|
93
|
+
eps:
|
|
94
|
+
Small constant used for numerical stability.
|
|
95
|
+
"""
|
|
96
|
+
|
|
97
|
+
kernels: Sequence[KernelSpec]
|
|
98
|
+
weights: Optional[Sequence[float]] = None
|
|
99
|
+
normalize: bool = True
|
|
100
|
+
eps: float = 1e-12
|
|
101
|
+
|
|
102
|
+
def __post_init__(self) -> None:
|
|
103
|
+
if self.kernels is None or len(self.kernels) == 0:
|
|
104
|
+
raise ValueError("kernels must be a non-empty sequence")
|
|
105
|
+
|
|
106
|
+
self._parsed: List[ParsedKernel] = [_parse_kernel_spec(s) for s in self.kernels]
|
|
107
|
+
self._weights: np.ndarray = _validate_weights(len(self._parsed), self.weights)
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def parsed_kernels(self) -> List[ParsedKernel]:
|
|
111
|
+
return list(self._parsed)
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def normalized_weights(self) -> np.ndarray:
|
|
115
|
+
return self._weights.copy()
|
|
116
|
+
|
|
117
|
+
def as_callable(self) -> Callable[[np.ndarray, np.ndarray], np.ndarray]:
|
|
118
|
+
def _k(X: np.ndarray, Y: np.ndarray) -> np.ndarray:
|
|
119
|
+
return self(X, Y)
|
|
120
|
+
|
|
121
|
+
return _k
|
|
122
|
+
|
|
123
|
+
def __call__(self, X: np.ndarray, Y: np.ndarray) -> np.ndarray:
|
|
124
|
+
X = np.asarray(X)
|
|
125
|
+
Y = np.asarray(Y)
|
|
126
|
+
|
|
127
|
+
# Compute normalization scalars using K(X, X) for each kernel if requested
|
|
128
|
+
scales: List[float] = []
|
|
129
|
+
if self.normalize:
|
|
130
|
+
for pk in self._parsed:
|
|
131
|
+
Kxx = _compute_kernel(pk, X, X)
|
|
132
|
+
if Kxx.shape[0] != Kxx.shape[1]:
|
|
133
|
+
raise ValueError("Expected square K(X, X) during normalization")
|
|
134
|
+
scales.append(_kernel_scale_from_diag(Kxx, eps=self.eps))
|
|
135
|
+
else:
|
|
136
|
+
scales = [1.0] * len(self._parsed)
|
|
137
|
+
|
|
138
|
+
# Combine kernels
|
|
139
|
+
K_total = None
|
|
140
|
+
for w, s, pk in zip(self._weights, scales, self._parsed):
|
|
141
|
+
K = _compute_kernel(pk, X, Y)
|
|
142
|
+
if K_total is None:
|
|
143
|
+
K_total = (w / s) * K
|
|
144
|
+
else:
|
|
145
|
+
K_total += (w / s) * K
|
|
146
|
+
|
|
147
|
+
assert K_total is not None
|
|
148
|
+
return np.asarray(K_total, dtype=float)
|
svdynamics/logo.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
##################################################################################
|
|
2
|
+
# ASCII/ ANSI Logo
|
|
3
|
+
##################################################################################
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
svdynamics_logo = """
|
|
8
|
+
|
|
9
|
+
███████╗██╗ ██╗██████╗ ██████╗ ██████╗ ██████╗ ████████╗
|
|
10
|
+
██╔════╝██║ ██║██╔══██╗██╔══██╗██╔═══██╗██╔══██╗╚══██╔══╝
|
|
11
|
+
███████╗██║ ██║██████╔╝██████╔╝██║ ██║██████╔╝ ██║
|
|
12
|
+
╚════██║██║ ██║██╔═══╝ ██╔═══╝ ██║ ██║██╔══██╗ ██║
|
|
13
|
+
███████║╚██████╔╝██║ ██║ ╚██████╔╝██║ ██║ ██║
|
|
14
|
+
╚══════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝
|
|
15
|
+
██╗ ██╗███████╗ ██████╗████████╗ ██████╗ ██████╗
|
|
16
|
+
██║ ██║██╔════╝██╔════╝╚══██╔══╝██╔═══██╗██╔══██╗
|
|
17
|
+
██║ ██║█████╗ ██║ ██║ ██║ ██║██████╔╝
|
|
18
|
+
╚██╗ ██╔╝██╔══╝ ██║ ██║ ██║ ██║██╔══██╗
|
|
19
|
+
╚████╔╝ ███████╗╚██████╗ ██║ ╚██████╔╝██║ ██║
|
|
20
|
+
╚═══╝ ╚══════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
|
|
21
|
+
██████╗ ██╗ ██╗███╗ ██╗ █████╗ ███╗ ███╗██╗ ██████╗███████╗
|
|
22
|
+
██╔══██╗╚██╗ ██╔╝████╗ ██║██╔══██╗████╗ ████║██║██╔════╝██╔════╝
|
|
23
|
+
██║ ██║ ╚████╔╝ ██╔██╗ ██║███████║██╔████╔██║██║██║ ███████╗
|
|
24
|
+
██║ ██║ ╚██╔╝ ██║╚██╗██║██╔══██║██║╚██╔╝██║██║██║ ╚════██║
|
|
25
|
+
██████╔╝ ██║ ██║ ╚████║██║ ██║██║ ╚═╝ ██║██║╚██████╗███████║
|
|
26
|
+
╚═════╝ ╚═╝ ╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═════╝╚══════╝
|
|
27
|
+
|
|
28
|
+
"""
|
|
29
|
+
|
svdynamics/svm.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Any, Dict, Optional, Union
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin
|
|
8
|
+
from sklearn.svm import SVC, SVR
|
|
9
|
+
|
|
10
|
+
from .kernels import CompositeKernel
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
KernelArg = Union[str, CompositeKernel, Any] # string for sklearn kernels, CompositeKernel, or callable
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class SVDClassifier(BaseEstimator, ClassifierMixin):
|
|
18
|
+
"""
|
|
19
|
+
SVDClassifier is a thin wrapper around sklearn.svm.SVC that supports CompositeKernel.
|
|
20
|
+
|
|
21
|
+
If kernel is a CompositeKernel, the estimator uses a callable kernel.
|
|
22
|
+
Otherwise it passes through to sklearn SVC.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
C: float = 1.0
|
|
26
|
+
kernel: KernelArg = "rbf"
|
|
27
|
+
degree: int = 3
|
|
28
|
+
gamma: Union[str, float] = "scale"
|
|
29
|
+
coef0: float = 0.0
|
|
30
|
+
shrinking: bool = True
|
|
31
|
+
probability: bool = False
|
|
32
|
+
tol: float = 1e-3
|
|
33
|
+
cache_size: float = 200.0
|
|
34
|
+
class_weight: Optional[Dict[Any, float]] = None
|
|
35
|
+
verbose: bool = False
|
|
36
|
+
max_iter: int = -1
|
|
37
|
+
decision_function_shape: str = "ovr"
|
|
38
|
+
break_ties: bool = False
|
|
39
|
+
random_state: Optional[int] = None
|
|
40
|
+
|
|
41
|
+
def fit(self, X: np.ndarray, y: np.ndarray) -> "SVDClassifier":
|
|
42
|
+
k = self.kernel.as_callable() if isinstance(self.kernel, CompositeKernel) else self.kernel
|
|
43
|
+
|
|
44
|
+
self._model = SVC(
|
|
45
|
+
C=self.C,
|
|
46
|
+
kernel=k,
|
|
47
|
+
degree=self.degree,
|
|
48
|
+
gamma=self.gamma,
|
|
49
|
+
coef0=self.coef0,
|
|
50
|
+
shrinking=self.shrinking,
|
|
51
|
+
probability=self.probability,
|
|
52
|
+
tol=self.tol,
|
|
53
|
+
cache_size=self.cache_size,
|
|
54
|
+
class_weight=self.class_weight,
|
|
55
|
+
verbose=self.verbose,
|
|
56
|
+
max_iter=self.max_iter,
|
|
57
|
+
decision_function_shape=self.decision_function_shape,
|
|
58
|
+
break_ties=self.break_ties,
|
|
59
|
+
random_state=self.random_state,
|
|
60
|
+
)
|
|
61
|
+
self._model.fit(X, y)
|
|
62
|
+
|
|
63
|
+
# ---- expose sklearn-required fitted attributes ----
|
|
64
|
+
self.classes_ = self._model.classes_
|
|
65
|
+
|
|
66
|
+
# optional but good hygiene for sklearn tooling
|
|
67
|
+
if hasattr(self._model, "n_features_in_"):
|
|
68
|
+
self.n_features_in_ = self._model.n_features_in_
|
|
69
|
+
|
|
70
|
+
return self
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def predict(self, X: np.ndarray) -> np.ndarray:
|
|
74
|
+
return self._model.predict(X)
|
|
75
|
+
|
|
76
|
+
def decision_function(self, X: np.ndarray) -> np.ndarray:
|
|
77
|
+
return self._model.decision_function(X)
|
|
78
|
+
|
|
79
|
+
def predict_proba(self, X: np.ndarray) -> np.ndarray:
|
|
80
|
+
if not getattr(self._model, "probability", False):
|
|
81
|
+
raise AttributeError("predict_proba is only available when probability=True")
|
|
82
|
+
return self._model.predict_proba(X)
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def support_vectors_(self) -> np.ndarray:
|
|
86
|
+
return self._model.support_vectors_
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def n_support_(self) -> np.ndarray:
|
|
90
|
+
return self._model.n_support_
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@dataclass
|
|
94
|
+
class SVDRegressor(BaseEstimator, RegressorMixin):
|
|
95
|
+
"""
|
|
96
|
+
SVDRegressor is a thin wrapper around sklearn.svm.SVR that supports CompositeKernel.
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
C: float = 1.0
|
|
100
|
+
kernel: KernelArg = "rbf"
|
|
101
|
+
degree: int = 3
|
|
102
|
+
gamma: Union[str, float] = "scale"
|
|
103
|
+
coef0: float = 0.0
|
|
104
|
+
tol: float = 1e-3
|
|
105
|
+
epsilon: float = 0.1
|
|
106
|
+
shrinking: bool = True
|
|
107
|
+
cache_size: float = 200.0
|
|
108
|
+
verbose: bool = False
|
|
109
|
+
max_iter: int = -1
|
|
110
|
+
|
|
111
|
+
def fit(self, X: np.ndarray, y: np.ndarray) -> "SVDRegressor":
|
|
112
|
+
k = self.kernel.as_callable() if isinstance(self.kernel, CompositeKernel) else self.kernel
|
|
113
|
+
|
|
114
|
+
self._model = SVR(
|
|
115
|
+
C=self.C,
|
|
116
|
+
kernel=k,
|
|
117
|
+
degree=self.degree,
|
|
118
|
+
gamma=self.gamma,
|
|
119
|
+
coef0=self.coef0,
|
|
120
|
+
tol=self.tol,
|
|
121
|
+
epsilon=self.epsilon,
|
|
122
|
+
shrinking=self.shrinking,
|
|
123
|
+
cache_size=self.cache_size,
|
|
124
|
+
verbose=self.verbose,
|
|
125
|
+
max_iter=self.max_iter,
|
|
126
|
+
)
|
|
127
|
+
self._model.fit(X, y)
|
|
128
|
+
return self
|
|
129
|
+
|
|
130
|
+
def predict(self, X: np.ndarray) -> np.ndarray:
|
|
131
|
+
return self._model.predict(X)
|
|
132
|
+
|
|
133
|
+
@property
|
|
134
|
+
def support_(self) -> np.ndarray:
|
|
135
|
+
return self._model.support_
|
svdynamics/utils.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple, Union
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
KernelSpec = Union[
|
|
10
|
+
str, # "rbf", "linear", "poly", "sigmoid"
|
|
11
|
+
Tuple[str, Dict[str, Any]], # ("rbf", {"gamma": 0.1})
|
|
12
|
+
Any, # callable kernel(X, Y) -> ndarray
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class ParsedKernel:
|
|
18
|
+
kind: str # "rbf", "linear", "poly", "sigmoid", or "callable"
|
|
19
|
+
params: Dict[str, Any]
|
|
20
|
+
fn: Optional[Any] = None # for callable kernels
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _as_list(x: Optional[Sequence[Any]]) -> List[Any]:
|
|
24
|
+
if x is None:
|
|
25
|
+
return []
|
|
26
|
+
return list(x)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _validate_weights(num_kernels: int, weights: Optional[Sequence[float]]) -> np.ndarray:
|
|
30
|
+
if weights is None:
|
|
31
|
+
w = np.ones(num_kernels, dtype=float)
|
|
32
|
+
return w / w.sum()
|
|
33
|
+
|
|
34
|
+
w = np.asarray(weights, dtype=float)
|
|
35
|
+
if w.ndim != 1 or w.shape[0] != num_kernels:
|
|
36
|
+
raise ValueError(f"weights must be length {num_kernels}, got shape {w.shape}")
|
|
37
|
+
|
|
38
|
+
if np.any(~np.isfinite(w)):
|
|
39
|
+
raise ValueError("weights must be finite")
|
|
40
|
+
|
|
41
|
+
if np.allclose(w.sum(), 0.0):
|
|
42
|
+
raise ValueError("weights sum to 0")
|
|
43
|
+
|
|
44
|
+
return w / w.sum()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _validate_square_kernel_matrix(K: np.ndarray, name: str = "K") -> None:
|
|
48
|
+
if not isinstance(K, np.ndarray):
|
|
49
|
+
raise TypeError(f"{name} must be a numpy array")
|
|
50
|
+
if K.ndim != 2:
|
|
51
|
+
raise ValueError(f"{name} must be 2D, got {K.ndim}D")
|
|
52
|
+
if K.shape[0] == 0 or K.shape[1] == 0:
|
|
53
|
+
raise ValueError(f"{name} must be non-empty, got shape {K.shape}")
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: svdynamics
|
|
3
|
+
Version: 0.0.0a0
|
|
4
|
+
Summary: Support Vector Dynamics: mixed (composite) kernels for scikit-learn SVMs.
|
|
5
|
+
Author: Leonid Shpaner
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Leon Shpaner
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/lshpaner/svdynamics
|
|
29
|
+
Requires-Python: >=3.8
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
License-File: LICENSE
|
|
32
|
+
Requires-Dist: numpy>=1.21
|
|
33
|
+
Requires-Dist: scikit-learn>=1.0
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
<img src="https://raw.githubusercontent.com/lshpaner/svdynamics/refs/heads/main/assets/svd_logo.svg" height="250" style="border: none; outline: none; box-shadow: none;" oncontextmenu="return false;">
|
|
37
|
+
|
|
38
|
+
Welcome to `svdynamics`! Support Vector Dynamics is a lightweight, scikit-learn
|
|
39
|
+
compatible Python library for building and using mixed (composite) kernels for
|
|
40
|
+
support vector machines. It provides a simple and extensible interface for
|
|
41
|
+
combining multiple kernel functions into a single weighted kernel, while
|
|
42
|
+
remaining fully compatible with existing sklearn pipelines, cross-validation,
|
|
43
|
+
and calibration workflows.
|
|
44
|
+
|
|
45
|
+
`svdynamics` focuses on making kernel composition a first-class modeling primitive
|
|
46
|
+
for both classification and regression, without requiring any changes to the
|
|
47
|
+
underlying scikit-learn API.
|
|
48
|
+
|
|
49
|
+
## Prerequisites
|
|
50
|
+
|
|
51
|
+
Before you install `svdynamics`, ensure your system meets the following requirements:
|
|
52
|
+
|
|
53
|
+
- `Python`: Version `3.8` or higher.
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
Additionally, `svdynamics` depends on the following packages, which will be automatically installed:
|
|
57
|
+
|
|
58
|
+
- `numpy`: version `1.21` or higher
|
|
59
|
+
- `scikit-learn`: version `1.0` or higher
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
## 💾 Installation
|
|
63
|
+
|
|
64
|
+
To install `svdynamics`, simply run the following command in your terminal:
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pip install svdynamics
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Quick Start
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
import numpy as np
|
|
75
|
+
from sklearn.datasets import make_classification
|
|
76
|
+
from svdynamics import CompositeKernel, SVDClassifier
|
|
77
|
+
|
|
78
|
+
X, y = make_classification(n_samples=300, n_features=10, random_state=0)
|
|
79
|
+
|
|
80
|
+
kernel = CompositeKernel(
|
|
81
|
+
kernels=[
|
|
82
|
+
("rbf", {"gamma": 0.2}),
|
|
83
|
+
("linear", {}),
|
|
84
|
+
("poly", {"degree": 2, "coef0": 1.0}),
|
|
85
|
+
],
|
|
86
|
+
weights=[0.6, 0.3, 0.1],
|
|
87
|
+
normalize=True,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
clf = SVDClassifier(C=1.0, kernel=kernel, probability=True, random_state=0)
|
|
91
|
+
clf.fit(X, y)
|
|
92
|
+
proba = clf.predict_proba(X[:5])
|
|
93
|
+
pred = clf.predict(X[:5])
|
|
94
|
+
|
|
95
|
+
print(proba)
|
|
96
|
+
print(pred)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## 📄 Official Documentation
|
|
100
|
+
|
|
101
|
+
https://lshpaner.github.io/svdynamics
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
## 🌐 Authors' Website
|
|
105
|
+
|
|
106
|
+
1. [Leon Shpaner](https://www.leonshpaner.com)
|
|
107
|
+
|
|
108
|
+
## ⚖️ License
|
|
109
|
+
|
|
110
|
+
`svdynamics` is distributed under the MIT License. See [LICENSE](https://github.com/lshpaner/svdynamics/blob/main/LICENSE.md) for more information.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
svdynamics/__init__.py,sha256=lGoDzb38myqFmzTyiw83JXULgROJqpATdHoRdzEfwB8,1747
|
|
2
|
+
svdynamics/kernels.py,sha256=qZD_WhMD4sW8685o3t-czS7patdwsPSPrP7h18q_ork,4949
|
|
3
|
+
svdynamics/logo.py,sha256=rgNBeoHVgkS5toHGgd03N5a5YNS9EnxrXPrsfgwUIO0,3150
|
|
4
|
+
svdynamics/svm.py,sha256=w1-Z6t6Sznx7qEtuvuMPftZ3bYSA0vwBjn8HeUOpljI,4098
|
|
5
|
+
svdynamics/utils.py,sha256=nJ_1Fg1y0dzK0ldBm_z6AVYYJZkCM0fM9k3H6rNpmjw,1578
|
|
6
|
+
svdynamics-0.0.0a0.dist-info/licenses/LICENSE,sha256=EgB5za7q_O_ot4diuV-MKV5dGsAY1g5VDBX1mSrSG14,1069
|
|
7
|
+
svdynamics-0.0.0a0.dist-info/METADATA,sha256=mMiX1rpw1Xh75-tAG5UegPNbxS9pHC_5dCISB_tJVyg,3824
|
|
8
|
+
svdynamics-0.0.0a0.dist-info/WHEEL,sha256=YCfwYGOYMi5Jhw2fU4yNgwErybb2IX5PEwBKV4ZbdBo,91
|
|
9
|
+
svdynamics-0.0.0a0.dist-info/top_level.txt,sha256=S_2tKPzvnhIcuViHPIZKkbV_NpzmCisBBy8UWWmPB3E,11
|
|
10
|
+
svdynamics-0.0.0a0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Leon Shpaner
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
svdynamics
|