Ceri Stagg avatar Ceri Stagg committed bba164d

Add read_model method

Comments (0)

Files changed (1)

svmlight/svmlight.py

 MAXSHRINK = 50000
 svm = CDLL("./svmlight.so")
 
-svm.sprod_ss.restype = c_double
-
 class WORD(Structure):
     _fields_ = [("wnum",   FNUM),
                 ("weight", FVAL)]
                 ("last_lin",        POINTER(c_double))]
 # ----------------------------------------------
 
+# specify return types for key methods in the svm C library
+svm.sprod_ss.restype = c_double
+svm.read_model.restype = POINTER( MODEL )
+# ----------------------------------------------
+
 ''' This auxiliary function to svm_learn reads some parameters from the keywords to
  * the function and fills the rest in with defaults (from read_input_parameters()
  * in svm_learn_main.c:109).
     svm.write_model( filename, pointer( model ) )
 # ----------------------------------------------
 
+def read_model( filename ):
+    filename_as_c_string = generate_C_string_from_python( filename )
+    pmodel = svm.read_model( filename )
+    return pmodel.contents
+# ----------------------------------------------
+
 # -------------------- MAIN --------------------
 if __name__ == "__main__":
     training_data = localdata.train0
     test_data = localdata.test0
     learn_results_tuple = svm_learn( training_data, type='classification' )
-    
+
+    print( "Begin read model" )
+    temp_model = read_model('my_python_model.dat')
+    print( "End read model" )
+
     print( "Begin write model" )
-    write_model( learn_results_tuple.model, 'my_python_model.dat')
+    write_model( temp_model, 'my_python_model_duplicate.dat')
     print( "End write model" )
 
     with open("model.pickle", 'wb') as f:
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.