导航
×
   ❮   
HTML CSS JavaScript PHP Python C C++ Go Sass W3C Colors TypeScript ECMS

Python 教程

Python 简介 Python 历史 Python 下载安装配置 Python 入门 Python 语法 Python 语句 Python 输出 Python 注释 Python 变量 Python 数据类型 Python 数值类型 Python 类型转换 Python 字符串 Python 布尔值 Python 运算符 Python 列表 Python 元组 Python 集合 Python 字典 Python If...Else Python While 循环 Python For 循环 Python 函数 Python Range Python 数组 Python 类和对象 Python 继承 Python 迭代 Python 模块 Python 日期时间 Python 数学运算 Python JSON Python 正则表达式 Python PIP Python Try...Except Python 用户输入 Python 字符串格式化

Python 文件处理

Python 文件处理 Python 打开文件 Python 创建/写入文件 Python 删除文件

Python NumPy

NumPy 简介 NumPy 入门 NumPy 创建数组 NumPy 数组索引 NumPy 数组裁切 NumPy 数据类型 NumPy 副本 vs 视图 NumPy 数组形状 NumPy 数组重塑 NumPy 数组迭代 NumPy 数组连接 NumPy 数组拆分 NumPy 数组搜索 NumPy 数组排序 NumPy 数组过滤 NumPy 随机数 NumPy ufunc 通用函数

Python SciPy

SciPy 简介 SciPy 入门 SciPy 常量 SciPy 优化器 SciPy 稀疏数据 SciPy 图表 SciPy 空间数据 SciPy Matlab 数组 SciPy 插值 SciPy 统计显着性检验

Python 机器学习

Machine 机器学习入门 Machine 平均中位数模式 Machine 标准差 Machine 百分位数 Machine 数据分布 Machine 正态数据分布 Machine 散点图 Machine 线性回归 Machine 多项式回归 Machine 多元回归 Machine 缩放 Machine 训练/测试 Machine 决策树

Python MySQL

MySQL 入门 MySQL Create Database MySQL Create Table MySQL Insert MySQL Select MySQL Where MySQL Order By MySQL Delete MySQL Drop Table MySQL Update MySQL Limit MySQL Join

Python MongoDB

MongoDB 入门 MongoDB 创建数据库 MongoDB 创建集合 MongoDB 插入 MongoDB 查找 MongoDB 查询 MongoDB 排序 MongoDB 删除 MongoDB 删除集合 MongoDB 更新 MongoDB 限制

Python 参考手册

Python 参考手册 Python 内置函数 Python 字符串方法 Python 列表/数组方法 Python 字典方法 Python 元组方法 Python 集合方法 Python 文件方法 Python 关键字 Python 内置异常 Python 词汇表
Python 词汇表 Indentation Multi Line Comments Creating Variables Variable Names Output Variables String Concatenation Global Variables Built-In Data Types Getting Data Type Setting Data Type Numbers Int Float Complex Random Number Specify a Variable Type String Literals Assigning a String to a Variable Multiline Strings Strings are Arrays Slicing a String Negative Indexing on a String String Length Check In String Format String Escape Characters Evaluate Booleans Return Boolean Value Comparison Operators Identity Operators Access List Items Change List Item Loop Through List Items Check if List Item Exists List Length Add List Items Remove List Items Copy a List Join Two Lists Access Tuple Items Change Tuple Item Loop List Items Check if Tuple Item Exists Tuple Length Tuple With One Item Remove Tuple Items Join Two Tuples 访问集合项 添加集合项目 Loop Set Items 检查集合项是否存在 集合长度 删除集合项 连接两个集合 Access Dictionary Items Change Dictionary Item Loop Dictionary Items Check if Dictionary Item Exists Dictionary Length Add Dictionary Item Remove Dictionary Items Copy Dictionary Nested Dictionaries If Statement If 缩进 Elif Else 简写 If Else If AND If OR 嵌套 If If中的pass关键字 While While Break While Continue While Else Loop Through a String For Break For Continue Looping Through a rangee For Else Nested Loops For pass Call a Function *args Keyword Arguments *kwargs Default Parameter Value Passing a List as an Argument Function Return Value The pass Statement i Functions Function Recursion Why Use Lambda Functions Array What is an Array Array Length Looping Array Elements Add Array Element Remove Array Element Array Methods Class Create Class The Class __init__() Function Object Methods self Modify Object Properties Delete Object Properties Delete Object Class pass Statement Create Parent Class Create Child Class Create the __init__() Function Add Class Properties Add Class Methods Iterator vs Iterable Loop Through an Iterator Create an Iterator StopIteration Global Scope Global Keyword Create a Module Variables in Modules Renaming a Module Built-in Modules Using the dir() Function Import From Module Date Output Create a Date Object The strftime Method Date Format Codes Parse JSON Convert into JSON Format JSON Sort JSON RegEx Functions Metacharacters in RegEx RegEx Special Sequences RegEx Sets RegEx Match Object Install PIP PIP Packages PIP Remove Package Error Handling Try Else Try Finally raise Comments Assign Values to Multiple Variables Type Conversion Boolean Values Operators Arithmetic Operators Assignment Operators Logical Operators 成员运算符 Bitwise Operators Lists Tuple 集合 Dictionary 简写 If For Function Function Arguments Lambda Function super Function Iterators Datetime Module JSON RegEx Module Handle Many Exceptions Access Arrays

Python 模块参考

Python 内置模块 Python random 随机模块 Python requests 请求模块 Python statistics 统计模块 Python math 数学模块 Python cMath 模块

Python 如何使用

Python 删除列表重复项 Python 反转字符串 Python 添加两个数字

Python 实例

Python 实例 Python 测验

机器学习 - 多元回归


多元回归(Multiple Regression)

多元回归就像线性回归一样,但是具有多个独立值,这意味着我们试图基于两个或多个变量来预测一个值。

请看下面的数据集,其中包含了一些有关汽车的信息。

Car Model Volume Weight CO2
Toyota Aygo 1000 790 99
Mitsubishi Space Star 1200 1160 95
Skoda Citigo 1000 929 95
Fiat 500 900 865 90
Mini Cooper 1500 1140 105
VW Up! 1000 929 105
Skoda Fabia 1400 1109 90
Mercedes A-Class 1500 1365 92
Ford Fiesta 1500 1112 98
Audi A1 1600 1150 99
Hyundai I20 1100 980 99
Suzuki Swift 1300 990 101
Ford Fiesta 1000 1112 99
Honda Civic 1600 1252 94
Hundai I30 1600 1326 97
Opel Astra 1600 1330 97
BMW 1 1600 1365 99
Mazda 3 2200 1280 104
Skoda Rapid 1600 1119 104
Ford Focus 2000 1328 105
Ford Mondeo 1600 1584 94
Opel Insignia 2000 1428 99
Mercedes C-Class 2100 1365 99
Skoda Octavia 1600 1415 99
Volvo S60 2000 1415 99
Mercedes CLA 1500 1465 102
Audi A4 2000 1490 104
Audi A6 2000 1725 114
Volvo V70 1600 1523 109
BMW 5 2000 1705 114
Mercedes E-Class 2100 1605 115
Volvo XC70 2000 1746 117
Ford B-Max 1600 1235 104
BMW 2 1600 1390 108
Opel Zafira 1600 1405 109
Mercedes SLK 2500 1395 120

我们可以根据发动机排量的大小预测汽车的二氧化碳排放量,但是通过多元回归,我们可以引入更多变量,例如汽车的重量,以使预测更加准确。


工作原理

在 Python 中,我们拥有可以完成这项工作的模块。首先导入 Pandas 模块:

import pandas

Pandas 模块允许我们读取 csv 文件并返回一个 DataFrame 对象。

此文件仅用于测试目的,您可以在此处下载: cars.csv

df = pandas.read_csv("cars.csv")

然后列出独立值,并将这个变量命名为 X。

将相关值放入名为 y 的变量中。

X = df[['Weight', 'Volume']]
y = df['CO2']

提示: 通常,将独立值列表命名为大写 X,将相关值列表命名为小写 y。

我们将使用 sklearn 模块中的一些方法,因此我们也必须导入该模块:

from sklearn import linear_model

在 sklearn 模块中,我们将使用 LinearRegression() 方法创建一个线性回归对象。

该对象有一个名为 fit() 的方法,该方法将独立值和从属值作为参数,并用描述这种关系的数据填充回归对象:

regr = linear_model.LinearRegression()
regr.fit(X, y)

现在,我们有了一个回归对象,可以根据汽车的重量和排量预测 CO2 值:

#predict the CO2 emission of a car where the weight is 2300kg, and the volume is 1300cm3:
predictedCO2 = regr.predict([[2300, 1300]])

实例

请看完整实例:

import pandas
from sklearn import linear_model

df = pandas.read_csv("cars.csv")

X = df[['Weight', 'Volume']]
y = df['CO2']

regr = linear_model.LinearRegression()
regr.fit(X, y)

#predict the CO2 emission of a car where the weight is 2300kg, and the volume is 1300cm3:
predictedCO2 = regr.predict([[2300, 1300]])

print(predictedCO2)

结果:

[107.2087328]

运行实例 »

我们预测,配备 1.3 升发动机,重量为 2300 千克的汽车,每行驶 1 公里,就会释放约 107 克二氧化碳。


系数

系数是描述与未知变量的关系的因子。

例如:如果 x 是变量,则 2x 是 x 的两倍。x 是未知变量,数字 2 是系数。

在这种情况下,我们可以要求重量相对于 CO2 的系数值,以及体积相对于 CO2 的系数值。我们得到的答案告诉我们,如果我们增加或减少其中一个独立值,将会发生什么。

实例

打印回归对象的系数值:

import pandas
from sklearn import linear_model

df = pandas.read_csv("cars.csv")

X = df[['Weight', 'Volume']]
y = df['CO2']

regr = linear_model.LinearRegression()
regr.fit(X, y)

print(regr.coef_)

结果:

[0.00755095 0.00780526]

运行实例 »

结果解释

结果数组表示重量和排量的系数值。

Weight: 0.00755095
Volume: 0.00780526

这些值告诉我们,如果重量增加 1g,则 CO2 排放量将增加 0.00755095g。

如果发动机尺寸(容积)增加 1 ccm,则 CO2 排放量将增加 0.00780526g。

我认为这是一个合理的猜测,但还是请进行测试!

我们已经预言过,如果一辆配备 1300ccm 发动机的汽车重 2300 千克,则二氧化碳排放量将约为 107 克。

如果我们增加 1000g 的重量会怎样?

实例

复制之前的例子,但是将车重从 2300 更改为 3300:

import pandas
from sklearn import linear_model

df = pandas.read_csv("cars.csv")

X = df[['Weight', 'Volume']]
y = df['CO2']

regr = linear_model.LinearRegression()
regr.fit(X, y)

predictedCO2 = regr.predict([[3300, 1300]])

print(predictedCO2)

结果:

[114.75968007]

运行实例 »

我们已经预测,配备 1.3 升发动机,重量为 3.3 吨的汽车,每行驶 1 公里,就会释放约 115 克二氧化碳。

这表明 0.00755095 的系数是正确的:

107.2087328 + (1000 * 0.00755095) = 114.75968


Copyright ©2026 蒙ICP备2026009383号 All Rights Reserved 提供的内容仅用于学习和测试,不保证内容的正确性。