File size: 42,602 Bytes
7885a28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 |
import sys
import numpy as np
import pytest
from numpy.testing import assert_allclose, assert_array_equal
from scipy import sparse
from scipy.interpolate import BSpline
from scipy.sparse import random as sparse_random
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import (
KBinsDiscretizer,
PolynomialFeatures,
SplineTransformer,
)
from sklearn.preprocessing._csr_polynomial_expansion import (
_calc_expanded_nnz,
_calc_total_nnz,
_get_sizeof_LARGEST_INT_t,
)
from sklearn.utils._testing import assert_array_almost_equal
from sklearn.utils.fixes import (
CSC_CONTAINERS,
CSR_CONTAINERS,
parse_version,
sp_version,
)
@pytest.mark.parametrize("est", (PolynomialFeatures, SplineTransformer))
def test_polynomial_and_spline_array_order(est):
"""Test that output array has the given order."""
X = np.arange(10).reshape(5, 2)
def is_c_contiguous(a):
return np.isfortran(a.T)
assert is_c_contiguous(est().fit_transform(X))
assert is_c_contiguous(est(order="C").fit_transform(X))
assert np.isfortran(est(order="F").fit_transform(X))
@pytest.mark.parametrize(
"params, err_msg",
[
({"knots": [[1]]}, r"Number of knots, knots.shape\[0\], must be >= 2."),
({"knots": [[1, 1], [2, 2]]}, r"knots.shape\[1\] == n_features is violated"),
({"knots": [[1], [0]]}, "knots must be sorted without duplicates."),
],
)
def test_spline_transformer_input_validation(params, err_msg):
"""Test that we raise errors for invalid input in SplineTransformer."""
X = [[1], [2]]
with pytest.raises(ValueError, match=err_msg):
SplineTransformer(**params).fit(X)
@pytest.mark.parametrize("extrapolation", ["continue", "periodic"])
def test_spline_transformer_integer_knots(extrapolation):
"""Test that SplineTransformer accepts integer value knot positions."""
X = np.arange(20).reshape(10, 2)
knots = [[0, 1], [1, 2], [5, 5], [11, 10], [12, 11]]
_ = SplineTransformer(
degree=3, knots=knots, extrapolation=extrapolation
).fit_transform(X)
def test_spline_transformer_feature_names():
"""Test that SplineTransformer generates correct features name."""
X = np.arange(20).reshape(10, 2)
splt = SplineTransformer(n_knots=3, degree=3, include_bias=True).fit(X)
feature_names = splt.get_feature_names_out()
assert_array_equal(
feature_names,
[
"x0_sp_0",
"x0_sp_1",
"x0_sp_2",
"x0_sp_3",
"x0_sp_4",
"x1_sp_0",
"x1_sp_1",
"x1_sp_2",
"x1_sp_3",
"x1_sp_4",
],
)
splt = SplineTransformer(n_knots=3, degree=3, include_bias=False).fit(X)
feature_names = splt.get_feature_names_out(["a", "b"])
assert_array_equal(
feature_names,
[
"a_sp_0",
"a_sp_1",
"a_sp_2",
"a_sp_3",
"b_sp_0",
"b_sp_1",
"b_sp_2",
"b_sp_3",
],
)
@pytest.mark.parametrize(
"extrapolation",
["constant", "linear", "continue", "periodic"],
)
@pytest.mark.parametrize("degree", [2, 3])
def test_split_transform_feature_names_extrapolation_degree(extrapolation, degree):
"""Test feature names are correct for different extrapolations and degree.
Non-regression test for gh-25292.
"""
X = np.arange(20).reshape(10, 2)
splt = SplineTransformer(degree=degree, extrapolation=extrapolation).fit(X)
feature_names = splt.get_feature_names_out(["a", "b"])
assert len(feature_names) == splt.n_features_out_
X_trans = splt.transform(X)
assert X_trans.shape[1] == len(feature_names)
@pytest.mark.parametrize("degree", range(1, 5))
@pytest.mark.parametrize("n_knots", range(3, 5))
@pytest.mark.parametrize("knots", ["uniform", "quantile"])
@pytest.mark.parametrize("extrapolation", ["constant", "periodic"])
def test_spline_transformer_unity_decomposition(degree, n_knots, knots, extrapolation):
"""Test that B-splines are indeed a decomposition of unity.
Splines basis functions must sum up to 1 per row, if we stay in between boundaries.
"""
X = np.linspace(0, 1, 100)[:, None]
# make the boundaries 0 and 1 part of X_train, for sure.
X_train = np.r_[[[0]], X[::2, :], [[1]]]
X_test = X[1::2, :]
if extrapolation == "periodic":
n_knots = n_knots + degree # periodic splines require degree < n_knots
splt = SplineTransformer(
n_knots=n_knots,
degree=degree,
knots=knots,
include_bias=True,
extrapolation=extrapolation,
)
splt.fit(X_train)
for X in [X_train, X_test]:
assert_allclose(np.sum(splt.transform(X), axis=1), 1)
@pytest.mark.parametrize(["bias", "intercept"], [(True, False), (False, True)])
def test_spline_transformer_linear_regression(bias, intercept):
"""Test that B-splines fit a sinusodial curve pretty well."""
X = np.linspace(0, 10, 100)[:, None]
y = np.sin(X[:, 0]) + 2 # +2 to avoid the value 0 in assert_allclose
pipe = Pipeline(
steps=[
(
"spline",
SplineTransformer(
n_knots=15,
degree=3,
include_bias=bias,
extrapolation="constant",
),
),
("ols", LinearRegression(fit_intercept=intercept)),
]
)
pipe.fit(X, y)
assert_allclose(pipe.predict(X), y, rtol=1e-3)
@pytest.mark.parametrize(
["knots", "n_knots", "sample_weight", "expected_knots"],
[
("uniform", 3, None, np.array([[0, 2], [3, 8], [6, 14]])),
(
"uniform",
3,
np.array([0, 0, 1, 1, 0, 3, 1]),
np.array([[2, 2], [4, 8], [6, 14]]),
),
("uniform", 4, None, np.array([[0, 2], [2, 6], [4, 10], [6, 14]])),
("quantile", 3, None, np.array([[0, 2], [3, 3], [6, 14]])),
(
"quantile",
3,
np.array([0, 0, 1, 1, 0, 3, 1]),
np.array([[2, 2], [5, 8], [6, 14]]),
),
],
)
def test_spline_transformer_get_base_knot_positions(
knots, n_knots, sample_weight, expected_knots
):
"""Check the behaviour to find knot positions with and without sample_weight."""
X = np.array([[0, 2], [0, 2], [2, 2], [3, 3], [4, 6], [5, 8], [6, 14]])
base_knots = SplineTransformer._get_base_knot_positions(
X=X, knots=knots, n_knots=n_knots, sample_weight=sample_weight
)
assert_allclose(base_knots, expected_knots)
@pytest.mark.parametrize(["bias", "intercept"], [(True, False), (False, True)])
def test_spline_transformer_periodic_linear_regression(bias, intercept):
"""Test that B-splines fit a periodic curve pretty well."""
# "+ 3" to avoid the value 0 in assert_allclose
def f(x):
return np.sin(2 * np.pi * x) - np.sin(8 * np.pi * x) + 3
X = np.linspace(0, 1, 101)[:, None]
pipe = Pipeline(
steps=[
(
"spline",
SplineTransformer(
n_knots=20,
degree=3,
include_bias=bias,
extrapolation="periodic",
),
),
("ols", LinearRegression(fit_intercept=intercept)),
]
)
pipe.fit(X, f(X[:, 0]))
# Generate larger array to check periodic extrapolation
X_ = np.linspace(-1, 2, 301)[:, None]
predictions = pipe.predict(X_)
assert_allclose(predictions, f(X_[:, 0]), atol=0.01, rtol=0.01)
assert_allclose(predictions[0:100], predictions[100:200], rtol=1e-3)
def test_spline_transformer_periodic_spline_backport():
"""Test that the backport of extrapolate="periodic" works correctly"""
X = np.linspace(-2, 3.5, 10)[:, None]
degree = 2
# Use periodic extrapolation backport in SplineTransformer
transformer = SplineTransformer(
degree=degree, extrapolation="periodic", knots=[[-1.0], [0.0], [1.0]]
)
Xt = transformer.fit_transform(X)
# Use periodic extrapolation in BSpline
coef = np.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 1.0]])
spl = BSpline(np.arange(-3, 4), coef, degree, "periodic")
Xspl = spl(X[:, 0])
assert_allclose(Xt, Xspl)
def test_spline_transformer_periodic_splines_periodicity():
"""Test if shifted knots result in the same transformation up to permutation."""
X = np.linspace(0, 10, 101)[:, None]
transformer_1 = SplineTransformer(
degree=3,
extrapolation="periodic",
knots=[[0.0], [1.0], [3.0], [4.0], [5.0], [8.0]],
)
transformer_2 = SplineTransformer(
degree=3,
extrapolation="periodic",
knots=[[1.0], [3.0], [4.0], [5.0], [8.0], [9.0]],
)
Xt_1 = transformer_1.fit_transform(X)
Xt_2 = transformer_2.fit_transform(X)
assert_allclose(Xt_1, Xt_2[:, [4, 0, 1, 2, 3]])
@pytest.mark.parametrize("degree", [3, 5])
def test_spline_transformer_periodic_splines_smoothness(degree):
"""Test that spline transformation is smooth at first / last knot."""
X = np.linspace(-2, 10, 10_000)[:, None]
transformer = SplineTransformer(
degree=degree,
extrapolation="periodic",
knots=[[0.0], [1.0], [3.0], [4.0], [5.0], [8.0]],
)
Xt = transformer.fit_transform(X)
delta = (X.max() - X.min()) / len(X)
tol = 10 * delta
dXt = Xt
# We expect splines of degree `degree` to be (`degree`-1) times
# continuously differentiable. I.e. for d = 0, ..., `degree` - 1 the d-th
# derivative should be continuous. This is the case if the (d+1)-th
# numerical derivative is reasonably small (smaller than `tol` in absolute
# value). We thus compute d-th numeric derivatives for d = 1, ..., `degree`
# and compare them to `tol`.
#
# Note that the 0-th derivative is the function itself, such that we are
# also checking its continuity.
for d in range(1, degree + 1):
# Check continuity of the (d-1)-th derivative
diff = np.diff(dXt, axis=0)
assert np.abs(diff).max() < tol
# Compute d-th numeric derivative
dXt = diff / delta
# As degree `degree` splines are not `degree` times continuously
# differentiable at the knots, the `degree + 1`-th numeric derivative
# should have spikes at the knots.
diff = np.diff(dXt, axis=0)
assert np.abs(diff).max() > 1
@pytest.mark.parametrize(["bias", "intercept"], [(True, False), (False, True)])
@pytest.mark.parametrize("degree", [1, 2, 3, 4, 5])
def test_spline_transformer_extrapolation(bias, intercept, degree):
"""Test that B-spline extrapolation works correctly."""
# we use a straight line for that
X = np.linspace(-1, 1, 100)[:, None]
y = X.squeeze()
# 'constant'
pipe = Pipeline(
[
[
"spline",
SplineTransformer(
n_knots=4,
degree=degree,
include_bias=bias,
extrapolation="constant",
),
],
["ols", LinearRegression(fit_intercept=intercept)],
]
)
pipe.fit(X, y)
assert_allclose(pipe.predict([[-10], [5]]), [-1, 1])
# 'linear'
pipe = Pipeline(
[
[
"spline",
SplineTransformer(
n_knots=4,
degree=degree,
include_bias=bias,
extrapolation="linear",
),
],
["ols", LinearRegression(fit_intercept=intercept)],
]
)
pipe.fit(X, y)
assert_allclose(pipe.predict([[-10], [5]]), [-10, 5])
# 'error'
splt = SplineTransformer(
n_knots=4, degree=degree, include_bias=bias, extrapolation="error"
)
splt.fit(X)
msg = "X contains values beyond the limits of the knots"
with pytest.raises(ValueError, match=msg):
splt.transform([[-10]])
with pytest.raises(ValueError, match=msg):
splt.transform([[5]])
def test_spline_transformer_kbindiscretizer():
"""Test that a B-spline of degree=0 is equivalent to KBinsDiscretizer."""
rng = np.random.RandomState(97531)
X = rng.randn(200).reshape(200, 1)
n_bins = 5
n_knots = n_bins + 1
splt = SplineTransformer(
n_knots=n_knots, degree=0, knots="quantile", include_bias=True
)
splines = splt.fit_transform(X)
kbd = KBinsDiscretizer(n_bins=n_bins, encode="onehot-dense", strategy="quantile")
kbins = kbd.fit_transform(X)
# Though they should be exactly equal, we test approximately with high
# accuracy.
assert_allclose(splines, kbins, rtol=1e-13)
@pytest.mark.skipif(
sp_version < parse_version("1.8.0"),
reason="The option `sparse_output` is available as of scipy 1.8.0",
)
@pytest.mark.parametrize("degree", range(1, 3))
@pytest.mark.parametrize("knots", ["uniform", "quantile"])
@pytest.mark.parametrize(
"extrapolation", ["error", "constant", "linear", "continue", "periodic"]
)
@pytest.mark.parametrize("include_bias", [False, True])
def test_spline_transformer_sparse_output(
degree, knots, extrapolation, include_bias, global_random_seed
):
rng = np.random.RandomState(global_random_seed)
X = rng.randn(200).reshape(40, 5)
splt_dense = SplineTransformer(
degree=degree,
knots=knots,
extrapolation=extrapolation,
include_bias=include_bias,
sparse_output=False,
)
splt_sparse = SplineTransformer(
degree=degree,
knots=knots,
extrapolation=extrapolation,
include_bias=include_bias,
sparse_output=True,
)
splt_dense.fit(X)
splt_sparse.fit(X)
X_trans_sparse = splt_sparse.transform(X)
X_trans_dense = splt_dense.transform(X)
assert sparse.issparse(X_trans_sparse) and X_trans_sparse.format == "csr"
assert_allclose(X_trans_dense, X_trans_sparse.toarray())
# extrapolation regime
X_min = np.amin(X, axis=0)
X_max = np.amax(X, axis=0)
X_extra = np.r_[
np.linspace(X_min - 5, X_min, 10), np.linspace(X_max, X_max + 5, 10)
]
if extrapolation == "error":
msg = "X contains values beyond the limits of the knots"
with pytest.raises(ValueError, match=msg):
splt_dense.transform(X_extra)
msg = "Out of bounds"
with pytest.raises(ValueError, match=msg):
splt_sparse.transform(X_extra)
else:
assert_allclose(
splt_dense.transform(X_extra), splt_sparse.transform(X_extra).toarray()
)
@pytest.mark.skipif(
sp_version >= parse_version("1.8.0"),
reason="The option `sparse_output` is available as of scipy 1.8.0",
)
def test_spline_transformer_sparse_output_raise_error_for_old_scipy():
"""Test that SplineTransformer with sparse=True raises for scipy<1.8.0."""
X = [[1], [2]]
with pytest.raises(ValueError, match="scipy>=1.8.0"):
SplineTransformer(sparse_output=True).fit(X)
@pytest.mark.parametrize("n_knots", [5, 10])
@pytest.mark.parametrize("include_bias", [True, False])
@pytest.mark.parametrize("degree", [3, 4])
@pytest.mark.parametrize(
"extrapolation", ["error", "constant", "linear", "continue", "periodic"]
)
@pytest.mark.parametrize("sparse_output", [False, True])
def test_spline_transformer_n_features_out(
n_knots, include_bias, degree, extrapolation, sparse_output
):
"""Test that transform results in n_features_out_ features."""
if sparse_output and sp_version < parse_version("1.8.0"):
pytest.skip("The option `sparse_output` is available as of scipy 1.8.0")
splt = SplineTransformer(
n_knots=n_knots,
degree=degree,
include_bias=include_bias,
extrapolation=extrapolation,
sparse_output=sparse_output,
)
X = np.linspace(0, 1, 10)[:, None]
splt.fit(X)
assert splt.transform(X).shape[1] == splt.n_features_out_
@pytest.mark.parametrize(
"params, err_msg",
[
({"degree": (-1, 2)}, r"degree=\(min_degree, max_degree\) must"),
({"degree": (0, 1.5)}, r"degree=\(min_degree, max_degree\) must"),
({"degree": (3, 2)}, r"degree=\(min_degree, max_degree\) must"),
({"degree": (1, 2, 3)}, r"int or tuple \(min_degree, max_degree\)"),
],
)
def test_polynomial_features_input_validation(params, err_msg):
"""Test that we raise errors for invalid input in PolynomialFeatures."""
X = [[1], [2]]
with pytest.raises(ValueError, match=err_msg):
PolynomialFeatures(**params).fit(X)
@pytest.fixture()
def single_feature_degree3():
X = np.arange(6)[:, np.newaxis]
P = np.hstack([np.ones_like(X), X, X**2, X**3])
return X, P
@pytest.mark.parametrize(
"degree, include_bias, interaction_only, indices",
[
(3, True, False, slice(None, None)),
(3, False, False, slice(1, None)),
(3, True, True, [0, 1]),
(3, False, True, [1]),
((2, 3), True, False, [0, 2, 3]),
((2, 3), False, False, [2, 3]),
((2, 3), True, True, [0]),
((2, 3), False, True, []),
],
)
@pytest.mark.parametrize("X_container", [None] + CSR_CONTAINERS + CSC_CONTAINERS)
def test_polynomial_features_one_feature(
single_feature_degree3,
degree,
include_bias,
interaction_only,
indices,
X_container,
):
"""Test PolynomialFeatures on single feature up to degree 3."""
X, P = single_feature_degree3
if X_container is not None:
X = X_container(X)
tf = PolynomialFeatures(
degree=degree, include_bias=include_bias, interaction_only=interaction_only
).fit(X)
out = tf.transform(X)
if X_container is not None:
out = out.toarray()
assert_allclose(out, P[:, indices])
if tf.n_output_features_ > 0:
assert tf.powers_.shape == (tf.n_output_features_, tf.n_features_in_)
@pytest.fixture()
def two_features_degree3():
X = np.arange(6).reshape((3, 2))
x1 = X[:, :1]
x2 = X[:, 1:]
P = np.hstack(
[
x1**0 * x2**0, # 0
x1**1 * x2**0, # 1
x1**0 * x2**1, # 2
x1**2 * x2**0, # 3
x1**1 * x2**1, # 4
x1**0 * x2**2, # 5
x1**3 * x2**0, # 6
x1**2 * x2**1, # 7
x1**1 * x2**2, # 8
x1**0 * x2**3, # 9
]
)
return X, P
@pytest.mark.parametrize(
"degree, include_bias, interaction_only, indices",
[
(2, True, False, slice(0, 6)),
(2, False, False, slice(1, 6)),
(2, True, True, [0, 1, 2, 4]),
(2, False, True, [1, 2, 4]),
((2, 2), True, False, [0, 3, 4, 5]),
((2, 2), False, False, [3, 4, 5]),
((2, 2), True, True, [0, 4]),
((2, 2), False, True, [4]),
(3, True, False, slice(None, None)),
(3, False, False, slice(1, None)),
(3, True, True, [0, 1, 2, 4]),
(3, False, True, [1, 2, 4]),
((2, 3), True, False, [0, 3, 4, 5, 6, 7, 8, 9]),
((2, 3), False, False, slice(3, None)),
((2, 3), True, True, [0, 4]),
((2, 3), False, True, [4]),
((3, 3), True, False, [0, 6, 7, 8, 9]),
((3, 3), False, False, [6, 7, 8, 9]),
((3, 3), True, True, [0]),
((3, 3), False, True, []), # would need 3 input features
],
)
@pytest.mark.parametrize("X_container", [None] + CSR_CONTAINERS + CSC_CONTAINERS)
def test_polynomial_features_two_features(
two_features_degree3,
degree,
include_bias,
interaction_only,
indices,
X_container,
):
"""Test PolynomialFeatures on 2 features up to degree 3."""
X, P = two_features_degree3
if X_container is not None:
X = X_container(X)
tf = PolynomialFeatures(
degree=degree, include_bias=include_bias, interaction_only=interaction_only
).fit(X)
out = tf.transform(X)
if X_container is not None:
out = out.toarray()
assert_allclose(out, P[:, indices])
if tf.n_output_features_ > 0:
assert tf.powers_.shape == (tf.n_output_features_, tf.n_features_in_)
def test_polynomial_feature_names():
X = np.arange(30).reshape(10, 3)
poly = PolynomialFeatures(degree=2, include_bias=True).fit(X)
feature_names = poly.get_feature_names_out()
assert_array_equal(
["1", "x0", "x1", "x2", "x0^2", "x0 x1", "x0 x2", "x1^2", "x1 x2", "x2^2"],
feature_names,
)
assert len(feature_names) == poly.transform(X).shape[1]
poly = PolynomialFeatures(degree=3, include_bias=False).fit(X)
feature_names = poly.get_feature_names_out(["a", "b", "c"])
assert_array_equal(
[
"a",
"b",
"c",
"a^2",
"a b",
"a c",
"b^2",
"b c",
"c^2",
"a^3",
"a^2 b",
"a^2 c",
"a b^2",
"a b c",
"a c^2",
"b^3",
"b^2 c",
"b c^2",
"c^3",
],
feature_names,
)
assert len(feature_names) == poly.transform(X).shape[1]
poly = PolynomialFeatures(degree=(2, 3), include_bias=False).fit(X)
feature_names = poly.get_feature_names_out(["a", "b", "c"])
assert_array_equal(
[
"a^2",
"a b",
"a c",
"b^2",
"b c",
"c^2",
"a^3",
"a^2 b",
"a^2 c",
"a b^2",
"a b c",
"a c^2",
"b^3",
"b^2 c",
"b c^2",
"c^3",
],
feature_names,
)
assert len(feature_names) == poly.transform(X).shape[1]
poly = PolynomialFeatures(
degree=(3, 3), include_bias=True, interaction_only=True
).fit(X)
feature_names = poly.get_feature_names_out(["a", "b", "c"])
assert_array_equal(["1", "a b c"], feature_names)
assert len(feature_names) == poly.transform(X).shape[1]
# test some unicode
poly = PolynomialFeatures(degree=1, include_bias=True).fit(X)
feature_names = poly.get_feature_names_out(["\u0001F40D", "\u262e", "\u05d0"])
assert_array_equal(["1", "\u0001F40D", "\u262e", "\u05d0"], feature_names)
@pytest.mark.parametrize(
["deg", "include_bias", "interaction_only", "dtype"],
[
(1, True, False, int),
(2, True, False, int),
(2, True, False, np.float32),
(2, True, False, np.float64),
(3, False, False, np.float64),
(3, False, True, np.float64),
(4, False, False, np.float64),
(4, False, True, np.float64),
],
)
@pytest.mark.parametrize("csc_container", CSC_CONTAINERS)
def test_polynomial_features_csc_X(
deg, include_bias, interaction_only, dtype, csc_container
):
rng = np.random.RandomState(0)
X = rng.randint(0, 2, (100, 2))
X_csc = csc_container(X)
est = PolynomialFeatures(
deg, include_bias=include_bias, interaction_only=interaction_only
)
Xt_csc = est.fit_transform(X_csc.astype(dtype))
Xt_dense = est.fit_transform(X.astype(dtype))
assert sparse.issparse(Xt_csc) and Xt_csc.format == "csc"
assert Xt_csc.dtype == Xt_dense.dtype
assert_array_almost_equal(Xt_csc.toarray(), Xt_dense)
@pytest.mark.parametrize(
["deg", "include_bias", "interaction_only", "dtype"],
[
(1, True, False, int),
(2, True, False, int),
(2, True, False, np.float32),
(2, True, False, np.float64),
(3, False, False, np.float64),
(3, False, True, np.float64),
],
)
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_polynomial_features_csr_X(
deg, include_bias, interaction_only, dtype, csr_container
):
rng = np.random.RandomState(0)
X = rng.randint(0, 2, (100, 2))
X_csr = csr_container(X)
est = PolynomialFeatures(
deg, include_bias=include_bias, interaction_only=interaction_only
)
Xt_csr = est.fit_transform(X_csr.astype(dtype))
Xt_dense = est.fit_transform(X.astype(dtype, copy=False))
assert sparse.issparse(Xt_csr) and Xt_csr.format == "csr"
assert Xt_csr.dtype == Xt_dense.dtype
assert_array_almost_equal(Xt_csr.toarray(), Xt_dense)
@pytest.mark.parametrize("n_features", [1, 4, 5])
@pytest.mark.parametrize(
"min_degree, max_degree", [(0, 1), (0, 2), (1, 3), (0, 4), (3, 4)]
)
@pytest.mark.parametrize("interaction_only", [True, False])
@pytest.mark.parametrize("include_bias", [True, False])
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_num_combinations(
n_features, min_degree, max_degree, interaction_only, include_bias, csr_container
):
"""
Test that n_output_features_ is calculated correctly.
"""
x = csr_container(([1], ([0], [n_features - 1])))
est = PolynomialFeatures(
degree=max_degree,
interaction_only=interaction_only,
include_bias=include_bias,
)
est.fit(x)
num_combos = est.n_output_features_
combos = PolynomialFeatures._combinations(
n_features=n_features,
min_degree=0,
max_degree=max_degree,
interaction_only=interaction_only,
include_bias=include_bias,
)
assert num_combos == sum([1 for _ in combos])
@pytest.mark.parametrize(
["deg", "include_bias", "interaction_only", "dtype"],
[
(2, True, False, np.float32),
(2, True, False, np.float64),
(3, False, False, np.float64),
(3, False, True, np.float64),
],
)
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_polynomial_features_csr_X_floats(
deg, include_bias, interaction_only, dtype, csr_container
):
X_csr = csr_container(sparse_random(1000, 10, 0.5, random_state=0))
X = X_csr.toarray()
est = PolynomialFeatures(
deg, include_bias=include_bias, interaction_only=interaction_only
)
Xt_csr = est.fit_transform(X_csr.astype(dtype))
Xt_dense = est.fit_transform(X.astype(dtype))
assert sparse.issparse(Xt_csr) and Xt_csr.format == "csr"
assert Xt_csr.dtype == Xt_dense.dtype
assert_array_almost_equal(Xt_csr.toarray(), Xt_dense)
@pytest.mark.parametrize(
["zero_row_index", "deg", "interaction_only"],
[
(0, 2, True),
(1, 2, True),
(2, 2, True),
(0, 3, True),
(1, 3, True),
(2, 3, True),
(0, 2, False),
(1, 2, False),
(2, 2, False),
(0, 3, False),
(1, 3, False),
(2, 3, False),
],
)
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_polynomial_features_csr_X_zero_row(
zero_row_index, deg, interaction_only, csr_container
):
X_csr = csr_container(sparse_random(3, 10, 1.0, random_state=0))
X_csr[zero_row_index, :] = 0.0
X = X_csr.toarray()
est = PolynomialFeatures(deg, include_bias=False, interaction_only=interaction_only)
Xt_csr = est.fit_transform(X_csr)
Xt_dense = est.fit_transform(X)
assert sparse.issparse(Xt_csr) and Xt_csr.format == "csr"
assert Xt_csr.dtype == Xt_dense.dtype
assert_array_almost_equal(Xt_csr.toarray(), Xt_dense)
# This degree should always be one more than the highest degree supported by
# _csr_expansion.
@pytest.mark.parametrize(
["include_bias", "interaction_only"],
[(True, True), (True, False), (False, True), (False, False)],
)
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_polynomial_features_csr_X_degree_4(
include_bias, interaction_only, csr_container
):
X_csr = csr_container(sparse_random(1000, 10, 0.5, random_state=0))
X = X_csr.toarray()
est = PolynomialFeatures(
4, include_bias=include_bias, interaction_only=interaction_only
)
Xt_csr = est.fit_transform(X_csr)
Xt_dense = est.fit_transform(X)
assert sparse.issparse(Xt_csr) and Xt_csr.format == "csr"
assert Xt_csr.dtype == Xt_dense.dtype
assert_array_almost_equal(Xt_csr.toarray(), Xt_dense)
@pytest.mark.parametrize(
["deg", "dim", "interaction_only"],
[
(2, 1, True),
(2, 2, True),
(3, 1, True),
(3, 2, True),
(3, 3, True),
(2, 1, False),
(2, 2, False),
(3, 1, False),
(3, 2, False),
(3, 3, False),
],
)
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_polynomial_features_csr_X_dim_edges(deg, dim, interaction_only, csr_container):
X_csr = csr_container(sparse_random(1000, dim, 0.5, random_state=0))
X = X_csr.toarray()
est = PolynomialFeatures(deg, interaction_only=interaction_only)
Xt_csr = est.fit_transform(X_csr)
Xt_dense = est.fit_transform(X)
assert sparse.issparse(Xt_csr) and Xt_csr.format == "csr"
assert Xt_csr.dtype == Xt_dense.dtype
assert_array_almost_equal(Xt_csr.toarray(), Xt_dense)
@pytest.mark.parametrize("interaction_only", [True, False])
@pytest.mark.parametrize("include_bias", [True, False])
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_csr_polynomial_expansion_index_overflow_non_regression(
interaction_only, include_bias, csr_container
):
"""Check the automatic index dtype promotion to `np.int64` when needed.
This ensures that sufficiently large input configurations get
properly promoted to use `np.int64` for index and indptr representation
while preserving data integrity. Non-regression test for gh-16803.
Note that this is only possible for Python runtimes with a 64 bit address
space. On 32 bit platforms, a `ValueError` is raised instead.
"""
def degree_2_calc(d, i, j):
if interaction_only:
return d * i - (i**2 + 3 * i) // 2 - 1 + j
else:
return d * i - (i**2 + i) // 2 + j
n_samples = 13
n_features = 120001
data_dtype = np.float32
data = np.arange(1, 5, dtype=np.int64)
row = np.array([n_samples - 2, n_samples - 2, n_samples - 1, n_samples - 1])
# An int64 dtype is required to avoid overflow error on Windows within the
# `degree_2_calc` function.
col = np.array(
[n_features - 2, n_features - 1, n_features - 2, n_features - 1], dtype=np.int64
)
X = csr_container(
(data, (row, col)),
shape=(n_samples, n_features),
dtype=data_dtype,
)
pf = PolynomialFeatures(
interaction_only=interaction_only, include_bias=include_bias, degree=2
)
# Calculate the number of combinations a-priori, and if needed check for
# the correct ValueError and terminate the test early.
num_combinations = pf._num_combinations(
n_features=n_features,
min_degree=0,
max_degree=2,
interaction_only=pf.interaction_only,
include_bias=pf.include_bias,
)
if num_combinations > np.iinfo(np.intp).max:
msg = (
r"The output that would result from the current configuration would have"
r" \d* features which is too large to be indexed"
)
with pytest.raises(ValueError, match=msg):
pf.fit(X)
return
X_trans = pf.fit_transform(X)
row_nonzero, col_nonzero = X_trans.nonzero()
n_degree_1_features_out = n_features + include_bias
max_degree_2_idx = (
degree_2_calc(n_features, col[int(not interaction_only)], col[1])
+ n_degree_1_features_out
)
# Account for bias of all samples except last one which will be handled
# separately since there are distinct data values before it
data_target = [1] * (n_samples - 2) if include_bias else []
col_nonzero_target = [0] * (n_samples - 2) if include_bias else []
for i in range(2):
x = data[2 * i]
y = data[2 * i + 1]
x_idx = col[2 * i]
y_idx = col[2 * i + 1]
if include_bias:
data_target.append(1)
col_nonzero_target.append(0)
data_target.extend([x, y])
col_nonzero_target.extend(
[x_idx + int(include_bias), y_idx + int(include_bias)]
)
if not interaction_only:
data_target.extend([x * x, x * y, y * y])
col_nonzero_target.extend(
[
degree_2_calc(n_features, x_idx, x_idx) + n_degree_1_features_out,
degree_2_calc(n_features, x_idx, y_idx) + n_degree_1_features_out,
degree_2_calc(n_features, y_idx, y_idx) + n_degree_1_features_out,
]
)
else:
data_target.extend([x * y])
col_nonzero_target.append(
degree_2_calc(n_features, x_idx, y_idx) + n_degree_1_features_out
)
nnz_per_row = int(include_bias) + 3 + 2 * int(not interaction_only)
assert pf.n_output_features_ == max_degree_2_idx + 1
assert X_trans.dtype == data_dtype
assert X_trans.shape == (n_samples, max_degree_2_idx + 1)
assert X_trans.indptr.dtype == X_trans.indices.dtype == np.int64
# Ensure that dtype promotion was actually required:
assert X_trans.indices.max() > np.iinfo(np.int32).max
row_nonzero_target = list(range(n_samples - 2)) if include_bias else []
row_nonzero_target.extend(
[n_samples - 2] * nnz_per_row + [n_samples - 1] * nnz_per_row
)
assert_allclose(X_trans.data, data_target)
assert_array_equal(row_nonzero, row_nonzero_target)
assert_array_equal(col_nonzero, col_nonzero_target)
@pytest.mark.parametrize(
"degree, n_features",
[
# Needs promotion to int64 when interaction_only=False
(2, 65535),
(3, 2344),
# This guarantees that the intermediate operation when calculating
# output columns would overflow a C-long, hence checks that python-
# longs are being used.
(2, int(np.sqrt(np.iinfo(np.int64).max) + 1)),
(3, 65535),
# This case tests the second clause of the overflow check which
# takes into account the value of `n_features` itself.
(2, int(np.sqrt(np.iinfo(np.int64).max))),
],
)
@pytest.mark.parametrize("interaction_only", [True, False])
@pytest.mark.parametrize("include_bias", [True, False])
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_csr_polynomial_expansion_index_overflow(
degree, n_features, interaction_only, include_bias, csr_container
):
"""Tests known edge-cases to the dtype promotion strategy and custom
Cython code, including a current bug in the upstream
`scipy.sparse.hstack`.
"""
data = [1.0]
# Use int32 indices as much as we can
indices_dtype = np.int32 if n_features - 1 <= np.iinfo(np.int32).max else np.int64
row = np.array([0], dtype=indices_dtype)
col = np.array([n_features - 1], dtype=indices_dtype)
# First degree index
expected_indices = [
n_features - 1 + int(include_bias),
]
# Second degree index
expected_indices.append(n_features * (n_features + 1) // 2 + expected_indices[0])
# Third degree index
expected_indices.append(
n_features * (n_features + 1) * (n_features + 2) // 6 + expected_indices[1]
)
X = csr_container((data, (row, col)))
pf = PolynomialFeatures(
interaction_only=interaction_only, include_bias=include_bias, degree=degree
)
# Calculate the number of combinations a-priori, and if needed check for
# the correct ValueError and terminate the test early.
num_combinations = pf._num_combinations(
n_features=n_features,
min_degree=0,
max_degree=degree,
interaction_only=pf.interaction_only,
include_bias=pf.include_bias,
)
if num_combinations > np.iinfo(np.intp).max:
msg = (
r"The output that would result from the current configuration would have"
r" \d* features which is too large to be indexed"
)
with pytest.raises(ValueError, match=msg):
pf.fit(X)
return
# In SciPy < 1.8, a bug occurs when an intermediate matrix in
# `to_stack` in `hstack` fits within int32 however would require int64 when
# combined with all previous matrices in `to_stack`.
if sp_version < parse_version("1.8.0"):
has_bug = False
max_int32 = np.iinfo(np.int32).max
cumulative_size = n_features + include_bias
for deg in range(2, degree + 1):
max_indptr = _calc_total_nnz(X.indptr, interaction_only, deg)
max_indices = _calc_expanded_nnz(n_features, interaction_only, deg) - 1
cumulative_size += max_indices + 1
needs_int64 = max(max_indices, max_indptr) > max_int32
has_bug |= not needs_int64 and cumulative_size > max_int32
if has_bug:
msg = r"In scipy versions `<1.8.0`, the function `scipy.sparse.hstack`"
with pytest.raises(ValueError, match=msg):
X_trans = pf.fit_transform(X)
return
# When `n_features>=65535`, `scipy.sparse.hstack` may not use the right
# dtype for representing indices and indptr if `n_features` is still
# small enough so that each block matrix's indices and indptr arrays
# can be represented with `np.int32`. We test `n_features==65535`
# since it is guaranteed to run into this bug.
if (
sp_version < parse_version("1.9.2")
and n_features == 65535
and degree == 2
and not interaction_only
): # pragma: no cover
msg = r"In scipy versions `<1.9.2`, the function `scipy.sparse.hstack`"
with pytest.raises(ValueError, match=msg):
X_trans = pf.fit_transform(X)
return
X_trans = pf.fit_transform(X)
expected_dtype = np.int64 if num_combinations > np.iinfo(np.int32).max else np.int32
# Terms higher than first degree
non_bias_terms = 1 + (degree - 1) * int(not interaction_only)
expected_nnz = int(include_bias) + non_bias_terms
assert X_trans.dtype == X.dtype
assert X_trans.shape == (1, pf.n_output_features_)
assert X_trans.indptr.dtype == X_trans.indices.dtype == expected_dtype
assert X_trans.nnz == expected_nnz
if include_bias:
assert X_trans[0, 0] == pytest.approx(1.0)
for idx in range(non_bias_terms):
assert X_trans[0, expected_indices[idx]] == pytest.approx(1.0)
offset = interaction_only * n_features
if degree == 3:
offset *= 1 + n_features
assert pf.n_output_features_ == expected_indices[degree - 1] + 1 - offset
@pytest.mark.parametrize("interaction_only", [True, False])
@pytest.mark.parametrize("include_bias", [True, False])
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_csr_polynomial_expansion_too_large_to_index(
interaction_only, include_bias, csr_container
):
n_features = np.iinfo(np.int64).max // 2
data = [1.0]
row = [0]
col = [n_features - 1]
X = csr_container((data, (row, col)))
pf = PolynomialFeatures(
interaction_only=interaction_only, include_bias=include_bias, degree=(2, 2)
)
msg = (
r"The output that would result from the current configuration would have \d*"
r" features which is too large to be indexed"
)
with pytest.raises(ValueError, match=msg):
pf.fit(X)
with pytest.raises(ValueError, match=msg):
pf.fit_transform(X)
@pytest.mark.parametrize("sparse_container", CSR_CONTAINERS + CSC_CONTAINERS)
def test_polynomial_features_behaviour_on_zero_degree(sparse_container):
"""Check that PolynomialFeatures raises error when degree=0 and include_bias=False,
and output a single constant column when include_bias=True
"""
X = np.ones((10, 2))
poly = PolynomialFeatures(degree=0, include_bias=False)
err_msg = (
"Setting degree to zero and include_bias to False would result in"
" an empty output array."
)
with pytest.raises(ValueError, match=err_msg):
poly.fit_transform(X)
poly = PolynomialFeatures(degree=(0, 0), include_bias=False)
err_msg = (
"Setting both min_degree and max_degree to zero and include_bias to"
" False would result in an empty output array."
)
with pytest.raises(ValueError, match=err_msg):
poly.fit_transform(X)
for _X in [X, sparse_container(X)]:
poly = PolynomialFeatures(degree=0, include_bias=True)
output = poly.fit_transform(_X)
# convert to dense array if needed
if sparse.issparse(output):
output = output.toarray()
assert_array_equal(output, np.ones((X.shape[0], 1)))
def test_sizeof_LARGEST_INT_t():
# On Windows, scikit-learn is typically compiled with MSVC that
# does not support int128 arithmetic (at the time of writing):
# https://stackoverflow.com/a/6761962/163740
if sys.platform == "win32" or (
sys.maxsize <= 2**32 and sys.platform != "emscripten"
):
expected_size = 8
else:
expected_size = 16
assert _get_sizeof_LARGEST_INT_t() == expected_size
@pytest.mark.xfail(
sys.platform == "win32",
reason=(
"On Windows, scikit-learn is typically compiled with MSVC that does not support"
" int128 arithmetic (at the time of writing)"
),
run=True,
)
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_csr_polynomial_expansion_windows_fail(csr_container):
# Minimum needed to ensure integer overflow occurs while guaranteeing an
# int64-indexable output.
n_features = int(np.iinfo(np.int64).max ** (1 / 3) + 3)
data = [1.0]
row = [0]
col = [n_features - 1]
# First degree index
expected_indices = [
n_features - 1,
]
# Second degree index
expected_indices.append(
int(n_features * (n_features + 1) // 2 + expected_indices[0])
)
# Third degree index
expected_indices.append(
int(n_features * (n_features + 1) * (n_features + 2) // 6 + expected_indices[1])
)
X = csr_container((data, (row, col)))
pf = PolynomialFeatures(interaction_only=False, include_bias=False, degree=3)
if sys.maxsize <= 2**32:
msg = (
r"The output that would result from the current configuration would"
r" have \d*"
r" features which is too large to be indexed"
)
with pytest.raises(ValueError, match=msg):
pf.fit_transform(X)
else:
X_trans = pf.fit_transform(X)
for idx in range(3):
assert X_trans[0, expected_indices[idx]] == pytest.approx(1.0)
|