# 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)
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
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']
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)
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)")
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:
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
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
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()
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]
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
_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
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
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
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
-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)
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