When I try to use the svmlight python package with data I already converted to svmlight format I get an error. It should be pretty basic, I don't understand what's happening. Here's the code:
import svmlight
training_data = open('thedata', "w")
model=svmlight.learn(training_data, type='classification', verbosity=0)
I've also tried:
training_data = numpy.load('thedata')
and
training_data = __import__('thedata')
One obvious problem is that you are truncating your data file when you open it because you are specifying write mode
"w"
. This means that there will be no data to read.Anyway, you don't need to read the file like that if your data file is like the one in this example, you need to import it because it is a python file. This should work:
You might want to compare your data against that of the example.
Edit after data file format clarified
The input file needs to be parsed into a list of tuples like this:
The svmlight package does not appear to support reading from a file in the SVM file format, and there aren't any parsing functions, so it will have to be implemented in Python. SVM files look like this:
so here is a parser that converts from the file format to that required by the svmlight package:
And you can use it like this: