
import numpy as np
import scipy.sparse as sp
from numpy.testing import assert_almost_equal, assert_array_almost_equal

from scikits.learn import datasets
from scikits.learn.linear_model.ridge import Ridge
from scikits.learn.linear_model.sparse.ridge import Ridge as SpRidge
from scikits.learn.linear_model.sparse.ridge import RidgeClassifier
from scikits.learn.linear_model.sparse.ridge import RidgeClassifierLOO

diabetes = datasets.load_diabetes()
iris = datasets.load_iris()

def test_ridge_diabetes():
    X, y = diabetes.data, diabetes.target

    #for fi in (True, False):
    for fi in (False,):
        ridge = Ridge(fit_intercept=fi)
        ridge.fit(X, y)
        for solver in ("cg", "default"):
            ridge_sp = SpRidge(fit_intercept=fi, solver=solver)
            ridge_sp.fit(X, y)
            assert_almost_equal(ridge.score(X, y), ridge_sp.score(X, y))

def test_multi_ridge_diabetes():
    X, y = diabetes.data, diabetes.target

    # simulate several responses
    Y = np.vstack((y,y)).T

    ridge = SpRidge(fit_intercept=False)
    ridge.fit(X, Y)
    Y_pred = ridge.predict(X)
    ridge.fit(X, y)
    y_pred = ridge.predict(X)
    assert_array_almost_equal(np.vstack((y_pred,y_pred)).T,
                              Y_pred)

def test_ridge_classifiers():
    for clf in (RidgeClassifier(), RidgeClassifierLOO()):
        clf.fit(iris.data, iris.target)
        y_pred = clf.predict(iris.data)
        assert np.mean(iris.target == y_pred) >= 0.8


@@ -477,77 +477,161 @@
 ()%0A%0A
-def test_ridge_diabetes():%0A    X, y = diabetes.data, diabetes.target%0A
+X_diabetes = sp.csr_matrix(diabetes.data)%0AX_iris = sp.csr_matrix(iris.data)%0Ay_diabetes = diabetes.target%0Ay_iris = iris.target%0A%0Adef test_ridge_diabetes():
 %0A   
@@ -735,28 +735,49 @@
   ridge.fit(
-X, y
+diabetes.data, y_diabetes
 )%0A        fo
@@ -893,27 +893,45 @@
 dge_sp.fit(X
-, y
+_diabetes, y_diabetes
 )%0A          
@@ -968,113 +968,141 @@
 ore(
-X, y), ridge_sp.score(X, y))%0A%0Adef test_multi_ridge_diabetes():%0A    X, y = diabetes.data, diabetes.target%0A
+diabetes.data, y_diabetes),%0A                                ridge_sp.score(X_diabetes, y_diabetes))%0A%0Adef test_multi_ridge_diabetes():
 %0A   
@@ -1151,18 +1151,36 @@
 stack((y
-,y
+_diabetes,y_diabetes
 )).T%0A%0A  
@@ -1233,16 +1233,25 @@
 ge.fit(X
+_diabetes
 , Y)%0A   
@@ -1271,24 +1271,33 @@
 ge.predict(X
+_diabetes
 )%0A    ridge.
@@ -1301,19 +1301,37 @@
 ge.fit(X
-, y
+_diabetes, y_diabetes
 )%0A    y_
@@ -1352,16 +1352,25 @@
 redict(X
+_diabetes
 )%0A    as
@@ -1570,30 +1570,22 @@
 fit(
+X_
 iris
-.data, iris.target
+, y_iris
 )%0A  
@@ -1611,25 +1611,22 @@
 predict(
+X_
 iris
-.data
 )%0A      
@@ -1642,27 +1642,22 @@
 np.mean(
+y_
 iris
-.target
  == y_pr

Fix tests for sparse case.