]> granicus.if.org Git - liblinear/commitdiff
moved all numpy references through scipy
authorSinacam <stinkingmadgod@gmail.com>
Fri, 5 Aug 2022 11:53:04 +0000 (19:53 +0800)
committerSinacam <stinkingmadgod@gmail.com>
Sun, 7 Aug 2022 09:23:16 +0000 (17:23 +0800)
python/README
python/liblinear/commonutil.py
python/liblinear/liblinear.py
python/liblinear/liblinearutil.py

index 4c744e4582d438209fa01f9d9cc3f729fa2f5fa8..aa31a00a4af595c9bc0023fcc0a037cf3d5b6940 100644 (file)
@@ -145,9 +145,10 @@ in liblinearutil.py and the usage is the same as the LIBLINEAR MATLAB interface.
 
 # Construct problem in Scipy format
 # Dense data: numpy ndarray
->>> y, x = scipy.asarray([1,-1]), scipy.asarray([[1,0,1], [-1,0,-1]])
+>>> import numpy as np
+>>> y, x = np.asarray([1,-1]), np.asarray([[1,0,1], [-1,0,-1]])
 # Sparse data: scipy csr_matrix((data, (row_ind, col_ind))
->>> y, x = scipy.asarray([1,-1]), scipy.sparse.csr_matrix(([1, 1, -1, -1], ([0, 0, 1, 1], [0, 2, 0, 2])))
+>>> y, x = np.asarray([1,-1]), scipy.sparse.csr_matrix(([1, 1, -1, -1], ([0, 0, 1, 1], [0, 2, 0, 2])))
 >>> prob  = problem(y, x)
 >>> param = parameter('-s 0 -c 4 -B 1')
 >>> m = train(prob, param)
@@ -171,12 +172,12 @@ all arguments and return values are in ctypes format. You need to handle them
 carefully.
 
 >>> from liblinear.liblinear import *
->>> prob = problem(scipy.asarray([1,-1]), scipy.sparse.csr_matrix(([1, 1, -1, -1], ([0, 0, 1, 1], [0, 2, 0, 2]))))
+>>> prob = problem(np.asarray([1,-1]), scipy.sparse.csr_matrix(([1, 1, -1, -1], ([0, 0, 1, 1], [0, 2, 0, 2]))))
 >>> param = parameter('-c 4')
 >>> m = liblinear.train(prob, param) # m is a ctype pointer to a model
 # Convert a tuple of ndarray (index, data) to feature_nodearray, a ctypes structure
 # Note that index starts from 0, though the following example will be changed to 1:1, 3:1 internally
->>> x0, max_idx = gen_feature_nodearray((scipy.asarray([0,2]), scipy.asarray([1,1])))
+>>> x0, max_idx = gen_feature_nodearray((np.asarray([0,2]), np.asarray([1,1])))
 >>> label = liblinear.predict(m, x0)
 
 Design Description
index c261c42c41bdf484de867c3cdb19d793e309a63b..02e64d72aeb8193045f273cc730e56e89d65cb31 100644 (file)
@@ -5,11 +5,11 @@ from array import array
 import sys
 
 try:
+    import numpy as np
     import scipy
     from scipy import sparse
 except:
     scipy = None
-    sparse = None
 
 
 __all__ = ['svm_read_problem', 'evaluations', 'csr_find_scale_param', 'csr_scale']
@@ -58,10 +58,10 @@ def svm_read_problem(data_file_name, return_scipy=False):
                 xi[int(ind)] = float(val)
             prob_x += [xi]
     if scipy != None and return_scipy:
-        prob_y = scipy.frombuffer(prob_y, dtype='d')
-        prob_x = scipy.frombuffer(prob_x, dtype='d')
-        col_idx = scipy.frombuffer(col_idx, dtype='l')
-        row_ptr = scipy.frombuffer(row_ptr, dtype='l')
+        prob_y = np.frombuffer(prob_y, dtype='d')
+        prob_x = np.frombuffer(prob_x, dtype='d')
+        col_idx = np.frombuffer(col_idx, dtype='l')
+        row_ptr = np.frombuffer(row_ptr, dtype='l')
         prob_x = sparse.csr_matrix((prob_x, col_idx, row_ptr))
     return (prob_y, prob_x)
 
@@ -73,7 +73,7 @@ def evaluations_scipy(ty, pv):
     Calculate accuracy, mean squared error and squared correlation coefficient
     using the true values (ty) and predicted values (pv).
     """
-    if not (scipy != None and isinstance(ty, scipy.ndarray) and isinstance(pv, scipy.ndarray)):
+    if not (scipy != None and isinstance(ty, np.ndarray) and isinstance(pv, np.ndarray)):
         raise TypeError("type of ty and pv must be ndarray")
     if len(ty) != len(pv):
         raise ValueError("len(ty) must be equal to len(pv)")
@@ -85,7 +85,7 @@ def evaluations_scipy(ty, pv):
     sumvy = (pv*ty).sum()
     sumvv = (pv*pv).sum()
     sumyy = (ty*ty).sum()
-    with scipy.errstate(all = 'raise'):
+    with np.errstate(all = 'raise'):
         try:
             SCC = ((l*sumvy-sumv*sumy)*(l*sumvy-sumv*sumy))/((l*sumvv-sumv*sumv)*(l*sumyy-sumy*sumy))
         except:
@@ -102,7 +102,7 @@ def evaluations(ty, pv, useScipy = True):
     using the true values (ty) and predicted values (pv).
     """
     if scipy != None and useScipy:
-        return evaluations_scipy(scipy.asarray(ty), scipy.asarray(pv))
+        return evaluations_scipy(np.asarray(ty), np.asarray(pv))
     if len(ty) != len(pv):
         raise ValueError("len(ty) must be equal to len(pv)")
     total_correct = total_error = 0
index d2239c74d513998fded18b1a37b2d79badb4f0f2..5f577dff03e56c34c8554a8a5a131954f1d7ffa3 100644 (file)
@@ -7,11 +7,11 @@ from glob import glob
 import sys
 
 try:
+    import numpy as np
     import scipy
     from scipy import sparse
 except:
     scipy = None
-    sparse = None
 
 if sys.version_info[0] < 3:
     range = xrange
@@ -82,15 +82,15 @@ def gen_feature_nodearray(xi, feature_max=None):
 
     xi_shift = 0 # ensure correct indices of xi
     if scipy and isinstance(xi, tuple) and len(xi) == 2\
-            and isinstance(xi[0], scipy.ndarray) and isinstance(xi[1], scipy.ndarray): # for a sparse vector
+            and isinstance(xi[0], np.ndarray) and isinstance(xi[1], np.ndarray): # for a sparse vector
         index_range = xi[0] + 1 # index starts from 1
         if feature_max:
-            index_range = index_range[scipy.where(index_range <= feature_max)]
-    elif scipy and isinstance(xi, scipy.ndarray):
+            index_range = index_range[np.where(index_range <= feature_max)]
+    elif scipy and isinstance(xi, np.ndarray):
         xi_shift = 1
         index_range = xi.nonzero()[0] + 1 # index starts from 1
         if feature_max:
-            index_range = index_range[scipy.where(index_range <= feature_max)]
+            index_range = index_range[np.where(index_range <= feature_max)]
     elif isinstance(xi, (dict, list, tuple)):
         if isinstance(xi, dict):
             index_range = xi.keys()
@@ -110,7 +110,7 @@ def gen_feature_nodearray(xi, feature_max=None):
     ret[-2].index = -1
 
     if scipy and isinstance(xi, tuple) and len(xi) == 2\
-            and isinstance(xi[0], scipy.ndarray) and isinstance(xi[1], scipy.ndarray): # for a sparse vector
+            and isinstance(xi[0], np.ndarray) and isinstance(xi[1], np.ndarray): # for a sparse vector
         for idx, j in enumerate(index_range):
             ret[idx].index = j
             ret[idx].value = (xi[1])[idx]
@@ -148,9 +148,9 @@ def csr_to_problem_nojit(l, x_val, x_ind, x_rowptr, prob_val, prob_ind, prob_row
 
 def csr_to_problem(x, prob):
     # Extra space for termination node and (possibly) bias term
-    x_space = prob.x_space = scipy.empty((x.nnz+x.shape[0]*2), dtype=feature_node)
+    x_space = prob.x_space = np.empty((x.nnz+x.shape[0]*2), dtype=feature_node)
     prob.rowptr = x.indptr.copy()
-    prob.rowptr[1:] += 2*scipy.arange(1,x.shape[0]+1)
+    prob.rowptr[1:] += 2*np.arange(1,x.shape[0]+1)
     prob_ind = x_space["index"]
     prob_val = x_space["value"]
     prob_ind[:] = -1
@@ -165,17 +165,17 @@ class problem(Structure):
     _fields_ = genFields(_names, _types)
 
     def __init__(self, y, x, bias = -1):
-        if (not isinstance(y, (list, tuple))) and (not (scipy and isinstance(y, scipy.ndarray))):
+        if (not isinstance(y, (list, tuple))) and (not (scipy and isinstance(y, np.ndarray))):
             raise TypeError("type of y: {0} is not supported!".format(type(y)))
 
         if isinstance(x, (list, tuple)):
             if len(y) != len(x):
                 raise ValueError("len(y) != len(x)")
-        elif scipy != None and isinstance(x, (scipy.ndarray, sparse.spmatrix)):
+        elif scipy != None and isinstance(x, (np.ndarray, sparse.spmatrix)):
             if len(y) != x.shape[0]:
                 raise ValueError("len(y) != len(x)")
-            if isinstance(x, scipy.ndarray):
-                x = scipy.ascontiguousarray(x) # enforce row-major
+            if isinstance(x, np.ndarray):
+                x = np.ascontiguousarray(x) # enforce row-major
             if isinstance(x, sparse.spmatrix):
                 x = x.tocsr()
                 pass
@@ -197,8 +197,8 @@ class problem(Structure):
         self.n = max_idx
 
         self.y = (c_double * l)()
-        if scipy != None and isinstance(y, scipy.ndarray):
-            scipy.ctypeslib.as_array(self.y, (self.l,))[:] = y
+        if scipy != None and isinstance(y, np.ndarray):
+            np.ctypeslib.as_array(self.y, (self.l,))[:] = y
         else:
             for i, yi in enumerate(y): self.y[i] = yi
 
@@ -206,7 +206,7 @@ class problem(Structure):
         if scipy != None and isinstance(x, sparse.csr_matrix):
             base = addressof(self.x_space.ctypes.data_as(POINTER(feature_node))[0])
             x_ptr = cast(self.x, POINTER(c_uint64))
-            x_ptr = scipy.ctypeslib.as_array(x_ptr,(self.l,))
+            x_ptr = np.ctypeslib.as_array(x_ptr,(self.l,))
             x_ptr[:] = self.rowptr[:-1]*sizeof(feature_node)+base
         else:
             for i, xi in enumerate(self.x_space): self.x[i] = xi
index 6d201aed7d4ad950258af0c0955296cc6a56a99a..9d757ecf9d049fb107d297404e77d31ffa258ef3 100644 (file)
@@ -3,11 +3,17 @@
 import os, sys
 from .liblinear import *
 from .liblinear import __all__ as liblinear_all
-from .liblinear import scipy, sparse
 from .commonutil import *
 from .commonutil import __all__ as common_all
 from ctypes import c_double
 
+try:
+    import numpy as np
+    import scipy
+    from scipy import sparse
+except:
+    scipy = None
+
 if sys.version_info[0] < 3:
     range = xrange
     from itertools import izip as zip
@@ -101,8 +107,8 @@ def train(arg1, arg2=None, arg3=None):
         -q : quiet mode (no outputs)
     """
     prob, param = None, None
-    if isinstance(arg1, (list, tuple)) or (scipy and isinstance(arg1, scipy.ndarray)):
-        assert isinstance(arg2, (list, tuple)) or (scipy and isinstance(arg2, (scipy.ndarray, sparse.spmatrix)))
+    if isinstance(arg1, (list, tuple)) or (scipy and isinstance(arg1, np.ndarray)):
+        assert isinstance(arg2, (list, tuple)) or (scipy and isinstance(arg2, (np.ndarray, sparse.spmatrix)))
         y, x, options = arg1, arg2, arg3
         prob = problem(y, x)
         param = parameter(options)
@@ -195,14 +201,14 @@ def predict(y, x, m, options=""):
     def info(s):
         print(s)
 
-    if scipy and isinstance(x, scipy.ndarray):
-        x = scipy.ascontiguousarray(x) # enforce row-major
+    if scipy and isinstance(x, np.ndarray):
+        x = np.ascontiguousarray(x) # enforce row-major
     elif sparse and isinstance(x, sparse.spmatrix):
         x = x.tocsr()
     elif not isinstance(x, (list, tuple)):
         raise TypeError("type of x: {0} is not supported!".format(type(x)))
 
-    if (not isinstance(y, (list, tuple))) and (not (scipy and isinstance(y, scipy.ndarray))):
+    if (not isinstance(y, (list, tuple))) and (not (scipy and isinstance(y, np.ndarray))):
         raise TypeError("type of y: {0} is not supported!".format(type(y)))
 
     predict_probability = 0