Thursday, January 13, 2011

Conver mat to libSVM data

>> KSC1_labels = hyperD_id(:,177);
>> KSC1_features = hyperD_id;KSC1_features(:,177) =[];
>> libsvmwrite('KSC1_ORG_libsvm', KSC1_labels, sparse(KSC1_features));

SERARCH: Conver mat to libSVM data
http://agbs.kyb.tuebingen.mpg.de/km/bb/archive/index.php/thread-1123.html

It depends on your data format. We have a simple C code to transfer space/colon separated format to libsvm format. Please contact us if needed.

Alternatively, a simple way is to use libsvmwrite in the libsvm matlab/octave interface. Take a CSV (colon separated format) file in UCI machine learning repository as an example. We download SPECTF.train. Labels are in the first column. The following steps produce a file in the libsvm format.

matlab> SPECTF = csvread('SPECTF.train'); % read a csv file
matlab> labels = SPECTF(:, 1); % labels from the 1st column
matlab> features = SPECTF(:, 2:end);
matlab> features_sparse = sparse(features); % features must be in a sparse matrix
matlab> libsvmwrite('SPECTFlibsvm.train', labels, features_sparse);

The tranformed data are stored in SPECTFlibsvm.train.

=== Then to read the data from Python:
>>> from svmutil import *
# Read data in LIBSVM format
>>> y, x = svm_read_problem('../heart_scale')
>>> m = svm_train(y[:200], x[:200], '-c 4')
>>> p_label, p_acc, p_val = svm_predict(y[200:], x[200:], m)

# There are bounch of LibSVM data on the LibSVM webSite We can try in Python. by just using this command: y -- labels; x -- features
y, x = svm_read_problem('../heart_scale')

No comments:

Post a Comment