一元线性回归

import numpy as np
'''一元(简单)线性回归,形如y=a*x+b'''
class SimpleLinearRegression:
    def __init__(self):
        """初始化Simple linear regression模型"""
        self.a_ = None
        self.b_ = None
    def fit(self,x_train,y_train):
        assert x_train.ndim == 1, \
        "一元线性回归模型仅处理矢量,而不能处理矩阵"
        x_mean = np.mean(x_train)
        y_mean = np.mean(y_train)
        denominator = 0.0
        numerator = 0.0
        #按照最小二乘法得到a和b
        for x_i, y_i in zip(x_train, y_train):
            numerator += (x_i - x_mean) * (y_i - y_mean) 
            denominator += (x_i - x_mean) ** 2 
        #得到a
        self.a_ = numerator / denominator 
        #得到b
        self.b_ = y_mean - self.a_ * x_mean 
        return self

    #对于输入矢量集合中的每一个矢量都进行一次预测,预测的具体实现被封装在_predict函数中。
    def predict(self,x_test_group):
        return np.array([self._predict(x_test) for x_test in x_test_group])
    def _predict(self,x_test):
        return self.a_ * x_test + self.b_ #求取每一个输入的x_test以得到预测值的具体实现
    def mean_squared_error(self,y_true,y_predict):
        return np.sum((y_true - y_predict) ** 2) / len(y_true)
    def r_square(self,y_true,y_predict):
        return 1 - (self.mean_squared_error(y_true,y_predict) / np.var(y_true))

if __name__ == '__main__':
    x = np.array([1, 2, 4, 6, 8])
    y = np.array([2, 5, 7, 8, 9])
    lr = SimpleLinearRegression()
    lr.fit(x,y)
    print(lr.predict([7])) 
    print(lr.r_square([8,9],lr.predict([6,8]))) 
posted @   YI颗白菜  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示