99
1010
1111def check_lr_fit (regressor , X , y ):
12- r """
12+ """
1313 Checks that a (linear) regressor is fitted, and if not,
14- fits it with the provided data
15-
16- :param regressor: sklearn-style regressor
17- :type regressor: object
18- :param X: feature matrix with which to fit the regressor
19- if it is not already fitted
20- :type X: array
21- :param y: target values with which to fit the regressor
22- if it is not already fitted
23- :type y: array
14+ fits it with the provided data.
15+
16+ Parameters
17+ ----------
18+ regressor : object
19+ sklearn-style regressor
20+ X : array-like
21+ Feature matrix with which to fit the regressor if it is not already fitted
22+ y : array-like
23+ Target values with which to fit the regressor if it is not already fitted
24+
25+ Returns
26+ -------
27+ fitted_regressor : object
28+ The fitted regressor. If input regressor was already fitted and compatible with
29+ the data, returns a deep copy. Otherwise returns a newly fitted regressor.
30+
31+ Raises
32+ ------
33+ ValueError
34+ If the fitted regressor's coefficients dimensions are incompatible with the
35+ target space.
2436 """
2537 try :
2638 check_is_fitted (regressor )
@@ -32,18 +44,18 @@ def check_lr_fit(regressor, X, y):
3244 # Check compatibility with y
3345 if fitted_regressor .coef_ .ndim != y .ndim :
3446 raise ValueError (
35- "The regressor coefficients have a dimension incompatible "
36- "with the supplied target space. "
37- "The coefficients have dimension %d and the targets "
38- "have dimension %d" % ( fitted_regressor . coef_ . ndim , y .ndim )
47+ "The regressor coefficients have a dimension incompatible with the "
48+ "supplied target space. The coefficients have dimension "
49+ f" { fitted_regressor . coef_ . ndim } and the targets have dimension "
50+ f" { y .ndim } "
3951 )
4052 elif y .ndim == 2 :
4153 if fitted_regressor .coef_ .shape [0 ] != y .shape [1 ]:
4254 raise ValueError (
43- "The regressor coefficients have a shape incompatible "
44- "with the supplied target space. "
45- "The coefficients have shape %r and the targets "
46- "have shape %r" % ( fitted_regressor . coef_ . shape , y .shape )
55+ "The regressor coefficients have a shape incompatible with the "
56+ "supplied target space. The coefficients have shape "
57+ f" { fitted_regressor . coef_ . shape } and the targets have shape "
58+ f" { y .shape } "
4759 )
4860
4961 except NotFittedError :
@@ -54,20 +66,37 @@ def check_lr_fit(regressor, X, y):
5466
5567
5668def check_krr_fit (regressor , K , X , y ):
57- r """
69+ """
5870 Checks that a (kernel ridge) regressor is fitted, and if not,
59- fits it with the provided data
60-
61- :param regressor: sklearn-style regressor
62- :type regressor: object
63- :param K: kernel matrix with which to fit the regressor
64- if it is not already fitted
65- :type K: array
66- :param X: feature matrix with which to check the regressor
67- :type X: array
68- :param y: target values with which to fit the regressor
69- if it is not already fitted
70- :type y: array
71+ fits it with the provided data.
72+
73+ Parameters
74+ ----------
75+ regressor : object
76+ sklearn-style regressor
77+ K : array-like
78+ Kernel matrix with which to fit the regressor if it is not already fitted
79+ X : array-like
80+ Feature matrix with which to check the regressor
81+ y : array-like
82+ Target values with which to fit the regressor if it is not already fitted
83+
84+ Returns
85+ -------
86+ fitted_regressor : object
87+ The fitted regressor. If input regressor was already fitted and compatible with
88+ the data, returns a deep copy. Otherwise returns a newly fitted regressor.
89+
90+ Raises
91+ ------
92+ ValueError
93+ If the fitted regressor's coefficients dimensions are incompatible with the
94+ target space.
95+
96+ Notes
97+ -----
98+ For unfitted regressors, sets the kernel to "precomputed" before fitting with the
99+ provided kernel matrix K to avoid recomputation.
71100 """
72101 try :
73102 check_is_fitted (regressor )
@@ -79,18 +108,18 @@ def check_krr_fit(regressor, K, X, y):
79108 # Check compatibility with y
80109 if fitted_regressor .dual_coef_ .ndim != y .ndim :
81110 raise ValueError (
82- "The regressor coefficients have a dimension incompatible "
83- "with the supplied target space. "
84- "The coefficients have dimension %d and the targets "
85- "have dimension %d" % ( fitted_regressor . dual_coef_ . ndim , y .ndim )
111+ "The regressor coefficients have a dimension incompatible with the "
112+ "supplied target space. The coefficients have dimension "
113+ f" { fitted_regressor . dual_coef_ . ndim } and the targets have dimension "
114+ f" { y .ndim } "
86115 )
87116 elif y .ndim == 2 :
88117 if fitted_regressor .dual_coef_ .shape [1 ] != y .shape [1 ]:
89118 raise ValueError (
90- "The regressor coefficients have a shape incompatible "
91- "with the supplied target space. "
92- "The coefficients have shape %r and the targets "
93- "have shape %r" % ( fitted_regressor . dual_coef_ . shape , y .shape )
119+ "The regressor coefficients have a shape incompatible with the "
120+ "supplied target space. The coefficients have shape "
121+ f" { fitted_regressor . dual_coef_ . shape } and the targets have shape "
122+ f" { y .shape } "
94123 )
95124
96125 except NotFittedError :
0 commit comments