1、线性回归API
from sklearn.linear_model import LinearRegression
model = LinearRegression()
# 使用fit方法进行训练
model.fit(x,y)
model.coef_ #回归系数
model.intercept_
model.predict(x)
2、scikit-learn中的MSE和MAE
from sklearn.metrics import mean_squared_error #均方误差
from sklearn.metrics import mean_absolute_error #平均绝对误差
mean_squared_error(y_test, y_predict)
mean_absolute_error(y_test, y_predict)
3、多项式回归
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree=2)
poly.fit(X)
X2 = poly.transform(X)
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X2, y)
y_predict = lin_reg2.predict(X2)
4、岭回归
l2正则化;倾向于抑制权重
from sklearn.linear_model import Ridge
ridge1_reg = RidgeRegression(alpha=alpha) #alpha:正则化力度
ridge1_reg.fit(X_train, y_train)
y_predict = ridge1_reg.predict(X_test)
5、Lasso回归
l1正则化;倾向于完全消除不重要的权重
from sklearn.linear_model import Lasso
lasso_reg = Lasso()
lasso_reg.fit(X_train, y_train)
y_predict = lasso_reg.predict(X_test)
6、弹性网络
L1,L2结合
from sklearn.linear_model import ElasticNet
7、SGDRegressor
梯度下降法,只适用与线性模型
from sklearn.linear_model import SGDRegressor