1.1 常用线性模型
1.1.1 线性回归,LinearRegression
>>> from sklearn import linear_model
>>> reg = linear_model.LinearRegression()
>>> reg.fit ([[0, 0], [1, 1], [2, 2]], [0, 1, 2])
...
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
normalize=False)
>>> reg.coef_
array([0.5, 0.5])
1.1.2 脊回归,Ridge Regression
>>> from sklearn import linear_model
>>> reg = linear_model.Ridge (alpha = .5)
>>> reg.fit ([[0, 0], [0, 0], [1, 1]], [0, .1, 1])
Ridge(alpha=0.5, copy_X=True, fit_intercept=True, max_iter=None,
normalize=False, random_state=None, solver='auto', tol=0.001)
>>> reg.coef_
array([0.34545455, 0.34545455])
>>> reg.intercept_
0.13636...
1.1.3 套索回归, Lasso
>>> from sklearn import linear_model
>>> reg = linear_model.Lasso(alpha = 0.1)
>>> reg.fit([[0, 0], [1, 1]], [0, 1])
Lasso(alpha=0.1, copy_X=True, fit_intercept=True, max_iter=1000,
normalize=False, positive=False, precompute=False, random_state=None,
selection='cyclic', tol=0.0001, warm_start=False)
>>> reg.predict([[1, 1]])
array([0.8])
1.1.4. Multi-task Lasso
MultiTaskLasso
1.1.5. Elastic Net
ElasticNet
1.1.6. Multi-task Elastic Net
MultiTaskElasticNet
1.1.7. Least Angle Regression
Lars
lars_path
1.1.8. LARS Lasso
>>> from sklearn import linear_model
>>> reg = linear_model.LassoLars(alpha=.1)
>>> reg.fit([[0, 0], [1, 1]], [0, 1])
LassoLars(alpha=0.1, copy_X=True, eps=..., fit_intercept=True,
fit_path=True, max_iter=500, normalize=True, positive=False,
precompute='auto', verbose=False)
>>> reg.coef_
array([0.717157..., 0. ])
1.1.9. Orthogonal Matching Pursuit (OMP)
OrthogonalMatchingPursuit
orthogonal_mp
1.1.10. Bayesian Regression
BayesianRidge
ARDRegression
1.1.11. 逻辑回归,Logistic regression
LogisticRegression
1.1.12. Stochastic Gradient Descent - SGD
SGDClassifier
SGDRegressor
1.1.13. Perceptron
Perceptron
1.1.14. Passive Aggressive Algorithms
PassiveAggressiveClassifier
PassiveAggressiveRegressor
1.1.15. Robustness regression: outliers and modeling errors
TheilSenRegressor
HuberRegressor
1.1.16. Polynomial regression: extending linear models with basis functions
>>> from sklearn.preprocessing import PolynomialFeatures
>>> import numpy as np
>>> X = np.arange(6).reshape(3, 2)
>>> X
array([[0, 1],
[2, 3],
[4, 5]])
>>> poly = PolynomialFeatures(degree=2)
>>> poly.fit_transform(X)
array([[ 1., 0., 1., 0., 0., 1.],
[ 1., 2., 3., 4., 6., 9.],
[ 1., 4., 5., 16., 20., 25.]])