Support Vector Machine APIs - 5.2 English - 68552

AOCL API Guide (68552)

Document ID
68552
Release Date
2025-12-29
Version
5.2 English
class aoclda.svm.SVC(C=1.0, kernel='rbf', degree=3, gamma=-1.0, coef0=0.0, probability=False, tol=0.001, cache_size=200.0, max_iter=-1, tau=1.0e-12, check_data=False)#

Support Vector Classification.

Train a C-Support Vector Classification model.

Note: probability estimation is expensive as it internally uses 5-fold cross validation.

Parameters:
  • C (float, optional) – Regularization parameter. Controls the trade-off between maximizing the margin between classes and minimizing classification errors. A larger value means higher penalty to the loss function on misclassified observations. Must be strictly positive. Default=1.0.

  • kernel (str, optional) – Kernel type to use in the algorithm. Possible values: ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’. Default=’rbf’.

  • degree (int, optional) – Degree of the polynomial kernel function. Ignored by all other kernels. Default=3.

  • gamma (float, optional) – Kernel coefficient. If set to -1, it is calculated as \(1/(Var(X) * n\_features)\). Default=-1.0.

  • coef0 (float, optional) – Independent term in kernel function (check kernel             functions for more details). It is only used in ‘poly’ and ‘sigmoid’ kernel functions. Default=0.0.

  • probability (bool, optional) – Whether to enable probability estimates. Default=False.

  • tol (float, optional) – Tolerance for stopping criterion. Default=0.001.

  • cache_size (float, optional) – Size of the kernel cache (in MB). Default=200.0.

  • max_iter (int, optional) – Hard limit on iterations within solver, or 0 for no limit. Default=0.

  • random_state (int, optional) – Random seed used for probability estimates Use -1 for non deterministic behavior. Default=0.

  • tau (float, optional) – Numerical stability parameter. If it is None then machine epsilon is used. Default=None.

  • check_data (bool, optional) – Whether to check data for NaNs. Default=False.

property bias#

The bias term(s) of the model (also known as the intercept).

Type:

numpy.ndarray or float

decision_function(X, shape='ovr')#

Evaluate the decision function for the samples in X.

In multi-class problems, you can use the ‘shape’ parameter to choose between one-vs-rest (‘ovr’) and one-vs-one (‘ovo’) decision function shape. Note that in our case, OvR decision values are derived from OvO. For binary problems, this parameter is ignored.

Parameters:
  • X (array-like) – Input vectors of shape (n_samples, n_features).

  • shape (str, optional) – Whether to return a one-vs-rest (‘ovr’) decision function or the original one-vs-one (‘ovo’). Default=’ovr’.

Returns:

Decision function values for each sample.

Return type:

numpy.ndarray

property dual_coef#

The dual coefficients of the support vectors.

Type:

numpy.ndarray of shape (n_classes-1, n_support)

fit(X, y)#

Fit the SVC model according to the given training data.

Parameters:
  • X (array-like) – Training vectors of shape (n_samples, n_features).

  • y (array-like) – Target values of shape (n_samples,). They are expected to range from 0 to n_class - 1.

Returns:

Returns the instance itself.

Return type:

self (object)

property n_classes#

The number of classes in the classification problem.

Type:

int

property n_features#

The number of features in the training data.

Type:

int

property n_iter#

The number of features in the training data.

Type:

int

property n_samples#

The number of training samples used to fit the model.

Type:

int

property n_support#

The total number of support vectors.

Type:

int

property n_support_per_class#

The number of support vectors for each class.

Type:

numpy.ndarray of shape (n_classes,)

predict(X)#

Perform classification on samples in X.

Parameters:

X (array-like) – Input vectors of shape (n_samples, n_features).

Returns:

Predicted class labels for samples in X.

Return type:

numpy.ndarray

predict_log_proba(X)#

` Predict class log probabilities for samples in X.

Parameters:

X (array-like) – Input data of shape (n_samples, n_features).

Returns:

Array of shape (n_samples, n_classes) with class log probability estimates.

Return type:

numpy.ndarray

predict_proba(X)#

` Predict class probabilities for samples in X.

Parameters:

X (array-like) – Input data of shape (n_samples, n_features).

Returns:

Array of shape (n_samples, n_classes) with class probability estimates.

Return type:

numpy.ndarray

property probA#

The probability parameter A of the model, used in probability estimates.

Type:

numpy.ndarray or float

property probB#

The probability parameter B of the model, used in probability estimates.

Type:

numpy.ndarray or float

score(X, y)#

Return the mean accuracy on the given test data and labels.

Parameters:
  • X (array-like) – Test samples of shape (n_samples, n_features).

  • y (array-like) – True labels for X.

Returns:

Mean accuracy of self.predict(X) wrt. y.

Return type:

float

property support_vectors#

The support vectors used by the model.

Type:

numpy.ndarray of shape (n_support, n_features)

property support_vectors_idx#

The indices of the support vectors.

Type:

numpy.ndarray of shape (n_support,)

class aoclda.svm.SVR(C=1.0, epsilon=0.1, kernel='rbf', degree=3, gamma=-1.0, coef0=0.0, tol=0.001, cache_size=200.0, max_iter=-1, tau=1.0e-12, check_data=False)#

Support Vector Regression.

Train an epsilon-Support Vector Regression model.

Parameters:
  • C (float, optional) – Regularization parameter. Controls the trade-off between maximizing the margin between classes and minimizing classification errors. A larger value means higher penalty to the loss function on misclassified observations. Must be strictly positive. Default=1.0.

  • epsilon (float, optional) – Epsilon in the SVR model. Defines the tolerance for errors in predictions by creating an acceptable margin (tube) within which errors are not penalized. Default=0.1.

  • kernel (str, optional) – Kernel type to use in the algorithm. Possible values: ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’. Default=’rbf’.

  • degree (int, optional) – Degree of the polynomial kernel function. Ignored by all other kernels. Default=3.

  • gamma (float, optional) – Kernel coefficient. If set to -1, it is calculated as \(1/(Var(X) * n\_features)\). Default=-1.0.

  • coef0 (float, optional) – Independent term in kernel function (check kernel             functions for more details). It is only used in ‘poly’ and ‘sigmoid’ kernel functions. Default=0.0.

  • probability (bool, optional) – Currently not supported. Whether to enable probability estimates. Default=False.

  • tol (float, optional) – Tolerance for stopping criterion. Default=0.001.

  • cache_size (float, optional) – Size of the kernel cache (in MB). Default=200.0.

  • max_iter (int, optional) – Hard limit on iterations within solver, or 0 for no limit. Default=0.

  • tau (float, optional) – Numerical stability parameter. If it is None then machine epsilon is used. Default=None.

  • check_data (bool, optional) – Whether to check data for NaNs. Default=False.

property bias#

The bias term(s) of the model (also known as the intercept).

Type:

numpy.ndarray or float

property dual_coef#

The dual coefficients of the support vectors.

Type:

numpy.ndarray of shape (n_classes-1, n_support)

fit(X, y)#

Fit the SVR model according to the given training data.

Parameters:
  • X (array-like) – Training vectors of shape (n_samples, n_features).

  • y (array-like) – Target values of shape (n_samples,).

Returns:

Returns the instance itself.

Return type:

self (object)

property n_features#

The number of features in the training data.

Type:

int

property n_iter#

The number of features in the training data.

Type:

int

property n_samples#

The number of training samples used to fit the model.

Type:

int

property n_support#

The total number of support vectors.

Type:

int

property n_support_per_class#

The number of support vectors for each class.

Type:

numpy.ndarray of shape (n_classes,)

predict(X)#

Predict regression values for samples in X.

Parameters:

X (array-like) – Input vectors of shape (n_samples, n_features).

Returns:

Predicted values.

Return type:

numpy.ndarray

score(X, y)#

Return the coefficient of determination \(R^2\) of the prediction.

Parameters:
  • X (array-like) – Test samples of shape (n_samples, n_features).

  • y (array-like) – True values for X.

Returns:

\(R^2\) of self.predict(X) wrt. y.

Return type:

float

property support_vectors#

The support vectors used by the model.

Type:

numpy.ndarray of shape (n_support, n_features)

property support_vectors_idx#

The indices of the support vectors.

Type:

numpy.ndarray of shape (n_support,)

class aoclda.svm.NuSVC(nu=0.5, kernel='rbf', degree=3, gamma=-1.0, coef0=0.0, probability=False, tol=0.001, cache_size=200.0, max_iter=-1, tau=1.0e-12, check_data=False)#

Nu-Support Vector Classification.

Train a Nu-Support Vector Classification model.

Note: probability estimation is expensive as it internally uses 5-fold cross validation.

Parameters:
  • nu (float, optional) – An upper bound on the fraction of training errors and a lower bound of the fraction of support vectors. Default=0.5.

  • kernel (str, optional) – Kernel type to use in the algorithm. Possible values: ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’. Default=’rbf’.

  • degree (int, optional) – Degree of the polynomial kernel function. Ignored by all other kernels. Default=3.

  • gamma (float, optional) – Kernel coefficient. If set to -1, it is calculated as \(1/(Var(X) * n\_features)\). Default=-1.0.

  • coef0 (float, optional) – Independent term in kernel function (check kernel             functions for more details). It is only used in ‘poly’ and ‘sigmoid’ kernel functions. Default=0.0.

  • probability (bool, optional) – Whether to enable probability estimates. Default=False.

  • tol (float, optional) – Tolerance for stopping criterion. Default=0.001.

  • cache_size (float, optional) – Size of the kernel cache (in MB). Default=200.0.

  • max_iter (int, optional) – Hard limit on iterations within solver, or 0 for no limit. Default=0.

  • random_state (int, optional) – Random seed used for probability estimates Use -1 for non deterministic behavior. Default=0.

  • tau (float, optional) – Numerical stability parameter. If it is None then machine epsilon is used. Default=None.

  • check_data (bool, optional) – Whether to check data for NaNs. Default=False.

property bias#

The bias term(s) of the model (also known as the intercept).

Type:

numpy.ndarray or float

decision_function(X, shape='ovr')#

Evaluate the decision function for the samples in X.

In multi-class problems, you can use the ‘shape’ parameter to choose between one-vs-rest (‘ovr’) and one-vs-one (‘ovo’) decision function shape. Note that in our case, OvR decision values are derived from OvO. For binary problems, this parameter is ignored.

Parameters:
  • X (array-like) – Input vectors of shape (n_samples, n_features).

  • shape (str, optional) – Whether to return a one-vs-rest (‘ovr’) decision function or the original one-vs-one (‘ovo’). Default=’ovr’.

Returns:

Decision function values for each sample.

Return type:

numpy.ndarray

property dual_coef#

The dual coefficients of the support vectors.

Type:

numpy.ndarray of shape (n_classes-1, n_support)

fit(X, y)#

Fit the NuSVC model according to the given training data.

Parameters:
  • X (array-like) – Training vectors of shape (n_samples, n_features).

  • y (array-like) – Target values of shape (n_samples,). They are expected to range from 0

  • 1. (to n_class -) –

Returns:

Returns the instance itself.

Return type:

self (object)

property n_classes#

The number of classes in the classification problem.

Type:

int

property n_features#

The number of features in the training data.

Type:

int

property n_iter#

The number of features in the training data.

Type:

int

property n_samples#

The number of training samples used to fit the model.

Type:

int

property n_support#

The total number of support vectors.

Type:

int

property n_support_per_class#

The number of support vectors for each class.

Type:

numpy.ndarray of shape (n_classes,)

predict(X)#

Perform classification on samples in X.

Parameters:

X (array-like) – Input vectors of shape (n_samples, n_features).

Returns:

Predicted class labels for samples in X.

Return type:

numpy.ndarray

predict_log_proba(X)#

` Predict class log probabilities for samples in X.

Parameters:

X (array-like) – Input data of shape (n_samples, n_features).

Returns:

Array of shape (n_samples, n_classes) with class log probability estimates.

Return type:

numpy.ndarray

predict_proba(X)#

` Predict class probabilities for samples in X.

Parameters:

X (array-like) – Input data of shape (n_samples, n_features).

Returns:

Array of shape (n_samples, n_classes) with class probability estimates.

Return type:

numpy.ndarray

property probA#

The probability parameter A of the model, used in probability estimates.

Type:

numpy.ndarray or float

property probB#

The probability parameter B of the model, used in probability estimates.

Type:

numpy.ndarray or float

score(X, y)#

Return the mean accuracy on the given test data and labels.

Parameters:
  • X (array-like) – Test samples of shape (n_samples, n_features).

  • y (array-like) – True labels for X.

Returns:

Mean accuracy of self.predict(X) wrt. y.

Return type:

float

property support_vectors#

The support vectors used by the model.

Type:

numpy.ndarray of shape (n_support, n_features)

property support_vectors_idx#

The indices of the support vectors.

Type:

numpy.ndarray of shape (n_support,)

class aoclda.svm.NuSVR(nu=0.5, C=1.0, kernel='rbf', degree=3, gamma=-1.0, coef0=0.0, tol=0.001, cache_size=200.0, max_iter=-1, tau=1.0e-12, check_data=False)#

Nu-Support Vector Regression.

Train a Nu-Support Vector Regression model.

Parameters:
  • nu (float, optional) – An upper bound on the fraction of training errors and a lower bound of the fraction of support vectors. Default=0.5.

  • C (float, optional) – Regularization parameter. Controls the trade-off between maximizing the margin between classes and minimizing classification errors. A larger value means higher penalty to the loss function on misclassified observations. Must be strictly positive. Default=1.0.

  • kernel (str, optional) – Kernel type to use in the algorithm. Possible values: ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’. Default=’rbf’.

  • degree (int, optional) – Degree of the polynomial kernel function. Ignored by all other kernels. Default=3.

  • gamma (float, optional) – Kernel coefficient. If set to -1, it is calculated as \(1/(Var(X) * n\_features)\). Default=-1.0.

  • coef0 (float, optional) – Independent term in kernel function (check kernel             functions for more details). It is only used in ‘poly’ and ‘sigmoid’ kernel functions. Default=0.0.

  • probability (bool, optional) – Currently not supported. Whether to enable probability estimates. Default=False.

  • tol (float, optional) – Tolerance for stopping criterion. Default=0.001.

  • cache_size (float, optional) – Size of the kernel cache (in MB). Default=200.0.

  • max_iter (int, optional) – Hard limit on iterations within solver, or 0 for no limit. Default=0.

  • tau (float, optional) – Numerical stability parameter. If it is None then machine epsilon is used. Default=None.

  • check_data (bool, optional) – Whether to check data for NaNs. Default=False.

property bias#

The bias term(s) of the model (also known as the intercept).

Type:

numpy.ndarray or float

property dual_coef#

The dual coefficients of the support vectors.

Type:

numpy.ndarray of shape (n_classes-1, n_support)

fit(X, y)#

Fit the NuSVR model according to the given training data.

Parameters:
  • X (array-like) – Training vectors of shape (n_samples, n_features).

  • y (array-like) – Target values of shape (n_samples,).

Returns:

Returns the instance itself.

Return type:

self (object)

property n_features#

The number of features in the training data.

Type:

int

property n_iter#

The number of features in the training data.

Type:

int

property n_samples#

The number of training samples used to fit the model.

Type:

int

property n_support#

The total number of support vectors.

Type:

int

property n_support_per_class#

The number of support vectors for each class.

Type:

numpy.ndarray of shape (n_classes,)

predict(X)#

Predict regression values for samples in X.

Parameters:

X (array-like) – Input vectors of shape (n_samples, n_features).

Returns:

Predicted values.

Return type:

numpy.ndarray

score(X, y)#

Return the coefficient of determination \(R^2\) of the prediction.

Parameters:
  • X (array-like) – Test samples of shape (n_samples, n_features).

  • y (array-like) – True values for X.

Returns:

\(R^2\) of self.predict(X) wrt. y.

Return type:

float

property support_vectors#

The support vectors used by the model.

Type:

numpy.ndarray of shape (n_support, n_features)

property support_vectors_idx#

The indices of the support vectors.

Type:

numpy.ndarray of shape (n_support,)

da_status da_svm_select_model_s(da_handle handle, da_svm_model mod)#
da_status da_svm_select_model_d(da_handle handle, da_svm_model mod)#

Select which SVM model to solve.

The model definition can be further enhanced by setting optional parameters such as regularization parameters and kernel functions. See the svm options section for more information.

Parameters:
Returns:

da_status. The function returns:

da_status da_svm_set_data_s(da_handle handle, da_int n_samples, da_int n_features, const float *X, da_int ldx, const float *y)#
da_status da_svm_set_data_d(da_handle handle, da_int n_samples, da_int n_features, const double *X, da_int ldx, const double *y)#

Define the data to train an SVM model.

Pass pointers to a data matrix X containing n_samples observations (rows) over n_features features (columns) and a response vector y of size n_samples.

Only the pointers to X and y are stored; no internal copy is made.

Parameters:
  • handle[inout] a da_handle object, initialized with type da_handle_svm.

  • n_samples[in] the number of observations (rows) of the data matrix X. Constraint: n_samples \(\ge\) 1.

  • n_features[in] the number of features (columns) of the data matrix, X. Constraint: n_features \(\ge\) 1.

  • X[in] the n_samples \(\times\) n_features data matrix. By default, it should be stored in column-major order, unless you have set the storage order option to row-major.

  • ldx[in] the leading dimension of X. Constraint: ldx \(\ge\) n_samples if X is stored in column-major order, or ldx \(\ge\) n_features if X is stored in row-major order.

  • y[in] the response vector, of size n_samples. The label values are expected to range from 0 to n_class - 1.

Returns:

da_status. The function returns:

da_status da_svm_compute_s(da_handle handle)#
da_status da_svm_compute_d(da_handle handle)#

Fit the SVM model defined in the handle.

Compute the SVM model defined by da_svm_select_model_? on the data passed by the last call to the function da_svm_set_data_?.

Parameters:

handle[inout] a da_handle object, initialized with type da_handle_svm.

Returns:

da_status. The function returns:

Post:

After successful execution, da_handle_get_result_? can be queried with the following enum for floating-point output:

  • da_svm_dual_coef - return an array of size n_class-1 \(\times\) n_support containing the dual coefficients of the support vectors.

  • da_svm_bias - return an array of size n_class-1 containing the bias terms.

  • da_svm_probaA - return an array of size n_class-1 containing the parameters A of sigmoid function approximating posterior class probability.

  • da_svm_probaB - return an array of size n_class-1 containing the parameters B of sigmoid function approximating posterior class probability.

  • da_svm_support_vectors - return an array of size n_support \(\times\) n_features containing the support vectors.

  • da_rinfo - return an array of size 3 containing the values of n_samples, n_features, n_class.

In addition da_handle_get_result_int can be queried with the following enums:

  • da_svm_n_support_vectors - return a total number of support vectors (integer).

  • da_svm_n_support_vectors_per_class - return an array of size n_class containing the number of support vectors per class.

  • da_svm_idx_support_vectors - return an array of size n_support containing the indexes of the support vectors.

da_status da_svm_predict_s(da_handle handle, da_int n_samples, da_int n_features, const float *X_test, da_int ldx_test, float *predictions)#
da_status da_svm_predict_d(da_handle handle, da_int n_samples, da_int n_features, const double *X_test, da_int ldx_test, double *predictions)#

Predict labels (or outputs) using the previously fitted SVM model.

Predicts the labels (classification) or values (regression) for the given data matrix X_test, storing results in predictions.

Parameters:
  • handle[inout] a da_handle object, with type da_handle_svm and a model already computed via da_svm_compute_?.

  • n_samples[in] number of observations (rows) in the data matrix X_test. Constraint: n_samples \(\ge\) 1.

  • n_features[in] number of features (columns) of the data matrix X_test. Constraint: n_features has to be the same as in da_svm_set_data_?.

  • X_test[in] the n_samples \(\times\) n_features data matrix. By default, it should be stored in column-major order, unless you have set the storage order option to row-major.

  • ldx_test[in] the leading dimension of X_test. Constraint: ldx_test \(\ge\) n_samples if X_test is stored in column-major order, or ldx_test \(\ge\) n_features if X_test is stored in row-major order.

  • predictions[out] the array of size n_samples to store predicted outcomes.

Returns:

da_status. Possible returns:

da_status da_svm_decision_function_s(da_handle handle, da_int n_samples, da_int n_features, const float *X_test, da_int ldx_test, da_svm_decision_function_shape shape, float *decision_values, da_int ldd)#
da_status da_svm_decision_function_d(da_handle handle, da_int n_samples, da_int n_features, const double *X_test, da_int ldx_test, da_svm_decision_function_shape shape, double *decision_values, da_int ldd)#

Compute the decision function for each sample in X_test using the SVM model.

The decision values are stored in decision_values, and its leading dimension is given by ldd. The parameter shape selects the decision function shape for multi-class classification. Note that One-vs-Rest decision values are constructed from One-vs-One values. The function is defined only for classification problems (for regression simply use da_svm_predict_?).

Parameters:
  • handle[inout] a da_handle object, with type da_handle_svm and a model already computed via da_svm_compute_?.

  • n_samples[in] number of observations (rows) in the data matrix X_test. Constraint: n_samples \(\ge\) 1.

  • n_features[in] number of features (columns) of the data matrix X_test. Constraint: n_features has to be the same as in da_svm_set_data_?.

  • X_test[in] the n_samples \(\times\) n_features data matrix. By default, it should be stored in column-major order, unless you have set the storage order option to row-major.

  • ldx_test[in] leading dimension of X_test. Constraint: ldx_test \(\ge\) n_samples if X_test is stored in column-major order, or ldx_test \(\ge\) n_features if X_test is stored in row-major order.

  • shape[in] a da_svm_decision_function_shape enum specifying either one-vs-rest or one-vs-one, only used in multi-class classification.

  • decision_values[out] array to store the decision function output. Must be of size at least n_samples \(\times\) n_class if shape is one-vs-rest or n_class*(n_class-1)/2 if shape is one-vs-one.

  • ldd[in] leading dimension of decision_values. Constraint: ldd \(\ge\) n_samples if decision_values is stored in column-major order, or if decision_values is stored in row-major order then ldd \(\ge\) n_class if shape is one-vs-rest and ldd \(\ge\) n_class*(n_class-1)/2 if shape is one-vs-one.

Returns:

da_status. Possible returns:

da_status da_svm_score_s(da_handle handle, da_int n_samples, da_int n_features, const float *X_test, da_int ldx_test, const float *y_test, float *score)#
da_status da_svm_score_d(da_handle handle, da_int n_samples, da_int n_features, const double *X_test, da_int ldx_test, const double *y_test, double *score)#

Evaluate the quality of the predictions on the dataset X_test against the true labels y_test.

Computes a scalar score (accuracy for classification, \(R^2\) for regression) and stores it in score.

Parameters:
  • handle[inout] a da_handle object, with type da_handle_svm and a model already computed via da_svm_compute_?.

  • n_samples[in] the number of observations (rows) in the data matrix X_test. Constraint: n_samples \(\ge\) 1.

  • n_features[in] the number of features (columns) of the data matrix X_test. Constraint: n_features has to be the same as in da_svm_set_data_?.

  • X_test[in] the n_samples \(\times\) n_features data matrix. By default, it should be stored in column-major order, unless you have set the storage order option to row-major.

  • ldx_test[in] the leading dimension of X_test. Constraint: ldx_test \(\ge\) n_samples if X_test is stored in column-major order, or ldx_test \(\ge\) n_features if X_test is stored in row-major order.

  • y_test[in] the true labels or responses, of size n_samples.

  • score[out] pointer to a single scalar value in which the score is stored.

Returns:

da_status. Possible returns:

typedef enum da_svm_model_ da_svm_model#

Defines which SVM model is computed.

enum da_svm_model_#

Defines which SVM model is computed.

Values:

enumerator svm_undefined#

No svm model set.

enumerator svc#

C regularized classification.

enumerator nusvc#

Nu regularized classification.

enumerator svr#

Eps regularized regression.

enumerator nusvr#

Nu regularized regression.

typedef enum da_svm_decision_function_shape_ da_svm_decision_function_shape#

Defines which shape of decision function is computed.

enum da_svm_decision_function_shape_#

Defines which shape of decision function is computed.

Values:

enumerator ovr#

One-vs-Rest.

enumerator ovo#

One-vs-One.