Skip to content

Commit

Permalink
Compatibility fixes for old scipy.sparse.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Pedregosa committed Oct 4, 2010
1 parent 3baf985 commit ed1dd5c
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 38 deletions.
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ Dependencies
============

The required dependencies to build the software are python >= 2.5,
NumPy >= 1.1, SciPy and a working C++ compiler.
NumPy >= 1.1, SciPy >= 0.6 (although having at least 0.7 is highly
recommended and required by some modules) and a working C++ compiler.

To run the tests you will also need nosetests.
To run the tests you will also need nose >= 0.10.


Install
Expand Down
4 changes: 2 additions & 2 deletions scikits/learn/cluster/tests/test_spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

import numpy as np
from numpy.testing import assert_equal, assert_
from numpy.testing import assert_equal
from scipy import sparse

from .. import SpectralClustering
Expand Down Expand Up @@ -49,5 +49,5 @@ def test_spectral_clustering_sparse():
if labels[0] == 0:
labels = 1 - labels

assert_(np.mean(labels == [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]) > .9)
assert np.mean(labels == [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]) > .9

2 changes: 1 addition & 1 deletion scikits/learn/features/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def img_to_graph(img, mask=None,
graph = sparse.coo_matrix((np.hstack((weights, weights, img)),
(np.hstack((i_idx, diag_idx)),
np.hstack((j_idx, diag_idx)))),
shape=(n_voxels, n_voxels))
(n_voxels, n_voxels))
if return_as is np.ndarray:
return graph.todense()
return return_as(graph)
Expand Down
1 change: 1 addition & 0 deletions scikits/learn/features/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def test_sparse_tf_idf():


def test_dense_sparse_idf_sanity():

hv = HashingVectorizer(dim=100, probes=3)
shv = SparseHashingVectorizer(dim=100, probes=3)

Expand Down
2 changes: 2 additions & 0 deletions scikits/learn/features/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ class SparseHashingVectorizer(object):
The logic is the same as HashingVectorizer but it is possible to use much
larger dimension vectors without memory issues thanks to the usage of
scipy.sparse datastructure to store the tf vectors.
This function requires scipy 0.7 or higher.
"""

def __init__(self, dim=100000, probes=1, use_idf=True,
Expand Down
18 changes: 9 additions & 9 deletions scikits/learn/glm/sparse/tests/test_coordinate_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from numpy.testing import assert_array_almost_equal
from numpy.testing import assert_almost_equal
from numpy.testing import assert_equal
from numpy.testing import assert_

from scikits.learn.glm.sparse.coordinate_descent import Lasso as SparseLasso
from scikits.learn.glm.sparse.coordinate_descent import ElasticNet as SparseENet
Expand All @@ -27,8 +26,9 @@ def test_lasso_zero():
"""Check that the sparse lasso can handle zero data without crashing"""
X = sparse.csc_matrix((3, 1))
y = [0, 0, 0]
T = np.array([[1], [2], [3]])
clf = SparseLasso().fit(X, y)
pred = clf.predict([[1], [2], [3]])
pred = clf.predict(T)
assert_array_almost_equal(clf.coef_, [0])
assert_array_almost_equal(pred, [0, 0, 0])
assert_almost_equal(clf.dual_gap_, 0)
Expand All @@ -37,9 +37,9 @@ def test_lasso_zero():
def test_enet_toy_list_input():
"""Test ElasticNet for various parameters of alpha and rho with list X"""

X = [[-1], [0], [1]]
X = np.array([[-1], [0], [1]])
Y = [-1, 0, 1] # just a straight line
T = [[2], [3], [4]] # test sample
T = np.array([[2], [3], [4]]) # test sample

# this should be the same as unregularized least squares
clf = SparseENet(alpha=0, rho=1.0)
Expand Down Expand Up @@ -134,18 +134,18 @@ def test_sparse_enet_not_as_toy_dataset():
s_clf = SparseENet(alpha=0.1, rho=0.8, fit_intercept=False)
s_clf.fit(X_train, y_train, maxit=maxit, tol=1e-7)
assert_almost_equal(s_clf.dual_gap_, 0, 4)
assert_(s_clf.score(X_test, y_test) > 0.85)
assert s_clf.score(X_test, y_test) > 0.85

# check the convergence is the same as the dense version
d_clf = DenseENet(alpha=0.1, rho=0.8, fit_intercept=False)
d_clf.fit(X_train, y_train, maxit=maxit, tol=1e-7)
assert_almost_equal(d_clf.dual_gap_, 0, 4)
assert_(d_clf.score(X_test, y_test) > 0.85)
assert d_clf.score(X_test, y_test) > 0.85

assert_almost_equal(s_clf.coef_, d_clf.coef_, 5)

# check that the coefs are sparse
assert_(np.sum(s_clf.coef_ != 0.0) < 2 * n_informative)
assert np.sum(s_clf.coef_ != 0.0) < 2 * n_informative


def test_sparse_lasso_not_as_toy_dataset():
Expand All @@ -160,13 +160,13 @@ def test_sparse_lasso_not_as_toy_dataset():
s_clf = SparseLasso(alpha=0.1, fit_intercept=False)
s_clf.fit(X_train, y_train, maxit=maxit, tol=1e-7)
assert_almost_equal(s_clf.dual_gap_, 0, 4)
assert_(s_clf.score(X_test, y_test) > 0.85)
assert s_clf.score(X_test, y_test) > 0.85

# check the convergence is the same as the dense version
d_clf = DenseLasso(alpha=0.1, fit_intercept=False)
d_clf.fit(X_train, y_train, maxit=maxit, tol=1e-7)
assert_almost_equal(d_clf.dual_gap_, 0, 4)
assert_(d_clf.score(X_test, y_test) > 0.85)
assert d_clf.score(X_test, y_test) > 0.85

# check that the coefs are sparse
assert_equal(np.sum(s_clf.coef_ != 0.0), n_informative)
Expand Down
6 changes: 4 additions & 2 deletions scikits/learn/glm/tests/test_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
import numpy as np

from numpy.testing import assert_array_equal, \
assert_array_almost_equal, decorators
assert_array_almost_equal

import nose

from ..bayes import BayesianRidge, ARDRegression

from scikits.learn import datasets

@decorators.skipif(True, "XFailed test")
def test_bayesian_on_diabetes():
"""
Test BayesianRidge on diabetes
"""
raise nose.SkipTest("XFailed Test")
diabetes = datasets.load_diabetes()
X, y = diabetes.data, diabetes.target

Expand Down
6 changes: 3 additions & 3 deletions scikits/learn/glm/tests/test_coordinate_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


import numpy as np
from numpy.testing import assert_array_almost_equal, assert_almost_equal, assert_
from numpy.testing import assert_array_almost_equal, assert_almost_equal

from ..coordinate_descent import Lasso, LassoCV, ElasticNet, ElasticNetCV

Expand Down Expand Up @@ -113,7 +113,7 @@ def test_lasso_path():
# test set
X_test = np.random.randn(n_samples, n_features)
y_test = np.dot(X_test, w)
assert_(clf.score(X_test, y_test) > 0.85)
assert clf.score(X_test, y_test) > 0.85

def test_enet_path():

Expand All @@ -133,5 +133,5 @@ def test_enet_path():
# test set
X_test = np.random.randn(n_samples, n_features)
y_test = np.dot(X_test, w)
assert_(clf.score(X_test, y_test) > 0.85)
assert clf.score(X_test, y_test) > 0.85

14 changes: 8 additions & 6 deletions scikits/learn/svm/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
import scipy.sparse
from scikits.learn import datasets, svm
from numpy.testing import assert_array_almost_equal, \
assert_array_equal, assert_equal, assert_raises
assert_array_equal, assert_equal

from nose.tools import assert_raises

# test sample 1
X = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]
X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]])
Y = [1, 1, 1, 2, 2, 2]
T = [[-1, -1], [2, 2], [3, 2]]
T = np.array([[-1, -1], [2, 2], [3, 2]])
true_result = [1, 2, 2]

# test sample 2
X2 = [[0, 0, 0], [1, 1, 1], [2, 0, 0, ],
[0, 0, 2], [3, 3, 3]]
X2 = np.array([[0, 0, 0], [1, 1, 1], [2, 0, 0, ],
[0, 0, 2], [3, 3, 3]])
Y2 = [1, 2, 2, 2, 3]
T2 = [[-1, -1, -1], [1, 1, 1], [2, 2, 2]]
T2 = np.array([[-1, -1, -1], [1, 1, 1], [2, 2, 2]])
true_result2 = [1, 2, 3]


Expand Down
5 changes: 3 additions & 2 deletions scikits/learn/svm/tests/test_svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import numpy as np
from numpy.testing import assert_array_equal, assert_array_almost_equal, \
assert_almost_equal, assert_raises, assert_
assert_almost_equal
from nose.tools import assert_raises

from scikits.learn import svm, datasets

Expand Down Expand Up @@ -262,5 +263,5 @@ def test_LinearSVC_iris():
Test that LinearSVC gives plausible predictions on the iris dataset
"""
clf = svm.LinearSVC().fit(iris.data, iris.target)
assert_ (np.mean(clf.predict(iris.data) == iris.target) > 0.95)
assert np.mean(clf.predict(iris.data) == iris.target) > 0.95

9 changes: 5 additions & 4 deletions scikits/learn/tests/test_hmm.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from numpy.testing import (assert_array_equal, assert_array_almost_equal,
TestCase)
import numpy as np

from .. import hmm
import numpy as np

from numpy.testing import assert_array_equal, assert_array_almost_equal
from unittest import TestCase
from .test_gmm import _generate_random_spd_matrix

from .. import hmm

SKIP_FAILING = True # skip failing tests


Expand Down
11 changes: 7 additions & 4 deletions scikits/learn/tests/test_logistic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import numpy as np
from numpy.testing import assert_array_equal, assert_raises, \
assert_, assert_array_almost_equal, decorators

from numpy.testing import assert_array_equal, \
assert_array_almost_equal
import nose
from nose.tools import assert_raises

from .. import logistic, datasets

Expand Down Expand Up @@ -42,13 +45,13 @@ def test_predict_iris():
clf = logistic.LogisticRegression().fit(iris.data, iris.target)
pred = clf.predict(iris.data)

assert_ ( np.mean(pred == iris.target) > .95 )
assert np.mean(pred == iris.target) > .95

@decorators.skipif(True, "XFailed test")
def test_predict_proba():
"""
I think this test is wrong. Is there a way to know the right results ?
"""
raise nose.SkipTest("XFailed test")
clf = logistic.LogisticRegression().fit(X, Y2)
assert_array_almost_equal(clf.predict_proba([[1, 1]]),
[[ 0.21490268, 0.32639437, 0.45870294]])
Expand Down
6 changes: 3 additions & 3 deletions scikits/learn/tests/test_preprocessing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from numpy.testing import assert_array_almost_equal, assert_array_equal, assert_
from numpy.testing import assert_array_almost_equal, assert_array_equal

#from ..preprocessing import Scaler
from scikits.learn.preprocessing import Scaler, scale
Expand All @@ -15,13 +15,13 @@ def test_scaler():
assert_array_almost_equal(X_scaled.mean(axis=0), 5*[0.0])
assert_array_almost_equal(X_scaled.std(axis=0), 5*[1.0])
# Check that X has not been copied
assert_(X_scaled is X)
assert X_scaled is X

X_scaled = scaler.fit(X).transform(X, copy=True)
assert_array_almost_equal(X_scaled.mean(axis=0), 5*[0.0])
assert_array_almost_equal(X_scaled.std(axis=0), 5*[1.0])
# Check that X has not been copied
assert_(X_scaled is not X)
assert X_scaled is not X

X_scaled = scale(X, axis=1, with_std=False)
assert_array_almost_equal(X_scaled.mean(axis=1), 4*[0.0])
Expand Down

0 comments on commit ed1dd5c

Please sign in to comment.