0

I am trying to convert Matlab code to Python code. I have downloaded the libsvm library using sudo apt-get install python-libsvm but I am getting an error.

import scipy.io
from svmutil import svm_predict
def SVM_LIBSVM(VECTOR_TRAINING_LABELS , VECTOR_TRAINING_POINTS ,VECTOR_TESTING_LABELS , VECTOR_TESTING_POINTS):
    mat = scipy.io.loadmat('model.mat')
    model = mat['model']
    predicted_label = list()
    predicted_label = svm_predict(VECTOR_TESTING_LABELS , VECTOR_TESTING_POINTS , model)
    return predicted_label

I am getting the following error:

File "/home/optimum/mat2py/svmutil.py", line 193, in svm_predict
    svm_type = m.get_svm_type()
AttributeError: 'numpy.ndarray' object has no attribute 'get_svm_type'

1 Answer 1

1

I'm assuming that model object is from the MATLAB libsvm interface, which would make it a MATLAB struct and not a svm_model object. AFAIK there is not a direct conversion, but you should be able to read the fields from the struct, which you may be able to use for a python svm_model object (not 100% on this; I haven't used the python API much).

model_parameters = model['Parameters']
model_nr_class = model['nr_class']
model_total_sv = model['totalSV']
model_rho = model['rho']
model_label = model['Label']
model_sv_indices = model['sv_indices']
model_prob_a = model['ProbA']
model_prob_b = model['ProbB']
model_n_sv = model['nSV']
model_sv_coef = model['sv_coef']
model_svs = model['SVs']

The most painful way to generate this, if you can't find a way in python, would be to move the MATLAB struct values to a ctypes struct then use toPyModel (python API) to convert it to a python svm_model.

struct svm_model
{
    struct svm_parameter param; /* parameter */
    int nr_class;       /* number of classes, = 2 in regression/one class svm */
    int l;          /* total #SV */
    struct svm_node **SV;       /* SVs (SV[l]) */
    double **sv_coef;   /* coefficients for SVs in decision functions (sv_coef[k-1][l]) */
    double *rho;        /* constants in decision functions (rho[k*(k-1)/2]) */
    double *probA;      /* pairwise probability information */
    double *probB;
    int *sv_indices;        /* sv_indices[0,...,nSV-1] are values in [1,...,num_traning_data] to indicate SVs in the training set */

    /* for classification only */

    int *label;     /* label of each class (label[k]) */
    int *nSV;       /* number of SVs for each class (nSV[k]) */
                /* nSV[0] + nSV[1] + ... + nSV[k-1] = l */
    /* XXX */
    int free_sv;        /* 1 if svm_model is created by svm_load_model*/
                /* 0 if svm_model is created by svm_train */
};
Sign up to request clarification or add additional context in comments.

2 Comments

thnks ..can you give me link to the python api
thanks for replying,can't we do it in pure python like using svm_predict function in package mat2py ? i used it but i am getting the above error i am using the following package github.com/edersoncorbari/mat2py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.