8000 Bug in sparse in Ridge with sample weights · Issue #15438 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

8000
Skip to content
Bug in sparse in Ridge with sample weights #15438
Closed
@lorentzenchr

Description

@lorentzenchr

Description

Ridge with sample weights gives wrong results for sparse input.

Steps/Code to Reproduce

import numpy as np
from numpy.testing import assert_array_almost_equal
from pytest import approx
import scipy.sparse as sparse
from sklearn.linear_model import Ridge

rng = np.random.RandomState(123)
n_samples, n_features = 10, 3
X = rng.rand(n_samples, n_features)
y = rng.rand(n_samples)

params = dict(alpha=0.05, fit_intercept=True)

# 1. Reference model: dense and sample_weight=None
reg = Ridge(**params, solver='svd').fit(X, y)
coef, intercept = reg.coef_.copy(), reg.intercept_

# 2. sample_weight = 2 * np.ones(..), but alpha / 2
sw = 2 * np.ones_like(y)
params['alpha'] = 2 * params['alpha']
reg = Ridge(**params, solver='svd').fit(X, y, sample_weight=sw)
assert_array_almost_equal(reg.coef_, coef)
assert reg.intercept_ == approx(intercept)

# 3. X sparse, sample_weight = 2 * np.ones(..), but alpha / 2=> ERROR
X = sparse.csr_matrix(X)
reg = Ridge(**params, solver='sparse_cg').fit(X, y, sample_weight=sw)
print('intercept true = {}, intercept sparse = {}'.format(intercept, reg.intercept_))
print('coef true = {}, coef sparse = {}'.format(coef, reg.coef_))
assert_array_almost_equal(reg.coef_, coef)
assert reg.intercept_ == approx(intercept)

Expected Results

No AssertionError is thrown.

Actual Results

Last two assertion statements throw AssertionError.

Versions

System:
python: 3.7.2
sklearn: 0.22.dev0
commit 9caf835
Author: Thomas J Fan thomasjpfan@gmail.com
Date: Sun Oct 27 03:32:45 2019 -0400

DOC Fix broken link to docs (#15364)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0