code: def test_linear_regression_sample_weights(setup):
      # TODO: loop over sparse data as well
  
      rng = np.random.RandomState(0)
  
      # It would not work with under-determined systems
      for n_samples, n_features in ((6, 5),):
          y = rng.randn(n_samples)
          X = rng.randn(n_samples, n_features)
          sample_weight = 1.0 + rng.rand(n_samples)
  
          for intercept in (True, False):
              # LinearRegression with explicit sample_weight
              reg = LinearRegression(fit_intercept=intercept)
              reg.fit(X, y, sample_weight=sample_weight)
              coefs1 = reg.coef_
              inter1 = reg.intercept_
  
              assert reg.coef_.shape == (X.shape[1],)  # sanity checks
              assert reg.score(X, y).to_numpy() > 0.5
  
              # Closed form of the weighted least square
              # theta = (X^T W X)^(-1) * X^T W y
              W = np.diag(sample_weight)
              if intercept is False:
                  X_aug = X
              else:
                  dummy_column = np.ones(shape=(n_samples, 1))
                  X_aug = np.concatenate((dummy_column, X), axis=1)
  
              coefs2 = linalg.solve(
                  X_aug.T.dot(W).dot(X_aug), X_aug.T.dot(W).dot(y)
              )
  
              if intercept is False:
                  assert_array_almost_equal(coefs1, coefs2)
              else:
                  assert_array_almost_equal(coefs1, coefs2[1:])
                  assert_almost_equal(inter1.to_numpy(), coefs2[0])
  
imports: import re
  import numpy
  import pytest
  from numpy.testing import assert_array_almost_equal, assert_array_equal, assert_almost_equal, assert_allclose
  from scipy import sparse
  from scipy import linalg
  from sklearn.datasets import make_sparse_uncorrelated, make_regression, load_iris
  from sklearn.linear_model import LinearRegression
  from sklearn.linear_model._base import make_dataset
  from sklearn.utils import check_random_state
  from ..None import LinearRegression
  from .._base import _preprocess_data, _rescale_data
label_desc: Use image node_id 2 for calling a global function with example usage: test_linear_regression_sample_weights(setup) without return types