Python program to export numpy / lists in svmlight format

How to export python array to SVM format?

+5
source share
3 answers

I wrote this completely non-optimized script a while ago, maybe it can help! Data and tags must be in two separate numpy arrays.

def save_svmlight_data(data, labels, data_filename, data_folder = ''):
    file = open(data_folder+data_filename,'w')

    for i,x in enumerate(data):
        indexes = x.nonzero()[0]
        values = x[indexes]

        label = '%i'%(labels[i])
        pairs = ['%i:%f'%(indexes[i]+1,values[i]) for i in xrange(len(indexes))]

        sep_line = [label]
        sep_line.extend(pairs)
        sep_line.append('\n')

        line = ' '.join(sep_line)

        file.write(line)
+5
source

In scikit-learn :

http://scikit-learn.org/stable/modules/generated/sklearn.datasets.dump_svmlight_file.html

It is basic, but it works for both numpy arrays and scipy.sparse.

+5
source

svmlight-loader svmlight numpy. , - , , , .

+1

All Articles