with pytest.raises(ValueError, match="must be 1D array or scalar"): glm.fit(X, y, weights) # 1d but wrong length weights = [1, 0] msg = r"sample_weight.shape == \(2,\), expected \(1,\)!" with pytest.raises(ValueError, match=msg): glm.fit(X, y, weights) @pytest.mark.parametrize( "name, instance", [ ("normal", NormalDistribution()), ("poisson", PoissonDistribution()), ("gamma", GammaDistribution()), ("inverse-gaussian", InverseGaussianDistribution()), ], ) def test_glm_family_argument(name, instance): """Test GLM family argument set as string.""" y = np.array([0.1, 0.5]) # in range of all distributions X = np.array([[1], [2]]) glm = GeneralizedLinearRegressor(family=name, alpha=0).fit(X, y) assert isinstance(glm._family_instance, instance.__class__) glm = GeneralizedLinearRegressor(family="not a family") with pytest.raises(ValueError, match="family must be"): glm.fit(X, y)
TweedieDistribution, NormalDistribution, PoissonDistribution, GammaDistribution, InverseGaussianDistribution, DistributionBoundary, ) @pytest.mark.parametrize( "family, expected", [ (NormalDistribution(), [True, True, True]), (PoissonDistribution(), [False, True, True]), (TweedieDistribution(power=1.5), [False, True, True]), (GammaDistribution(), [False, False, True]), (InverseGaussianDistribution(), [False, False, True]), (TweedieDistribution(power=4.5), [False, False, True]), ], ) def test_family_bounds(family, expected): """Test the valid range of distributions at -1, 0, 1.""" result = family.in_y_range([-1, 0, 1]) assert_array_equal(result, expected) def test_invalid_distribution_bound(): dist = TweedieDistribution() dist._lower_bound = 0 with pytest.raises(TypeError, match="must be of type DistributionBoundary"):
# 2d array weights = [[0]] with pytest.raises(ValueError, match="must be 1D array or scalar"): glm.fit(X, y, weights) # 1d but wrong length weights = [1, 0] msg = r"sample_weight.shape == \(2,\), expected \(1,\)!" with pytest.raises(ValueError, match=msg): glm.fit(X, y, weights) @pytest.mark.parametrize('name, instance', [('normal', NormalDistribution()), ('poisson', PoissonDistribution()), ('gamma', GammaDistribution()), ('inverse-gaussian', InverseGaussianDistribution())]) def test_glm_family_argument(name, instance): """Test GLM family argument set as string.""" y = np.array([0.1, 0.5]) # in range of all distributions X = np.array([[1], [2]]) glm = GeneralizedLinearRegressor(family=name, alpha=0).fit(X, y) assert isinstance(glm._family_instance, instance.__class__) glm = GeneralizedLinearRegressor(family='not a family') with pytest.raises(ValueError, match="family must be"): glm.fit(X, y) @pytest.mark.parametrize('name, instance', [('identity', IdentityLink()), ('log', LogLink())])