📜  使用 Turicreate 进行多项式回归

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

使用 Turicreate 进行多项式回归

在本文中,我们将讨论使用 Turicreate 实现多项式回归。多项式回归:多项式回归是回归分析的一种形式,它将因变量y和自变量x之间的关系建模为n 次多项式。它表示为:

所以让我们通过实践来学习这个概念。

第 1 步:导入重要的库并在 turicreate 中使用 SArray 和 SFrame 生成一个非常小的数据集,我们将使用它们来执行多项式回归。

Python3
# importing required python libraries
import turicreate
import matplotlib.pyplot as plt
import random
  
# Generating datapoints
X = [data for data in range(1, 21)]
Y = [random.randrange(100, 1000, 1) for data in range(20)]
  
# Creating Sarrays from the generated data points
Xs = turicreate.SArray(X, dtype=float)
Ys = turicreate.SArray(Y, dtype=float)
  
print(f"""Xs : {Xs}
\n-------------------------------------------------------------------------------------------\n
Ys : {Ys}""")


Python3
# plotting the generated data
plt.scatter(Xs, Ys)
plt.show()


Python3
# Creating an Sframe where all the inputs and the polynomial degree and output
def createSframe(inputs, pol_degree):
    datapoints = turicreate.SFrame({'x1': inputs})
    for degree in range(2, pol_degree+1):
        datapoints[f'x{degree}'] = datapoints[f'x{degree-1}']*datapoints['x1']
    return datapoints
  
  
# Creating a SFrame with polynomial degree 20
data_points = createSframe(Xs, 20)
data_points['y'] = Ys
  
# showing the first 10 entries in the SFrame
data_points.head()


Python3
# Polynomial Regression
features = [f'x{i}' for i in range(1, 21)]
poly_model = turicreate.linear_regression.create(
    data_points, features=features, target='y')


Python3
# predicting the some data
# Generating test datapoints
test_X = [random.randrange(1, 60, 1) for data in range(20)]
test_Xs = turicreate.SArray(X, dtype=float)
test_data = createSframe(test_Xs, 5)
data_points['predicted_y'] = poly_model.predict(test_data)
  
data_points.head()


Python
# Measuring the accuracy
# Generating test datapoints
test_X = [random.randrange(1, 60, 1) for data in range(20)]
test_Xs = turicreate.SArray(X, dtype=float)
test_data = createSframe(test_Xs, 20)
poly_model.evaluate(data_points)


Python3
plt.scatter(data_points['x1'], data_points['y'])
plt.plot(data_points['x1'], data_points['predicted_y'])
plt.show()


输出:

第 2 步:绘制生成的数据

蟒蛇3

# plotting the generated data
plt.scatter(Xs, Ys)
plt.show()

第 3 步:创建一个包含输入、其 polynomial_degrees 和输出的 SFrame 以拟合我们的回归模型。

蟒蛇3

# Creating an Sframe where all the inputs and the polynomial degree and output
def createSframe(inputs, pol_degree):
    datapoints = turicreate.SFrame({'x1': inputs})
    for degree in range(2, pol_degree+1):
        datapoints[f'x{degree}'] = datapoints[f'x{degree-1}']*datapoints['x1']
    return datapoints
  
  
# Creating a SFrame with polynomial degree 20
data_points = createSframe(Xs, 20)
data_points['y'] = Ys
  
# showing the first 10 entries in the SFrame
data_points.head()

步骤 4:将多项式回归拟合到生成的数据集。

蟒蛇3

# Polynomial Regression
features = [f'x{i}' for i in range(1, 21)]
poly_model = turicreate.linear_regression.create(
    data_points, features=features, target='y')

步骤 5:使用拟合模型预测结果并将结果存储在 SFrame 中。

蟒蛇3

# predicting the some data
# Generating test datapoints
test_X = [random.randrange(1, 60, 1) for data in range(20)]
test_Xs = turicreate.SArray(X, dtype=float)
test_data = createSframe(test_Xs, 5)
data_points['predicted_y'] = poly_model.predict(test_data)
  
data_points.head()

第 6 步:测量我们预测结果的准确性

Python

# Measuring the accuracy
# Generating test datapoints
test_X = [random.randrange(1, 60, 1) for data in range(20)]
test_Xs = turicreate.SArray(X, dtype=float)
test_data = createSframe(test_Xs, 20)
poly_model.evaluate(data_points)

步骤 7:使用输入数据和预测结果的散点图和线图可视化多项式回归结果。

蟒蛇3

plt.scatter(data_points['x1'], data_points['y'])
plt.plot(data_points['x1'], data_points['predicted_y'])
plt.show()