📜  使用 Turicreate 进行线性回归

📅  最后修改于: 2022-05-13 01:55:36.237000             🧑  作者: Mango

使用 Turicreate 进行线性回归

线性回归是监督学习的一种方法或途径。监督学习采用历史或过去的数据,然后训练模型并根据过去的结果预测事物。线性回归来自“线性”和“回归”这个词。回归概念处理使用过去的数据预测未来。线性意味着能够在图形上用直线表示。线性回归有两个东西一个自变量和另一个因变量,线性回归是两者之间的关系。

在本文中,我们将了解如何在 Turicreate 的帮助下实现线性回归。 Turicreate 是Python中的库,可帮助初学者轻松高效地学习和实现机器学习算法。

第 1 步:导入 Turicreate 库

Python3
import turicreate as tc


Python3
"""
The good thing about Turicreate is that we don't have
import any other library for data loading.Turicreate
itself can load data with the help of it's SFrame
Data Structure
"""
data_sets = tc.SFrame("data.csv")


Python3
# It will display the first few Lines of the data
data.head()


Python3
# We will have a target variable that stores the thing
# to the predicted and feature is the list of elements
# which we will take for making the model.
model = tc.linear_regression.create(
    data, target ="charges", features =['region'])


Python3
# this will tell us about the max error and
# the rmse (root mean squared error)
model.evaluate(data)


Python3
# this variable will be containing the data
# of person having the bmi 27.9
bmi_person = data[data['bmi']== 27.9]
# it will predict the charges for the person with bmi 27.9
model.predict(bmi_person)


第 2 步:读取数据集。

Python3

"""
The good thing about Turicreate is that we don't have
import any other library for data loading.Turicreate
itself can load data with the help of it's SFrame
Data Structure
"""
data_sets = tc.SFrame("data.csv")

数据链接是=https://www.kaggle.com/mirichoi0218/insurance
第 3 步:探索数据

Python3

# It will display the first few Lines of the data
data.head()

输出:

数据集前几行

第 4 步:制作线性回归模型。

Python3

# We will have a target variable that stores the thing
# to the predicted and feature is the list of elements
# which we will take for making the model.
model = tc.linear_regression.create(
    data, target ="charges", features =['region'])

第 5 步:现在评估模型

Python3

# this will tell us about the max error and
# the rmse (root mean squared error)
model.evaluate(data)

输出:

最大误差和均方根值

第 6 步:现在根据人的 BMI 预测费用

Python3

# this variable will be containing the data
# of person having the bmi 27.9
bmi_person = data[data['bmi']== 27.9]
# it will predict the charges for the person with bmi 27.9
model.predict(bmi_person)

输出:

费用预测