首先先安装要用到的包:sklearn,顾名思义机器学习包

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets, linear_model
data = pd.read_csv('C://Users//leon//Desktop//CCPP.csv') #导入数据
data.head()
data.shape
X = data[['AT', 'V', 'AP', 'RH']] #用AT, V,AP和RH这4个列作为样本特征
y = data[['PE']]
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
print (X_train.shape)
print (y_train.shape)
print (X_test.shape)
print (y_test.shape) #训练
from sklearn.linear_model import LinearRegression
linreg = LinearRegression() #建立模型
linreg.fit(X_train, y_train)
print (linreg.intercept_) #输出模型常量
print (linreg.coef_) #自变量系数
y_pred = linreg.predict(X_test)
from sklearn import metrics
print ("MSE:",metrics.mean_squared_error(y_test, y_pred)) # 通过MSE值进行模型检验
from sklearn.model_selection import cross_val_predict
predicted = cross_val_predict(linreg, X, y, cv=10)
fig, ax = plt.subplots()
ax.scatter(y, predicted)
ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
plt.show() #作图观察

通过训练数据集进行预测

python--线性回归的更多相关文章

  1. Python 线性回归(Linear Regression) 基本理解

    背景 学习 Linear Regression in Python – Real Python,对线性回归理论上的理解做个回顾,文章是前天读完,今天凭着记忆和理解写一遍,再回温更正. 线性回归(Lin ...

  2. python 线性回归示例

    说明:此文的第一部分参考了这里 用python进行线性回归分析非常方便,有现成的库可以使用比如:numpy.linalog.lstsq例子.scipy.stats.linregress例子.panda ...

  3. Python 线性回归(Linear Regression) - 到底什么是 regression?

    背景 学习 Linear Regression in Python – Real Python,对 regression 一词比较疑惑. 这个 linear Regression 中的 Regress ...

  4. Python - 线性回归(Linear Regression) 的 Python 实现

    背景 学习 Linear Regression in Python – Real Python,前面几篇文章分别讲了"regression怎么理解","线性回归怎么理解& ...

  5. python线性回归

    一.理论基础 1.回归公式 对于单元的线性回归,我们有:f(x) = kx + b 的方程(k代表权重,b代表截距). 对于多元线性回归,我们有: 或者为了简化,干脆将b视为k0·x0,,其中k0为1 ...

  6. 机器学习之路: python线性回归 过拟合 L1与L2正则化

    git:https://github.com/linyi0604/MachineLearning 正则化: 提高模型在未知数据上的泛化能力 避免参数过拟合正则化常用的方法: 在目标函数上增加对参数的惩 ...

  7. 机器学习之路: python 线性回归LinearRegression, 随机参数回归SGDRegressor 预测波士顿房价

    python3学习使用api 线性回归,和 随机参数回归 git: https://github.com/linyi0604/MachineLearning from sklearn.datasets ...

  8. 机器学习之路:python线性回归分类器 LogisticRegression SGDClassifier 进行良恶性肿瘤分类预测

    使用python3 学习了线性回归的api 分别使用逻辑斯蒂回归  和   随机参数估计回归 对良恶性肿瘤进行预测 我把数据集下载到了本地,可以来我的git下载源代码和数据集:https://gith ...

  9. Python线性回归算法【解析解,sklearn机器学习库】

    一.概述 参考博客:https://www.cnblogs.com/yszd/p/8529704.html 二.代码实现[解析解] import numpy as np import matplotl ...

  10. ml的线性回归应用(python语言)

    线性回归的模型是:y=theta0*x+theta1   其中theta0,theta1是我们希望得到的系数和截距. 下面是代码实例: 1. 用自定义数据来看看格式: # -*- coding:utf ...

随机推荐

  1. Python实现图片重命名——实用

    1 # encoding: utf-8 2 import os 3 import sys 4 5 import openpyxl 6 import tkinter as tk 7 from tkint ...

  2. SVN的安装和使用手册2

    转载:http://www.cnblogs.com/armyfai/p/3985660.html SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需 ...

  3. git提交到代码到远程仓库,合并分支提示entirely different commit histories(备忘)

    最近提交代码到github,合并分支的时候提示"master and main are entirely different commit histories" master为本地 ...

  4. JavaScript 类(class)

    JavaScript 类(class) 类是用于创建对象的模板. 我们使用 class 关键字来创建一个类,类体在一对大括号 {} 中,我们可以在大括号 {} 中定义类成员的位置,如方法或构造函数. ...

  5. 设置导航栏的title

    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAnd ...

  6. vue 中使用 XLSX 和 xlsx-style 实现前端下载Excel表格

    import XLSX from "xlsx-style"  //ps  需要修改源码:在\node_modules\xlsx-style\dist\cpexcel.js 807行 ...

  7. sequlizejs学习笔记整理

    1.事务 try { const transaction = await sequelize.transaction(); const user = await User.findOne(..., { ...

  8. Spring 自定义注解 操作日志

    1.自定义注解 package com.jay.demo3.aop1.myannotation;       import java.lang.annotation.Documented;   imp ...

  9. js防止表单重复方法

    用flag标识,下面的代码设置checkSubmitFlg标志: <script language=""javascript""> var chec ...

  10. RealSenseD435的一些参数整理

    一.深度相机D435 二.自校准功能 https://www.intelrealsense.com/self-calibration-for-depth-cameras/ https://dev.in ...