回归算法
1. 最小二乘回归
from sklearn.linear_model import LinearRegression
lg = LinearRegression()
'''
__init__参数
def __init__(self, fit_intercept=True, normalize=False, copy_X=True,
n_jobs=1):
fit_intercept,是否存在截距,默认存在
normalize,标准化开关,默认关闭
用法:
lg.fit(X_train, y_train)
y_pred = lg.predict(X_test)
lg.score(y_pred, y_test)
'''
2. 岭回归
from sklearn.linear_model import Ridge
clf = Ridge()
'''
__init__函数
def __init__(self, alpha=1.0, fit_intercept=True, normalize=False,
copy_X=True, max_iter=None, tol=1e-3, solver="auto",
random_state=None):
alpha,两项之间的权重;
fit_intercept,默认为true,数据可以拦截,没有中心化
normalize,输入的样本特征归一化,默认false
copy_X,复制或者重写
max_iter,最大迭代次数
tol,控制求解的精度
solver,求解器
用法:
clf.fit(X, y),输入训练样本数据X,和对应的标记y
clf.predict(X),利用学习好的线性分类器,预测标记,一般在fit之后调用;
clf.corf_,回归系数
clf.intercept_,截距
'''
3. 核岭回归
from sklearn.kernel_ridge import KernelRidge
clf = KernelRidge()
'''
__init__函数
def __init__(self, alpha=1, kernel="linear", gamma=None, degree=3, coef0=1,
kernel_params=None):
kernel,核的类型
gamma,rbf,laplacian,poly,chi2,sigmoid核中的参数,使用其他核时无效。
degree,poly核中的参数d,使用其他核时无效
coef0,poly和sigmoid核中的0参数的替代值,使用其他核时无效。
属性:
dual_coef_,核空间对应的模型参数
X_fit_,训练数据,预测时也需要该值
'''
4. 套索回归
from sklearn.linear_model import Lasso
lasso = Lasso()
'''
__init__函数
def __init__(self, alpha=1.0, fit_intercept=True, normalize=False,
precompute=False, copy_X=True, max_iter=1000,
tol=1e-4, warm_start=False, positive=False,
random_state=None, selection='cyclic'):
alpha,可选,默认 1.0。当 alpha 为 0 时算法等同于普通最小二乘法,可通过 Linear Regression 实现,因此不建议将 alpha 设为 0.
fit_intercept,是否进行拦截计算
max_iter,最大循环次数。
tol,优化容忍度 The tolerance for the optimization
warm_start,为 True 时, 重复使用上一次学习作为初始化,否则直接清除上次方案
positive,设为 True 时,强制使系数为正。
selection,若设为 ‘random’, 每次循环会随机更新参数,而按照默认设置则会依次更新
'''
5. 弹性网络回归
from sklearn.linear_model import ElasticNet
etn = ElasticNet()
'''
__init__函数
def __init__(self, alpha=1.0, l1_ratio=0.5, fit_intercept=True,
normalize=False, precompute=False, max_iter=1000,
copy_X=True, tol=1e-4, warm_start=False, positive=False,
random_state=None, selection='cyclic'):
'''
6. 多项式回归
from sklearn.preprocessing import PolynomialFeatures
pnf = PolynomialFeatures()
'''
__init__函数
def __init__(self, degree=2, interaction_only=False, include_bias=True):
degree,控制多项式的度
interaction_only,默认为False,如果指定为True,那么就不会有特征自己和自己结合的项
include_bias,是否包含截距,默认为True。
'''
7. 偏最小二乘回归
from sklearn.cross_decomposition import PLSRegression
plsr = PLSRegression()
'''
__init__函数
def __init__(self, n_components=2, scale=True,
max_iter=500, tol=1e-06, copy=True):
n_components,要保留的主成分数量,默认为2
scale,是否归一化数据,默认为是
max_iter,使用NIPALS时的最大迭代次数
tol,迭代截止条件
'''
8. 典型相关分析
from sklearn.cross_decomposition import CCA
cca = CCA()
'''
__init__函数
def __init__(self, n_components=2, scale=True,
max_iter=500, tol=1e-06, copy=True):
'''
9. 稳健回归
from sklearn.linear_model import RANSACRegressor
rsg = RANSACRegressor()
'''
__init__函数
def __init__(self, base_estimator=None, min_samples=None,
residual_threshold=None, is_data_valid=None,
is_model_valid=None, max_trials=100, max_skips=np.inf,
stop_n_inliers=np.inf, stop_score=np.inf,
stop_probability=0.99, residual_metric=None,
loss='absolute_loss', random_state=None):
'''
10. 支持向量机回归
from sklearn.svm import SVR
svr = SVR()
'''
__init__函数
def __init__(self, kernel='rbf', degree=3, gamma='auto', coef0=0.0,
tol=1e-3, C=1.0, epsilon=0.1, shrinking=True,
cache_size=200, verbose=False, max_iter=-1):
C,惩罚系数
'''
11. 贝叶斯回归
from sklearn.linear_model import BayesianRidge
bys = BayesianRidge()
'''
__init__函数
def __init__(self, n_iter=300, tol=1.e-3, alpha_1=1.e-6, alpha_2=1.e-6,
lambda_1=1.e-6, lambda_2=1.e-6, compute_score=False,
fit_intercept=True, normalize=False, copy_X=True,
verbose=False):
'''
网友评论