欢迎访问宙启技术站
智能推送

使用Python函数实现基本机器学习算法:线性回归

发布时间:2023-06-22 11:46:01

机器学习是一种广泛应用于各行各业的人工智能技术,其中线性回归是一种最基本也是最常用的模型之一。可以使用Python编写一个函数,实现线性回归算法,并用于数据集的预测。

首先,我们需要加载所需的Python库,包括numpy和matplotlib。然后,我们定义一个函数,名为linear_regression,该函数使用numpy库中的线性代数函数和计算函数来实现线性回归算法。函数需要三个参数:x-axis数据,y-axis数据和学习率。x-axis是指输入的自变量,y-axis是指输出的因变量。

以下是实现线性回归的代码:

import numpy as np
import matplotlib.pyplot as plt

def linear_regression(x, y, learn_rate):
    # Initialize parameters to be learned
    m = 0.0
    c = 0.0
    # Calculate number of data points
    n = float(len(x))
    # Set up array for errors
    errors = []
    # Set up iteration counter
    iterations = 1000
    # Begin gradient descent algorithm
    for i in range(iterations):
        # Calculate predicted y values
        y_pred = m * x + c
        # Calculate error between predicted and true y values
        error = y_pred - y
        # Calculate mean squared error
        mean_squared_error = np.mean(error**2)
        # Append mean squared error to errors list
        errors.append(mean_squared_error)
        # Calculate partial derivatives of cost with respect to m and c
        pd_m = (2/n) * np.sum(error*x)
        pd_c = (2/n) * np.sum(error)
        # Update m and c values
        m = m - learn_rate * pd_m
        c = c - learn_rate * pd_c
    # Plot error over time
    plt.plot(errors)
    plt.title("Error over Time")
    plt.xlabel("Iteration")
    plt.ylabel("Mean Squared Error")
    plt.show()
    # Return final m and c values
    return m, c

我们可以使用以下方式调用该函数:

x = np.array([1, 2, 3, 4, 5])
y = np.array([3, 4, 5, 6, 7])
m, c = linear_regression(x, y, 0.1)
print("Slope (m):", m, "Intercept (c):", c)

该代码将x,y数据作为输入,并将学习率设置为0.1。在函数中,我们将初始斜率和截距设置为0,并将迭代次数设置为1000次。我们计算预测的y值,然后计算误差,用梯度下降法来优化斜率和截距。每次迭代后,我们计算平均平方误差并将其附加到错误列表中,然后绘制错误随时间变化的图表。

在完成所有迭代之后,函数将输出最终值m和c,并将绘制误差图表。

此外,我们可以用这个函数预测新数据点的值:

y_pred = m * 6 + c
print("Predicted value of y:", y_pred)

这将输出预测的y值,给定x=6。

总之,通过使用Python中的线性代数函数,我们可以轻松地实现线性回归算法。通过预测数据集中的值,可以使用这个函数来实现各种实际应用,例如数据分析、机器人控制和金融预测。