API
ConstrainedLinearRegression
Bases: BaseEstimator
, RegressorMixin
Source code in src\constrainedlr\model.py
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 |
|
__init__(fit_intercept=True, alpha=0.0)
Least squares Linear Regression with optional constraints on its coefficients/weights.
ConstrainedLinearRegression fits a linear model with coefficients w = (w1, …, wp) to minimize the residual sum of squares between the observed targets in the dataset, and the targets predicted by the linear approximation, while at the same time imposing constraints on the signs and values of the coefficients.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fit_intercept |
bool
|
Whether to calculate the intercept for this model. |
True
|
alpha |
float
|
Constant that multiplies the L2 penalty term, controlling regularization strength. alpha must be a non-negative float i.e. in [0, inf). |
0.0
|
Attributes:
Name | Type | Description |
---|---|---|
coef_ |
Weight vector of shape (n_features,). |
|
intercept_ |
Independent/constant term in regression model. Set to None if fit_intercept = False. |
Source code in src\constrainedlr\model.py
fit(X, y, sample_weight=None, coefficients_sign_constraints={}, coefficients_range_constraints={}, intercept_sign_constraint=0, coefficients_sum_constraint=None)
Fit linear model with constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Union[np.ndarray, pd.DataFrame]
|
Training data of shape (n_samples, n_features). |
required |
y |
np.ndarray
|
Target values of shape (n_samples,). |
required |
sample_weight |
np.ndarray
|
Individual weights of shape (n_samples,) for each sample. |
None
|
coefficients_sign_constraints |
dict
|
Dictionary with sign constraints. Keys must be integers specifying the location of the corresponding feature in the columns in the dataset. Values must take the values: -1, 0, 1 indicating negative, unconstrained and positive sign respectively. Any column that is not present in the dictionary will default to 0. |
{}
|
coefficients_range_constraints |
dict
|
Dictionary of the form: |
{}
|
intercept_sign_constraint |
Union[int, str]
|
Indicates the sign of intercept, if present, and must take the values: -1, 0, 1. |
0
|
coefficients_sum_constraint |
float
|
Constraints the sum of all coefficients plus intercept (if present). |
None
|
Returns:
Type | Description |
---|---|
ConstrainedLinearRegression
|
Fitted Estimator. |
Source code in src\constrainedlr\model.py
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 |
|
predict(X)
Predict using the linear model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Union[np.ndarray, pd.DataFrame]
|
Samples of shape (n_samples, n_features). |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
Predicted values of shape (n_samples,). |