Skip to content

Commit a8f3977

Browse files
committed
Merge branch 'master' of github.com:scikit-learn/scikit-learn
2 parents d53bb58 + 5a6d911 commit a8f3977

File tree

8 files changed

+587
-460
lines changed

8 files changed

+587
-460
lines changed

scikits/learn/linear_model/ridge.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
# License: Simplified BSD
77

88
import numpy as np
9-
import scipy.sparse as sp
10-
from scipy import linalg
11-
from scipy.sparse import linalg as sp_linalg
129

1310
from .base import LinearModel
1411
from ..utils.extmath import safe_sparse_dot
@@ -85,6 +82,7 @@ def fit(self, X, y, sample_weight=1.0, solver="default", **params):
8582
X, y, Xmean, ymean = \
8683
LinearModel._center_data(X, y, self.fit_intercept)
8784

85+
import scipy.sparse as sp
8886
if sp.issparse(X):
8987
self._solve_sparse(X, y, sample_weight)
9088
else:
@@ -116,6 +114,7 @@ def _solve_dense(self, X, y, sample_weight):
116114
def _solve_sparse(self, X, y, sample_weight):
117115
n_samples, n_features = X.shape
118116

117+
import scipy.sparse as sp
119118
if n_features > n_samples or \
120119
isinstance(sample_weight, np.ndarray) or \
121120
sample_weight != 1.0:
@@ -132,14 +131,17 @@ def _solve_sparse(self, X, y, sample_weight):
132131
def _solve(self, A, b):
133132
if self.solver == "cg":
134133
# this solver cannot handle a 2-d b.
134+
from scipy.sparse import linalg as sp_linalg
135135
sol, error = sp_linalg.cg(A, b)
136136
if error:
137137
raise ValueError("Failed with error code %d" % error)
138138
return sol
139139
else:
140+
import scipy.sparse as sp
140141
# we are working with dense symmetric positive A
141142
if sp.issparse(A):
142143
A = A.todense()
144+
from scipy import linalg
143145
return linalg.solve(A, b, sym_pos=True, overwrite_a=True)
144146

145147

@@ -255,6 +257,7 @@ def __init__(self, alphas=[0.1, 1.0, 10.0], fit_intercept=True,
255257
def _pre_compute(self, X, y):
256258
# even if X is very sparse, K is usually very dense
257259
K = safe_sparse_dot(X, X.T, dense_output=True)
260+
from scipy import linalg
258261
v, Q = linalg.eigh(K)
259262
return K, v, Q
260263

scikits/learn/linear_model/tests/test_least_angle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import numpy as np
2-
from numpy.testing import assert_, assert_array_almost_equal
2+
from numpy.testing import assert_array_almost_equal
33

44
from scikits.learn import linear_model, datasets
55

@@ -84,7 +84,7 @@ def test_collinearity():
8484
y = np.array([1., 0., 0])
8585

8686
_, _, coef_path_ = linear_model.lars_path(X, y)
87-
assert_(not np.isnan(coef_path_).any())
87+
assert (not np.isnan(coef_path_).any())
8888
assert_array_almost_equal(np.dot(X, coef_path_[:,-1]), y)
8989

9090

scikits/learn/svm/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ def predict_proba(self, T):
221221
"probability estimates must be enabled to use this method")
222222
T = np.atleast_2d(np.asanyarray(T, dtype=np.float64, order='C'))
223223
kernel_type, T = self._get_kernel(T)
224+
if self.impl not in ('c_svc', 'nu_svc'):
225+
raise NotImplementedError
224226

225227
pprob = libsvm_predict_proba(T, self.support_vectors_,
226228
self.dual_coef_, self.intercept_,

scikits/learn/svm/src/libsvm/_libsvm.c

Lines changed: 551 additions & 426 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scikits/learn/svm/src/libsvm/_libsvm.pyx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,14 @@ def libsvm_train (np.ndarray[np.float64_t, ndim=2, mode='c'] X,
229229
cdef np.ndarray[np.float64_t, ndim=1, mode='c'] probA
230230
cdef np.ndarray[np.float64_t, ndim=1, mode='c'] probB
231231
if probability != 0:
232-
# this is only valid for SVC
233-
probA = np.empty(n_class*(n_class-1)/2, dtype=np.float64)
234-
probB = np.empty(n_class*(n_class-1)/2, dtype=np.float64)
232+
if svm_type < 2: # SVC and NuSVC
233+
probA = np.empty(n_class*(n_class-1)/2, dtype=np.float64)
234+
probB = np.empty(n_class*(n_class-1)/2, dtype=np.float64)
235+
copy_probB(probB.data, model, probB.shape)
236+
else:
237+
probA = np.empty(1, dtype=np.float64)
238+
probB = np.empty(0, dtype=np.float64)
235239
copy_probA(probA.data, model, probA.shape)
236-
copy_probB(probB.data, model, probB.shape)
237240

238241
# memory deallocation
239242
svm_free_and_destroy_model(&model)

scikits/learn/svm/src/libsvm/_libsvm_sparse.pyx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,13 @@ def libsvm_sparse_train ( int n_features,
170170
cdef np.ndarray[np.float64_t, ndim=1, mode='c'] probA
171171
cdef np.ndarray[np.float64_t, ndim=1, mode='c'] probB
172172
if probability != 0:
173-
# this is only valid for SVC
174-
probA = np.empty(nr*(nr-1)/2, dtype=np.float64)
175-
probB = np.empty(nr*(nr-1)/2, dtype=np.float64)
176-
copy_probA(probA.data, model, probA.shape)
177-
copy_probB(probB.data, model, probB.shape)
173+
if svm_type < 2: # SVC and NuSVC
174+
probA = np.empty(n_class*(n_class-1)/2, dtype=np.float64)
175+
probB = np.empty(n_class*(n_class-1)/2, dtype=np.float64)
176+
copy_probB(probB.data, model, probB.shape)
177+
else:
178+
probA = np.empty(1, dtype=np.float64)
179+
probB = np.empty(0, dtype=np.float64)
178180

179181
svm_csr_free_and_destroy_model (&model)
180182
free_problem(problem)

scikits/learn/svm/tests/test_svm.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,14 @@ def test_probability():
212212
clf = svm.SVC(probability=True)
213213
clf.fit(iris.data, iris.target)
214214

215-
# predict on a simple dataset
216-
T = [[0, 0, 0, 0],
217-
[2, 2, 2, 2]]
218-
assert_array_almost_equal(clf.predict_proba(T),
219-
[[0.993, 0.003, 0.002],
220-
[0.740, 0.223, 0.035]],
221-
decimal=2)
222-
223-
assert_almost_equal(clf.predict_proba(T),
224-
np.exp(clf.predict_log_proba(T)), 8)
225-
226-
# make sure probabilities sum to one
227-
pprob = clf.predict_proba(X)
228-
assert_array_almost_equal(pprob.sum(axis=1),
229-
np.ones(len(X)))
215+
prob_predict = clf.predict_proba(iris.data)
216+
assert_array_almost_equal(
217+
np.sum(prob_predict, 1), np.ones(iris.data.shape[0]))
218+
assert np.mean(np.argmax(prob_predict, 1)
219+
== clf.predict(iris.data)) > 0.95
220+
221+
assert_almost_equal(clf.predict_proba(iris.data),
222+
np.exp(clf.predict_log_proba(iris.data)), 8)
230223

231224

232225
def test_decision_function():

scikits/learn/tests/test_neighbors.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import numpy as np
2-
from numpy.testing import assert_array_almost_equal, assert_array_equal, \
3-
assert_
2+
from numpy.testing import assert_array_almost_equal, assert_array_equal
43

54
from scikits.learn import neighbors, datasets
65

@@ -58,13 +57,13 @@ def test_neighbors_iris():
5857
assert_array_equal(clf.predict(iris.data), iris.target)
5958

6059
clf.fit(iris.data, iris.target, n_neighbors=9, algorithm=s)
61-
assert_(np.mean(clf.predict(iris.data)== iris.target) > 0.95)
60+
assert np.mean(clf.predict(iris.data)== iris.target) > 0.95
6261

6362
for m in ('barycenter', 'mean'):
6463
rgs = neighbors.NeighborsRegressor()
6564
rgs.fit(iris.data, iris.target, mode=m, algorithm=s)
66-
assert_(np.mean(
67-
rgs.predict(iris.data).round() == iris.target) > 0.95)
65+
assert np.mean(
66+
rgs.predict(iris.data).round() == iris.target) > 0.95
6867

6968

7069
def test_kneighbors_graph():

0 commit comments

Comments
 (0)