pythonsvmscikit-learn-pipeline

Getting number of support vectors of a RBF SVC in a sklearn pipeline


Is it possible to get the number of support vectors and (or) their values for an RBF SVC when it is fit using a sklearn Pipeline object? My pipeline looks like this

dim_reduction = TruncatedSVD( n_components = dim_reduction_n_comp, random_state = 611 )   
classifier_obj = sklearn.pipeline.Pipeline([
        ('scaler', sklearn.preprocessing.StandardScaler()),
        ( 'dim_reduction', dim_reduction ),
        ( 'svc', sklearn.svm.SVC(C= svc_c, gamma = svc_gamma, probability = True ) )
    ])

I want to calculate the footprint of the SVC by getting the support vectors and their coefficients and use it as a parameter to optuna optimization study.


Solution

  • The Number of support vectors for each class can be retrieved from the property n_support_ of the SVC object, using:

    classifier_obj[2].n_support_

    The result will be an ndarray of shape (n_classes,) with integers which represent the number of vectors for each class, in the corresponding order as in the property classes_, which contains the classes labels:

    classifier_obj[2].classes_