LinearRegression fits a linear model with coefficients  to minimize the residual sum of squares between the observed responses in the dataset, and the responses predicted by the linear approximation. Mathematically it solves a problem of the form:

原理最小化:   

 
>>> from sklearn import linear_model
>>> clf = linear_model.LinearRegression()
>>> clf.fit ([[, ], [, ], [, ]], [, , ])
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=, normalize=False)
>>> clf.coef_
array([ 0.5, 0.5])

完整代理例子

#!/usr/bin/env python
# coding=utf-8 import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets,linear_model print(__doc__) # load dataset
diabetes = datasets.load_diabetes() # use only one feature
diabetes_x = diabetes.data[:,np.newaxis]
diabetes_x_temp = diabetes_x[:,:,2] # split data into training/testing sets
diabetes_x_train = diabetes_x_temp[:-20]
diabetes_x_test = diabetes_x_temp[-20:] # split the targets into training/testing sets
diabetes_y_train = diabetes.target[:-20]
diabetes_y_test = diabetes.target[-20:] # create linear regression object
regr = linear_model.LinearRegression()
regr.fit(diabetes_x_train,diabetes_y_train) # the coefficients
print('coefficients: \n ',regr.coef_) # the mean square error
print("Residual sum of squares:%.2f" % np.mean((regr.predict(diabetes_x_test)-diabetes_y_test)**2)) # Plot outputs
plt.scatter(diabetes_x_test,diabetes_y_test,color='black')
plt.plot(diabetes_x_test,regr.predict(diabetes_x_test),color='blue',linewidth=3) plt.title("linear_model example")
plt.xlabel("X")
plt.ylabel("Y")
# plt.xticks(())
# plt.yticks(()) plt.show()

转自:

http://scikit-learn.org/dev/auto_examples/linear_model/plot_ols.html#example-linear-model-plot-ols-py

sklearn 线性模型使用入门的更多相关文章

  1. sklearn.linear_model.LinearRegresion学习

    sklearn线性模型之线性回归 查看官网 https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearR ...

  2. Kaggle实战之二分类问题

    0. 前言 1. MNIST 数据集 2. 二分类器 3. 效果评测 4. 多分类器与误差分析 5. Kaggle 实战 0. 前言 "尽管新技术新算法层出不穷,但是掌握好基础算法就能解决手 ...

  3. Kaggle实战分类问题2

    Kaggle实战之二分类问题 0. 前言 1. MNIST 数据集 2. 二分类器 3. 效果评测 4. 多分类器与误差分析 5. Kaggle 实战 0. 前言 “尽管新技术新算法层出不穷,但是掌握 ...

  4. python常用库 - NumPy 和 sklearn入门

    Numpy 和 scikit-learn 都是python常用的第三方库.numpy库可以用来存储和处理大型矩阵,并且在一定程度上弥补了python在运算效率上的不足,正是因为numpy的存在使得py ...

  5. sklearn 快速入门教程

    1. 获取数据 1.1 导入sklearn数据集 sklearn中包含了大量的优质的数据集,在你学习机器学习的过程中,你可以通过使用这些数据集实现出不同的模型,从而提高你的动手实践能力,同时这个过程也 ...

  6. tensorflow实现线性模型和sklearn的线性模型比较

    自己用tensorflow实现了linear模型,但是和sklearn提供的模型效果相比,实验结果差了很多,所以尝试了修改优化算法,正则化,损失函数和归一化,记录尝试的所有过程和自己的实验心得. im ...

  7. 数据挖掘入门系列教程(九)之基于sklearn的SVM使用

    目录 介绍 基于SVM对MINIST数据集进行分类 使用SVM SVM分析垃圾邮件 加载数据集 分词 构建词云 构建数据集 进行训练 交叉验证 炼丹术 总结 参考 介绍 在上一篇博客:数据挖掘入门系列 ...

  8. sklearn机器学习算法--线性模型

    线性模型 用于回归的线性模型 线性回归(普通最小二乘法) 岭回归 lasso 用于分类的线性模型 用于多分类的线性模型 1.线性回归 LinearRegression,模型简单,不同调节参数 #2.导 ...

  9. 机器学习入门之sklearn介绍

    SKlearn简介 scikit-learn,又写作sklearn,是一个开源的基于python语言的机器学习工具包.它通过NumPy, SciPy和Matplotlib等python数值计算的库实现 ...

随机推荐

  1. anu - children

    import { _flattenChildren } from "./createElement"; export const Children = { only(childre ...

  2. 想ACCESS数据库插入新的用户

    public string AddUserN = ""; //定义用户名字符串 public string paswrd1 = ""; //密码1 public ...

  3. linux fdisk tf卡分区操作解析说明

    /***************************************************************************** * linux fdisk tf卡分区操作 ...

  4. .Protobuf,GRpc,Maven项目出现UnsatisfiedDependencyException、ClassNotFoundException、BuilderException等异常

    异常如下: Error starting ApplicationContext. To display the auto-configuration report re-run your applic ...

  5. [LeetCode&Python] Problem 496. Next Greater Element I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  6. 安卓手机文件管理器简单横向评比 - imsoft.cnblogs

      X-plore文件管理器 个人评价:安卓手机上管理文件的神器,所有文件一览无余,加上自己对软件常用功能的配置,管理文件无比方便.(本人一直使用)   Solid文件管理器 个人评价:用户体验真的很 ...

  7. 度限制最小生成树 POJ 1639 贪心+DFS+prim

    很好的解题报告: http://blog.csdn.net/new_c_yuer/article/details/6365689 注意两点: 1.预处理环中权值最大的边···· 2.可以把去掉度限制后 ...

  8. map和jsonObject 这2中数据结构之间转换

    前台写json直接是:var array = [ ] ; 调用方法:array[index],若是对象,再[“key”] var obj = {''a'':123 , "b":&q ...

  9. benthos 通过rest api 配置 stream 说明

    stream 模式,我们可以通过rest api 进行控制 使用方法 启动 benthos --streams 进行流的配置(rest api) curl http://localhost:4195/ ...

  10. 使用js 文件参数 以及IHttpModule实现服务验证asp.net 版的初步实现

    接上面的文章,上面的主要是进行html 页面自己进行处理.但是对于进行asp.net 的开发者以及其他的就显的不太好了. 我的实现方式是使用IHttpModule 进行对于用户请求的带有参数的js文件 ...