1.  普通线性回归

from sklearn import linear_model
reg = linear_model.LinearRegression()
reg.fit([[0, 0], [1, 1], [2, 2]], [0, 1, 2])
print(reg.coef_)import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_scoreX, y = datasets.load_diabetes(return_X_y=True)
# X,y 为数组类型
print(X.shape)   # (442, 10)
print(y.shape)   # (442,)
# 只用一列(特征)做演示
X = X[:, np.newaxis, 2]  # X[:, 2] 只有一维
print(X)
print(X.shape)
# 划分数据集,后20个用作测试
X_train = X[:-20]
X_test = X[-20:]
y_train = y[:-20]
y_test = y[-20:]# Create linear regression object
reg = linear_model.LinearRegression()#可以将所有系数限制为非负系数,当它们表示一些物理或自然非负数量(例如,频率计数或商品价格)时,这可能很有用。
#LinearRegression(positive=True)# Train the model using the training sets
reg.fit(X_train, y_train)# Make predictions using the testing set
y_pred = reg.predict(X_test)# The coefficients
print("Coefficients: \n", reg.coef_)
# The mean squared error
print("Mean squared error: %.2f" % mean_squared_error(y_test, y_pred))
print("Coefficient of determination: %.2f" % r2_score(y_test, y_pred))
# MSE和R方值是回归模型的评价指标
# Plot outputs
plt.scatter(X_test, y_test, color="black")
plt.plot(X_test, y_pred, color="blue", linewidth=3)# x,y 轴不显示label
plt.xticks(())
plt.yticks(())plt.title("regression plot")
plt.show()

2. Ridge回归

from sklearn import linear_model
reg = linear_model.Ridge(alpha=.5)
reg.fit([[0, 0], [0, 0], [1, 1]], [0, .1, 1])
print(reg.coef_)
print(reg.intercept_)

3. Lasso 回归

from sklearn import linear_model
reg = linear_model.Lasso(alpha=0.1)
reg.fit([[0, 0], [1, 1]], [0, 1])
print(reg)
print(reg.predict([[1, 1]]))

4. 弹性网回归

## Elastic Net
# 参数:alpha (α) and l1_ratio (ρ)
from sklearn import linear_model
enet = linear_model.ElasticNet(alpha=0.1, l1_ratio=0.7)
enet.fit([[0, 0], [0, 0], [1, 1]], [0, .1, 1])
print(enet)
print(enet.predict([[1, 1]]))

5. 多任务 Lasso回归

## Multi-task Lasso
# 多任务Lasso, Y是一个二维形状数组(n_个样本,n_个任务)
from sklearn import linear_model
clf = linear_model.MultiTaskLasso(alpha=0.1)
clf.fit([[0, 1], [1, 2], [2, 4]], [[0, 0], [1, 1], [2, 3]])
print(clf.coef_)
print(clf.intercept_)

6. 多任务弹性网回归

## Multi-task Elastic-Net
# 多任务弹性网模型用于联合估计多元回归问题的稀疏系数:Y是一个二维形状数组(n_个样本,n_个任务)。
from sklearn import linear_model
clf = linear_model.MultiTaskElasticNet(alpha=0.1)
clf.fit([[0, 0], [1, 1], [2, 2]], [[0, 0], [1, 1], [2, 2]])
print(clf.coef_)
print(clf.intercept_)

7. 最小角度Lasso回归

## LARS Lasso
# 最小角度回归(LARS)是一种针对高维数据的回归算法from sklearn import linear_model
reg = linear_model.LassoLars(alpha=.1, normalize=False)
reg.fit([[0, 0], [1, 1]], [0, 1])
print(reg.coef_)

8. OMP回归

## Orthogonal Matching Pursuit (OMP)
from sklearn.linear_model import OrthogonalMatchingPursuitomp = OrthogonalMatchingPursuit(n_nonzero_coefs=1, normalize=False)
omp.fit([[0, 0], [1, 1]], [0, 1])
print(omp.coef_)

9. Bayesian回归

## Bayesian Ridge Regression
# The prior: w - spherical Gaussian distribution
from sklearn import linear_modelX = [[0., 0.], [1., 1.], [2., 2.], [3., 3.]]
Y = [0., 1., 2., 3.]
reg = linear_model.BayesianRidge()
reg.fit(X, Y)
print(reg.predict([[1, 0.]]))## Automatic Relevance Determination (ARD) regression
# ARD回归与贝叶斯岭回归非常相似,但会导致系数更稀疏w
# The prior: w - axis-parallel, elliptical Gaussian distribution
from sklearn.linear_model import ARDRegression
clf = ARDRegression()
clf.fit([[0, 0], [1, 1]], [0, 1])
print(clf.coef_)

10. 广义线性回归

## 广义线性回归
# 1.有连接函数 2. 优化目标mse被分布偏差取代# Tweedie distribution
from sklearn.linear_model import TweedieRegressor
reg = TweedieRegressor(power=1, alpha=0.5, link='log')
reg.fit([[0, 0], [0, 1], [2, 2]], [0, 1, 2])
print(reg.coef_)
print(reg.intercept_)# Poisson distribution
from sklearn import linear_model
clf = linear_model.PoissonRegressor()
X = [[1, 2], [2, 3], [3, 4], [4, 3]]
y = [12, 17, 22, 21]
clf.fit(X, y)
print(clf.score(X, y))
print(clf.coef_)
print(clf.intercept_)
print(clf.predict([[1, 1], [3, 4]]))

11.其他

## 更多线性模型,查看:from sklearn import linear_model
print(dir(linear_model))

参考: https://scikit-learn.org/stable/modules/linear_model.html#perceptron

python库scikit-learn线性回归模型代码相关推荐

  1. python开源代码-这7个开源的Python库,让你轻松代码分析

    原标题:这7个开源的Python库,让你轻松代码分析 开源最前线(ID:OpenSourceTop) 猿妹编译 来源:https://opensource.com/article/18/7/7-pyt ...

  2. 【scikit-learn】如何用Python和SciKit Learn 0.18实现神经网络

    本教程的代码和数据来自于 Springboard 的博客教程.本文的作者为 Jose Portilla,他是网络教育平台 Udemy 一门数据科学类课程的讲师. GitHub 链接:https://g ...

  3. 使用Python+Pandas+Statsmodels建立线性回归模型预测房价

    [综述] 本文通过使用Python+Pandas+Statsmodels建立简单一元线性回归模型.多元线性回归模型来预测房价. 主要内容来源自网页:https://www.learndatasci.c ...

  4. 吴恩达-机器学习-多元线性回归模型代码

    吴恩达<机器学习>2022版 第一节第二周 多元线性回归 房价预测简单实现         以下以下共两个实验,都是通过调用sklearn函数,分别实现了 一元线性回归和多元线性回归的房价 ...

  5. 机器学习与Scikit Learn学习库

    摘要: 本文介绍机器学习相关的学习库Scikit Learn,包含其安装及具体识别手写体数字案例,适合机器学习初学者入门Scikit Learn. 在我科研的时候,机器学习(ML)是计算机科学领域中最 ...

  6. python代码库-吐血整理!绝不能错过的24个Python库

    Python有以下三个特点: 易用性和灵活性 全行业高接受度:Python无疑是业界最流行的数据科学语言 用于数据科学的Python库的数量优势 事实上,由于Python库种类很多,要跟上其发展速度非 ...

  7. python回归模型_Python实现线性回归模型

    从简单的线性回归模型中可以看到构建一个监督学习网络的基本步骤.下文摘自<动手学深度学习> 线性回归概念 线性回归输出是一个连续值,因此适用于回归问题.回归问题在实际中很常见,如预测房屋价格 ...

  8. 懒人必备!只需一行代码,就能导入所有的Python库

    今天给大家介绍一个懒人Python库--Pyforest. 使用一行代码,就能导入所有的Python库(本地已经安装的). GitHub地址:https://github.com/8080labs/p ...

  9. python预测实例教程_手把手教你用Python库Keras做预测(附代码)-阿里云开发者社区...

    当你在Keras中选择好最合适的深度学习模型,就可以用它在新的数据实例上做预测了.但是很多初学者不知道该怎样做好这一点,我经常能看到下面这样的问题: "我应该如何用Keras对我的模型作出预 ...

最新文章

  1. _declspec(naked) 使用(裸函数)
  2. 你的gpu驱动程序不满足_英特尔图形驱动程序现在在Windows/Linux之间共享约60%的代码库...
  3. 一步步使用SAP CRM Application Enhancement Tool创建扩展字段
  4. 青岛外卖小哥帮崩溃程序员写代码,网友看完破防了:佩服又感动。
  5. VBA FSO 对象模型知识点梳理
  6. 区块链网络安全平台Hapi Protocol将在Poolz上进行 IDO
  7. 如何使用AdminStudio软件把.exe转换成.msi
  8. C# winform界面上传附件到服务器(springboot)
  9. 【AMESim】AMESim和Simulink联合仿真步骤
  10. Java 有序的Map —— LinkedHashMap
  11. 【HTML5】Web前端——制作 3D 旋转魔方相册
  12. 如何解决github的code按钮一直转下载不了
  13. SQL语句(查询、新建表、删除表、更新表、新建视图)
  14. 1060显卡支持dx12吗_GTX1660和1060差距大吗?GTX1660和1060区别对比
  15. 最详细的原版win8系统安装指南,重装原版系统不再求人!
  16. 安卓webview中键盘遮挡输入框如何解决
  17. GOOGLE搜索秘籍
  18. 10.HTML基础——表格标签
  19. python安装绘图库_python绘图库Matplotlib的安装
  20. html中段落标志中标注文件子标题的是,Macromedia网页设计认证部分试题

热门文章

  1. pandas object转float_数据分析篇 | Pandas基础用法6【完结篇】
  2. 03Template Method模式
  3. R语言中if else、which、%in%的用法
  4. python调用webservice接口实例_python发布webservice接口
  5. OpenCV 4.5.4 刚刚发布!新增SoftNMS、DNN模型8位量化等功能
  6. 清华计算机系上热搜!近 9 成优秀毕业生放弃留学,前 50 名 41 人留校深造......
  7. TensorFlow中Session.run和Tensor.eval的区别
  8. 人工智能(AI)遇上仿制药
  9. HTMD | 从PDB文件获取3D特征描述符
  10. 基于RDKit的Python脚本:SDF格式转SMILES格式