Linear Models
Linear Models
Linear models. This module is in very early development and is subject to frequent breaking changes. Since the backend is Faer in Rust, better performance might be achieved if your NumPy ndarrays are Fortran-style column major. This currently only supports f64.
This module requires the NumPy package. PDS only requires Polars, but you can get all the optional dependencies by
pip install polars_ds[all]
Classes:
| Name | Description |
|---|---|
ElasticNet |
Elastic Net Regression. |
GLM |
Generalized Linear Models. |
LR |
Normal or Ridge Regression. |
MixedModel |
A random-intercept linear mixed model, fit via restricted maximum likelihood (REML). |
OnlineLR |
Normal or Ridge Online Regression. This doesn't support dataframe inputs. |
ElasticNet
Elastic Net Regression.
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes an ElasticNet regressor. This is equivalent to Sklearn's Elastic Net if you set |
coeffs |
Returns a copy of the coefficients. |
fit |
Fit the Elastic Net model on NumPy data. |
fit_df |
Fit the Elastic Net model on a dataframe. This will overwrite previously set feature names. |
from_values |
Constructs a LR class instance from coefficients and bias values. |
predict |
Returns the prediction of this linear model. |
predict_df |
Computes the prediction of the linear model and append it as a column in the dataframe. If input |
set_input_features |
Sets the names of input features. |
Source code in python/polars_ds/linear_models.py
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 | |
__init__(l1_reg, l2_reg, has_bias=False, tol=1e-05, max_iter=2000, feature_names_in_=None)
Initializes an ElasticNet regressor. This is equivalent to Sklearn's Elastic Net if you set
alpha and l1_ratio to be: alpha = l1_reg + l2_reg, and l1_ratio = l1_reg / (l1_reg + l2_reg).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
l1_reg
|
float
|
The l1 regularization parameters for the elastic net. |
required |
l2_reg
|
float
|
The l2 regularization parameters for the elastic net. |
required |
has_bias
|
bool
|
Whether to add a bias term. Also known as intercept in other packages. |
False
|
tol
|
float
|
When updates are smaller than tol, the algorithm will stop. |
1e-05
|
max_iter
|
int
|
The max number of iterations the algorithm will run. |
2000
|
feature_names_in_
|
List[str] | None
|
Names for the incoming features, if available. If None, the names will be empty. They will be learned if .fit_df() is run later, or .set_input_features() is set later. |
None
|
Source code in python/polars_ds/linear_models.py
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 | |
coeffs()
Returns a copy of the coefficients.
Source code in python/polars_ds/linear_models.py
446 447 448 449 450 | |
fit(X, y, null_policy='ignore')
Fit the Elastic Net model on NumPy data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
The feature Matrix. NumPy 2D matrix only. |
required |
y
|
ndarray
|
The target data. NumPy array. Must be reshape-able to (-1, 1). |
required |
null_policy
|
NullPolicy
|
One of options shown here, but you can also pass in any numeric string. E.g you may pass '1.25' to mean fill nulls with 1.25. If the string cannot be converted to a float, an error will be thrown. Note: if the target column has null, the rows with nulls will always be dropped. Null-fill only applies to non-target columns. If target has null, then the row will still be dropped. |
'ignore'
|
Source code in python/polars_ds/linear_models.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
fit_df(df, features, target, null_policy='skip')
Fit the Elastic Net model on a dataframe. This will overwrite previously set feature names. The null policy only handles null values in df, not NaN values. It is the user's responsibility to handle NaN values if they exist in their pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
PolarsFrame
|
Either an eager or a lazy Polars dataframe. |
required |
features
|
List[str]
|
List of strings of column names. |
required |
target
|
str
|
The target column's name. |
required |
null_policy
|
NullPolicy
|
One of options shown here, but you can also pass in any numeric string. E.g you may pass '1.25' to mean fill nulls with 1.25. If the string cannot be converted to a float, an error will be thrown. Note: if the target column has null, the rows with nulls will always be dropped. Null-fill only applies to non-target columns. If target has null, then the row will still be dropped. |
'skip'
|
Source code in python/polars_ds/linear_models.py
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 | |
from_values(coeffs, bias=0.0, feature_names_in_=None)
classmethod
Constructs a LR class instance from coefficients and bias values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coeffs
|
List[float]
|
Iterable of numbers representing the coefficients |
required |
bias
|
float
|
Value for the bias term |
0.0
|
feature_names_in_
|
List[str] | None
|
Names for the incoming features, if available. If None, the names will be empty. They will be learned if .fit_df() is run later, or .set_input_features() is set later. |
None
|
Source code in python/polars_ds/linear_models.py
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 | |
predict(X)
Returns the prediction of this linear model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Data to predict on, as a matrix |
required |
Source code in python/polars_ds/linear_models.py
520 521 522 523 524 525 526 527 528 529 530 | |
predict_df(df, name='prediction')
Computes the prediction of the linear model and append it as a column in the dataframe. If input is lazy, output will be lazy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
PolarsFrame
|
Either an eager or a lazy Polars dataframe. |
required |
name
|
str
|
The name of the prediction column |
'prediction'
|
Source code in python/polars_ds/linear_models.py
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 | |
set_input_features(features)
Sets the names of input features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
List[str]
|
List of strings. |
required |
Source code in python/polars_ds/linear_models.py
433 434 435 436 437 438 439 440 441 442 443 444 | |
GLM
Generalized Linear Models.
The GLM generalizes linear regression by allowing the linear model to be related to the response variable via a link function and by allowing the magnitude of the variance of each measurement to be a function of its predicted value.
Currently, the variance function will be determined by the link function. If a family is given, then the canonical link function is used. Here is a mapping between currently implemented families and their link functions:
gaussian / normal ==> id (x -> x) poisson ==> log (x -> ln(x)) binomial / logistic ==> logit (x -> ln(x/(1-x))) gamma ==> inverse (x -> 1/x)
Reference
https://en.wikipedia.org/wiki/Generalized_linear_model
Methods:
| Name | Description |
|---|---|
__init__ |
Parameters |
__repr__ |
Shows a textual representation of the GLM. |
coeffs |
Returns a copy of the coefficients. |
fit |
Fit the GLM model on NumPy data. |
fit_df |
Fit the GLM model on a dataframe. This will overwrite previously set feature names. |
predict |
Returns the prediction of this linear model. |
set_input_features |
Sets the names of input features. |
Source code in python/polars_ds/linear_models.py
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 | |
__init__(add_bias=False, solver='irls', family='normal', max_iter=100, tol=1e-08, feature_names_in_=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
family
|
GLMFamily
|
One of "gaussian", "normal", "poisson", "binomial", "logistic", "gamma". Note "gaussian" and "normal" represent the same family. |
'normal'
|
add_bias
|
bool
|
Whether to add a bias term. Also known as intercept in other packages. |
False
|
max_iter
|
int
|
Max number of iterations for the algorithm |
100
|
tol
|
float
|
The tolerance for convergence |
1e-08
|
feature_names_in_
|
List[str] | None
|
Names for the incoming features, if available. If None, the names will be empty. They will be learned if .fit_df() is run later, or .set_input_features() is set later. |
None
|
Source code in python/polars_ds/linear_models.py
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 | |
__repr__()
Shows a textual representation of the GLM.
Source code in python/polars_ds/linear_models.py
769 770 771 772 773 | |
coeffs()
Returns a copy of the coefficients.
Source code in python/polars_ds/linear_models.py
836 837 838 839 840 | |
fit(X, y, null_policy='ignore')
Fit the GLM model on NumPy data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
The feature Matrix. NumPy 2D matrix only. |
required |
y
|
ndarray
|
The target data. NumPy array. Must be reshape-able to (-1, 1). |
required |
null_policy
|
NullPolicy
|
One of options shown here, but you can also pass in any numeric string. E.g you may pass '1.25' to mean fill nulls with 1.25. If the string cannot be converted to a float, an error will be thrown. Note: if the target column has null, the rows with nulls will always be dropped. Null-fill only applies to non-target columns. If target has null, then the row will still be dropped. |
'ignore'
|
Source code in python/polars_ds/linear_models.py
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 | |
fit_df(df, features, target, null_policy='skip', show_report=False)
Fit the GLM model on a dataframe. This will overwrite previously set feature names. The null policy only handles null values in df, not NaN values. It is the user's responsibility to handle NaN values if they exist in their pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
PolarsFrame
|
Either an eager or a lazy Polars dataframe. |
required |
features
|
List[str]
|
List of strings of column names. |
required |
target
|
str
|
The target column's name. |
required |
null_policy
|
NullPolicy
|
One of options shown here, but you can also pass in any numeric string. E.g you may pass '1.25' to mean fill nulls with 1.25. If the string cannot be converted to a float, an error will be thrown. Note: if the target column has null, the rows with nulls will always be dropped. Null-fill only applies to non-target columns. If target has null, then the row will still be dropped. |
'skip'
|
show_report
|
bool
|
Whether to print out a regression report. |
False
|
Source code in python/polars_ds/linear_models.py
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 | |
predict(X, linear=False)
Returns the prediction of this linear model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Data to predict on, as a matrix |
required |
linear
|
bool
|
If true, return the linear predictor eta instead of the expected value of the response variable, E[Y|X]. |
False
|
Source code in python/polars_ds/linear_models.py
910 911 912 913 914 915 916 917 918 919 920 921 922 923 | |
set_input_features(features)
Sets the names of input features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
List[str]
|
List of strings. |
required |
Source code in python/polars_ds/linear_models.py
823 824 825 826 827 828 829 830 831 832 833 834 | |
LR
Normal or Ridge Regression.
Methods:
| Name | Description |
|---|---|
__init__ |
Parameters |
coeffs |
Returns a copy of the coefficients. |
fit |
Fit the linear regression model on NumPy data. |
fit_df |
Fit the linear regression model on a dataframe. This will overwrite previously set feature names. |
from_values |
Constructs a LR class instance from coefficients and bias values. |
predict |
Returns the prediction of this linear model. |
predict_df |
Computes the prediction of the linear model and append it as a column in the dataframe. If input |
set_input_features |
Sets the names of input features. |
Source code in python/polars_ds/linear_models.py
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 | |
__init__(has_bias=False, lambda_=0.0, solver='qr', feature_names_in_=None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lambda_
|
float
|
The regularization parameters for ridge. If this is positive, then this class will solve Ridge. |
0.0
|
solver
|
LRSolverMethods
|
Use one of 'svd', 'cholesky' and 'qr' method to solve the least square equation. Default is 'qr'. |
'qr'
|
has_bias
|
bool
|
Whether to add a bias term. Also known as intercept in other packages. |
False
|
feature_names_in_
|
List[str] | None
|
Names for the incoming features, if available. If None, the names will be empty. They will be learned if .fit_df() is run later, or .set_input_features() is set later. |
None
|
Source code in python/polars_ds/linear_models.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
coeffs()
Returns a copy of the coefficients.
Source code in python/polars_ds/linear_models.py
220 221 222 223 224 | |
fit(X, y, null_policy='ignore')
Fit the linear regression model on NumPy data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
The feature Matrix. NumPy 2D matrix only. |
required |
y
|
ndarray
|
The target data. NumPy array. Must be reshape-able to (-1, 1). |
required |
null_policy
|
NullPolicy
|
One of options shown here, but you can also pass in any numeric string. E.g you may pass '1.25' to mean fill nulls with 1.25. If the string cannot be converted to a float, an error will be thrown. Note: if the target column has null, the rows with nulls will always be dropped. Null-fill only applies to non-target columns. If target has null, then the row will still be dropped. |
'ignore'
|
Source code in python/polars_ds/linear_models.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
fit_df(df, features, target, null_policy='skip', show_report=False)
Fit the linear regression model on a dataframe. This will overwrite previously set feature names. The null policy only handles null values in df, not NaN values. It is the user's responsibility to handle NaN values if they exist in their pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
PolarsFrame
|
Either an eager or a lazy Polars dataframe. |
required |
features
|
List[str]
|
List of strings of column names. |
required |
target
|
str
|
The target column's name. |
required |
null_policy
|
NullPolicy
|
One of options shown here, but you can also pass in any numeric string. E.g you may pass '1.25' to mean fill nulls with 1.25. If the string cannot be converted to a float, an error will be thrown. Note: if the target column has null, the rows with nulls will always be dropped. Null-fill only applies to non-target columns. If target has null, then the row will still be dropped. |
'skip'
|
show_report
|
bool
|
Whether to print out a regression report. This will duplicate work and will not work for Ridge regression. E.g. Nothing will be printed if lambda_ > 0. |
False
|
Source code in python/polars_ds/linear_models.py
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 | |
from_values(coeffs, bias=0.0, feature_names_in_=None)
classmethod
Constructs a LR class instance from coefficients and bias values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coeffs
|
List[float]
|
Iterable of numbers representing the coefficients |
required |
bias
|
float
|
Value for the bias term |
0.0
|
feature_names_in_
|
List[str] | None
|
Names for the incoming features, if available. If None, the names will be empty. They will be learned if .fit_df() is run later, or .set_input_features() is set later. |
None
|
Source code in python/polars_ds/linear_models.py
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 | |
predict(X)
Returns the prediction of this linear model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Data to predict on, as a matrix |
required |
Source code in python/polars_ds/linear_models.py
310 311 312 313 314 315 316 317 318 319 320 | |
predict_df(df, name='prediction')
Computes the prediction of the linear model and append it as a column in the dataframe. If input is lazy, output will be lazy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
PolarsFrame
|
Either an eager or a lazy Polars dataframe. |
required |
name
|
str
|
The name of the prediction column |
'prediction'
|
Source code in python/polars_ds/linear_models.py
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 | |
set_input_features(features)
Sets the names of input features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
List[str]
|
List of strings. |
required |
Source code in python/polars_ds/linear_models.py
207 208 209 210 211 212 213 214 215 216 217 218 | |
MixedModel
A random-intercept linear mixed model, fit via restricted maximum likelihood (REML).
The form is
y = X @ beta + Z @ u + e, u ~ N(0, sigma_g^2 I), e ~ N(0, sigma_e^2 I)
where X is the fixed-effect design matrix (an intercept column plus the given
features) and Z is the indicator (dummy) design matrix for group, i.e. one
random intercept per group level.
Degrees of freedom for each fixed effect are assigned by containment: effects that are constant within every group level (including the intercept) are tested against the group ("between") stratum, everything else against the residual ("within") stratum.
This implementation mirrors SAS's PROC MIXED for a random-intercept model.
Examples:
import polars as pl
from polars_ds.linear_models import MixedModel
df = pl.DataFrame(
{
"y": [...],
"x1": [...],
"school": [...],
}
)
mm = MixedModel().fit_df(df, features=["x1"], target="y", group="school")
print(mm.report())
Wikipedia: https://en.wikipedia.org/wiki/Mixed_model#Definition SAS PROC MIXED: https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/statug/statug_mixed_syntax01.htm
Methods:
| Name | Description |
|---|---|
fit_df |
Fit the random-intercept model on a dataframe. |
report |
Returns a Polars dataframe with one row per fixed effect (intercept first), |
Attributes:
| Name | Type | Description |
|---|---|---|
gamma_ |
float
|
Variance ratio sigma_g^2 / sigma_e^2 at the REML optimum. |
resid_variance_ |
float
|
Residual variance sigma_e^2. |
Source code in python/polars_ds/linear_models.py
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 | |
gamma_
property
Variance ratio sigma_g^2 / sigma_e^2 at the REML optimum.
resid_variance_
property
Residual variance sigma_e^2.
fit_df(df, features, target, group, null_policy='skip', max_iter=200, tol=1e-10)
Fit the random-intercept model on a dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
PolarsFrame
|
Frame. |
required |
features
|
List[str]
|
List of strings of fixed-effect column names. Intercept is added. |
required |
target
|
str
|
The target column name. |
required |
group
|
str
|
The column identifying the random-intercept grouping factor. |
required |
null_policy
|
NullPolicy
|
One of options shown here. |
'skip'
|
max_iter
|
int
|
Max number of iterations used for REML deviance over gamma. |
200
|
tol
|
float
|
Convergence tolerance for the search. |
1e-10
|
Source code in python/polars_ds/linear_models.py
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 | |
report()
Returns a Polars dataframe with one row per fixed effect (intercept first), containing the estimate, standard error, containment degrees of freedom, t-value and two-sided p-value.
Source code in python/polars_ds/linear_models.py
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 | |
OnlineLR
Normal or Ridge Online Regression. This doesn't support dataframe inputs.
Because of implementation details, it is not recommended to set has_bias = True here if runtime speed is crucial.
Null Behaviors: 1. During the initial fit, no nulls/NaNs should be present 2. During online updates, if the record has null/NaN, then it will be ignored. Nothing will be updated.
Methods:
| Name | Description |
|---|---|
__init__ |
lambda_ |
coeffs |
Returns a copy of the current coefficients. |
fit |
Initial Fit for the online linear regression model on NumPy data. |
from_coeffs_bias_inverse |
Constructs an online linear regression instance from coefficients, inverse. This copies |
inv |
Returns a copy of the current inverse matrix (inverse of XtX in a linear regression). |
predict |
Returns the prediction of this online linear model. |
update |
Updates the online linear regression model with one row of data. If the row contains np.nan, |
Source code in python/polars_ds/linear_models.py
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 | |
__init__(lambda_=0.0, has_bias=False)
lambda_ The L2 regularization factor has_bias Whether this should fit the bias term
Source code in python/polars_ds/linear_models.py
572 573 574 575 576 577 578 579 580 581 582 583 | |
coeffs()
Returns a copy of the current coefficients.
Source code in python/polars_ds/linear_models.py
622 623 624 625 626 | |
fit(X, y)
Initial Fit for the online linear regression model on NumPy data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
The feature Matrix. NumPy 2D matrix only. |
required |
y
|
ndarray
|
The target data. NumPy array. Must be reshape-able to (-1, 1). |
required |
Source code in python/polars_ds/linear_models.py
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 | |
from_coeffs_bias_inverse(coeffs, bias, inv)
classmethod
Constructs an online linear regression instance from coefficients, inverse. This copies data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coeffs
|
List[float]
|
Iterable of numbers representing the coefficients |
required |
bias
|
float
|
The bias term |
required |
inv
|
ndarray
|
2D NumPy matrix representing the inverse of XtX in a regression problem. |
required |
Source code in python/polars_ds/linear_models.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | |
inv()
Returns a copy of the current inverse matrix (inverse of XtX in a linear regression).
Source code in python/polars_ds/linear_models.py
631 632 633 634 635 | |
predict(X)
Returns the prediction of this online linear model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Data to predict on, as a matrix |
required |
Source code in python/polars_ds/linear_models.py
685 686 687 688 689 690 691 692 693 694 695 | |
update(X, y, c=1.0)
Updates the online linear regression model with one row of data. If the row contains np.nan, it will be ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Either a a 1d array or a 2d array with 1 row. Must be reshapeable to a matrix with 1 row. |
required |
y
|
ndarray | float
|
Either a scalar, or a 1d array with 1 element, or a 2d array of size 1x1. |
required |
c
|
float
|
The middle term (C) in the woodbury matrix identity. A value of 1.0 means we add
the impact of the new data, and a value of -1.0 means we remove the impact of the
data. Any other value will |
1.0
|
Source code in python/polars_ds/linear_models.py
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 | |