python program to export numpy/lists in svmlight format
Asked Answered
T

3

5

Any way to export a python array into SVM light format?

Teeming answered 15/2, 2012 at 20:53 Comment(0)
T
6

There is one in scikit-learn:

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

It's basic but it works both for numpy arrays and scipy.sparse matrices.

Turban answered 16/2, 2012 at 20:37 Comment(0)
G
5

I wrote this totally un-optimized script a while ago, maybe it can help! Data and labels 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)
Gayomart answered 16/2, 2012 at 14:7 Comment(0)
S
1

The svmlight-loader module can load an svmlight file into a numpy array. I don't think anything exists for the other direction, but the module is probably a good starting point for extending its functionality.

Sunsunbaked answered 15/2, 2012 at 21:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.