python初学者编程指南
Machine Learning is making the computer learn from studying data and statistics. Machine Learning is a step into the direction of artificial intelligence (AI). Machine Learning is a program that analyses data and learns to predict the outcome.
机器学习使计算机从研究数据和统计信息中学习。 机器学习是迈向人工智能(AI)方向的一步。 机器学习是一个程序,可以分析数据并学习预测结果。
什么是回归? (What is Regression?)
The term regression is used when you try to find the relationship between variables. In Machine Learning and statistical modelling, that relationship is used to predict the outcome of future events.
当您尝试查找变量之间的关系时,将使用术语回归。 在机器学习和统计建模中,该关系用于预测未来事件的结果。
Multiple Regression
多重回归
Multiple regression is like linear regression, but with more than one independent value, meaning that we try to predict a value based on two or more variables.
多元回归就像线性回归一样,但是具有多个独立值,这意味着我们试图基于两个或多个变量来预测一个值。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/46d802737b5a0b0513554698d483d968.png)
We can predict the CO2 emission of a car based on the size of the engine, but with multiple regression we can throw in more variables, like the weight of the car, to make the prediction more accurate.
我们可以根据发动机的大小预测汽车的二氧化碳排放量,但是通过多元回归,我们可以引入更多变量,例如汽车的重量,以使预测更加准确。
入门 (Getting Started)
In Python we have modules that will do the work for us. Start by importing the Pandas module.
在Python中,我们有可以为我们完成工作的模块。 首先导入Pandas模块。
import pandas
The Pandas module allows us to read csv files and return a DataFrame object.
Pandas模块允许我们读取csv文件并返回一个DataFrame对象。
df = pandas.read_csv("cars.csv")
Then make a list of the independent values and call this variable x. Put the dependent values in a variable called y.
然后列出独立值,并将其称为变量x。 将相关值放入名为y的变量中。
X = df[['Weight', 'Volume']]
y = df['CO2']
We will use some methods from the sklearn module, so we will have to import that module as well:
我们将使用sklearn模块中的一些方法,因此我们也必须导入该模块:
from sklearn import linear_model
From the sklearn module we will use the ‘LinearRegression’ method to create a linear regression object.
在sklearn模块中,我们将使用“ LinearRegression”方法创建线性回归对象。
regr = linear_model.LinearRegression()
regr.fit(X, y)
Now we have a regression object that are ready to predict CO2 values based on a car’s weight and volume:
现在我们有了一个回归对象,可以根据汽车的重量和体积来预测CO2值:
predictedCO2 = regr.predict([[2300, 1300]])
Full Code Example
完整代码示例
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 1300ccm:
predictedCO2 = regr.predict([[2300, 1300]])
print(predictedCO2)
结论 (Conclusion)
I hope after this article you have a basic understanding of Mulitple Regression and how to use it in Python and that you will be able to run yourself some scripts of Machine Learning now!
我希望本文之后您对Mulitple Regression有基本的了解,以及如何在Python中使用它,并且现在您可以运行一些机器学习脚本!
Resources: W3 Schools.
资源:W3学校。
翻译自: https://medium.com/swlh/the-ultimate-beginners-guide-to-multiple-regression-in-python-3373527cb30b
python初学者编程指南