简单线性回归实现

import numpy as np


class SimpleLinearRegression:

  def __init__(self):
    '''初始话Simple Linear Regression模型'''
    self.a_ = None
    self.b_ = None

  def fit(self, x_train, y_train):
    '''根据训练集x_train,y_train 训练模型'''

    assert x_train.ndim == 1, \
      "simple linear regression can only solve single feature traininng data"
    assert len(x_train) == len(y_train), \
      "the size of x_train must be equal to the size of y_train"

    # 求x_train,y_train的均值
    x_mean = np.mean(x_train)
    y_mean = np.mean(y_train)

    # 分子
    num = 0.0
    # 分母
    d = 0.0

    # 采用向量化点乘的计算
    num = (x_train - x_mean).dot(y_train - y_mean)
    d = (x_train - x_mean).dot(x_train - x_mean)

    # 使用for循环计算效率比较低
    # for x_i, y_i in zip(x_train, y_train):
    #   num += (x_train - x_mean) * (y_train - y_mean)
    #   d += (x_train - x_mean) ** 2

    self.a_ = num / d
    self.b_ = y_mean - self.a_ * x_mean
    return self

  def predict(self, x_predict):
    '''给定带预测数据集x_predict,返回表示x_predict的结果向量'''
    assert x_predict.ndim == 1, \
      "simple linear regression can only solve single feature traininng data"
    assert self.a_ is not None and self.b_ is not None, \
      "must fit before predict"

    return np.array([self._predict(x) for x in x_predict])

  def _predict(self, x_single):
    '''给定单个待预测数据x_single,返回预测结果'''
    return self.a_ * x_single + self.b_

  def score(self,x_test,y_test):
    '''根据测试集确定模型准确度 ,返回R平方'''
    y_predict = self._predict(x_test)

    return 1 - (np.sum((y_predict - y_test)**2) / len(y_test)) / np.var(y_test)


  def __repr__(self):
    return 'simple linear regression'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值