Ceri Stagg avatar Ceri Stagg committed 45b9e98

Seems to work, but no pickling

Comments (0)

Files changed (1)

svmlight/svmlight.py

 from ctypes import *
 from collections import namedtuple
+import pickle
 
 import localdata
 
 # ----------------------------------------------
 
 
-UnpackData = namedtuple('Unpackdata', 'words doc_label queryid slackid costfactor numwords')
+UnpackData = namedtuple('Unpackdata', 'words doc_label queryid slackid costfactor')
 
 WordTuple = namedtuple('WordTuple', 'wnum weight' )
 # ----------------------------------------------
         words.append( wordtuple )
         # print( "Feature pair wnum %r weight %r" % ( wordtuple.wnum, wordtuple.weight ) )
 
-    returnval = UnpackData( words, label, queryid, slackid, costfactor, len( words ) )
+    words.append( WordTuple( 0, 0.0 ) )
+
+    returnval = UnpackData( words, label, queryid, slackid, costfactor )
     return returnval
 # ----------------------------------------------
 
         unpackdata = unpack_document( item, max_words )
         numwords = len( unpackdata.words )
         if numwords > 0:
-            candidatewords = unpackdata.words[-1].wnum
+            candidatewords = unpackdata.words[-2].wnum
             if candidatewords > totwords:
                 totwords = candidatewords
 
 
 SVMCallTuple = namedtuple('SVMCallTuple', 'doclistdata client_data kernel_cache model' )
 
-def svm_learn2( doclist, **kwds):
+LearnResultsTuple = namedtuple( 'LearnResultsTuple', 'model docs totdoc' )
+
+def svm_learn( doclist, **kwds):
     
     client_data = read_learning_parameters( **kwds )
 
     elif client_data.plearn.type == RANKING:
         call_svm_method_without_null( "svm_learn_ranking", svm_call_tuple )
 
+    result = LearnResultsTuple( model, doclistdata.docs, doclistdata.totdoc )
+    return result 
     # return (learn_parm, kernal_parm)
 # ----------------------------------------------
 
 
 # -------------------- MAIN --------------------
 if __name__ == "__main__":
-    result = svm_learn2( localdata.train0, type='classification' )
-    print result
+    training_data = localdata.train0
+    test_data = localdata.test0
+    model = svm_learn( training_data, type='classification' )
+    
+    # write_model(model, 'my_model.dat')
+
+    with open("model.pickle", 'wb') as f:
+        pickle.dump(model, f)
 # ----------------------------------------------
 
-'''
 
-static PyObject *svm_learn(PyObject *self, PyObject *args, PyObject *kwds)
-{
-    #DOC **docs;
-    double* target;
-    int totwords, totdoc;
-    KERNEL_CACHE *kernel_cache;
-    LEARN_PARM learn_parm;
-    KERNEL_PARM kernel_parm;
-    long verbosity;
-    PyObject *doclist;
-    MODEL *model;
-    MODEL_AND_DOCS *result;
-
-    if(!PyArg_ParseTuple(args, "O", &doclist))
-        return NULL;
-    read_learning_parameters(kwds, &verbosity, &learn_parm, &kernel_parm);
-    if(!unpack_doclist(doclist, &docs, &target, &totwords, &totdoc))
-        return NULL;
-
-    model = malloc(sizeof(MODEL));
-    if(kernel_parm.kernel_type == LINEAR)
-        kernel_cache = NULL;
-    else
-        kernel_cache = kernel_cache_init(totdoc, learn_parm.kernel_cache_size);
-
-    # DO THIS ONE!
-    if(learn_parm.type == CLASSIFICATION) {
-        svm_learn_classification(docs, target, totdoc, totwords, &learn_parm,
-                                 &kernel_parm, kernel_cache, model, NULL /* alpha_in */);
-    }
-    else if(learn_parm.type == REGRESSION) {
-        svm_learn_regression(docs, target, totdoc, totwords, &learn_parm,
-                             &kernel_parm, &kernel_cache, model);
-    }
-    else if(learn_parm.type == RANKING) {
-        svm_learn_ranking(docs, target, totdoc, totwords, &learn_parm,
-                          &kernel_parm, &kernel_cache, model);
-    }
-    else if(learn_parm.type == OPTIMIZATION) {
-        svm_learn_optimization(docs, target, totdoc, totwords, &learn_parm,
-                               &kernel_parm, kernel_cache, model, NULL /* alpha_in */);
-    }
-'''
-
-
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.