rfflearn_logo

rfflearn.cpu.RFFSVC

Support vector classification with random Fourier features. Note that the RFFSCV class uses sklearn.svm.LinearSVC as a backend, and always wraps it with sklearn.multiclass.

def rfflearn.cpu.RFFSVC(self, dim_kernel=128, std_kernel=0.1, W=None, b=None, multi_mode="ovr", n_jobs=-1, **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.

multi_mode: str  (default="ovr")

Treatment of multi-class. This string should be wither of "ovr" (one-versus-the-rest classifier) or "ovo" (one-versus-one classifier).

n_jobs: int  (default=-1)

The number of jobs to run in parallel.

args: dict  (default={})

Extra arguments. This will be passed to scikit-learn's LinearSVC 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.

svm: sklearn.multiclass.OneVsRestClassifier, or OneVsOneClassifier

Linear SVC model instance.

Member functions

def fit(self, X, y, **args)

Trains the SVC 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.SVC

Fitted estimator.

def predict_proba(self, X, **args)

Returns probabilities of possible outcomes for 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_proba function.

Returns:

np.ndarray

The probability for each class with shape (n_samples, n_classes).

def predict_log_proba(self, X, **args)

Returns log probabilities of possible outcomes for 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 sklearn's predict_log_proba function.

Returns:

np.ndarray

The log probability for each class with shape (n_samples, n_classes).

def predict(self, X, **args)

Performs classification 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

Predicted class labels.

def score(self, X, y, **args)

Returns the mean accuracy on the given data. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters:

X: np.ndarray

Input matrix with shape (n_samples, n_features_input).

y: np.ndarray

True class label vector with shape (n_samples,).

args: dict  (default={})

Extra arguments. This arguments will be passed to scikit-learn's score function.

Returns:

float

Mean accuracy of classification.

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([1, 1, 2, 2])                          # Defile label data
>>> svc = rfflearn.RFFSVC().fit(X, y)                   # Training
>>> svc.score(X, y)                                     # Inference (on CPU)
1.0
>>> svc.predict(np.array([[-0.8, -1]]))
array([1])