rfflearn.cpu.RFFSVR
Support vector regression with random Fourier features.
def rfflearn.cpu.RFFSVR(self, dim_kernel=128, std_kernel=0.1, W=None, b=None, **args)
Parameters:
dim_kernel: int (default=128)
Dimension of the random matrix.
std_kernel: float (default=0.1)
Standard deviation of the random matrix.
W: np.ndarray (default=None)
Random matrix for the input X. If None then generated automatically.
b: np.ndarray (default=None)
Random bias for the input X. If None then generated automatically.
args: dict (default={})
Extra arguments. This will be passed to scikit-learn's LinearSVR class constructor.
Attributes:
dim: int
Dimension of the random matrix.
s_k: float
Standard deviation of the random matrix.
mat: Callable
A function to generate the random matrix W.
W: np.ndarray
Random matrix for the input X.
b: np.ndarray
Random bias for the input X.
svr: sklearn.svm.SVR
Linear SVR model instance.
Member functions
def fit(self, X, y, **args)
Trains the SVR model according to the given data.
Parameters:
X: np.ndarray
Input matrix with shape (n_samples, n_features_input).
y: np.ndarray
Output vector with shape (n_samples,).
args: dict (default={})
Extra arguments. This arguments will be passed to scikit-learn's fit function.
Returns:
rfflearn.cpu.SVR
Fitted estimator.
def predict(self, X, **args)
Performs regression on the given data.
Parameters:
X: np.ndarray
Input matrix with shape (n_samples, n_features_input).
args: dict (default={})
Extra arguments. This arguments will be passed to scikit-learn's predict function.
Returns:
np.ndarray
The predicted values.
def score(self, X, y, **args)
Returns the R2 score (coefficient of determination) of the prediction.
Parameters:
X: np.ndarray
Input matrix with shape (n_samples, n_features_input).
y: np.ndarray
True Output vector with shape (n_samples,).
args: dict (default={})
Extra arguments. This arguments will be passed to scikit-learn's score function.
Returns:
float
The R2 score of the prediction.
Minimal Example
>>> import numpy as np # Import Numpy
>>> import rfflearn.cpu as rfflearn # Import module
>>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) # Define input data
>>> y = np.array([0.5, 1, 1.5, 2]) # Defile label data
>>> svr = rfflearn.RFFSVR().fit(X, y) # Training (on CPU)
>>> svr.score(X, y) # Inference (on CPU)
0.6160459856695937
>>> svr.predict(np.array([[-0.8, -1]]))
array([1.09586662])