LR回购demo(2)

import numpy as np

class SimpleLinearRegression1:
    def __init__(self):
        self.a_=None
        self.b_=None

    def fit(self,x_train,y_train):
        x_mean = np.mean(x_train)
        y_mean = np.mean(y_train)
        num = 0.0
        d = 0.0
        for x_i, y_i in zip(x_train, y_train):
            num += (x_i - x_mean) * (y_i - y_mean)
            d += (x_i - x_mean) ** 2

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

    def predict(self,x_predict):

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

    def _predict(self,x_single):
        return self.a_ * x_single+self.b_

    def __repr__(self):
        return "SimpleLinearRegression1"
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import SimpleLinearRegression1

x = np.array([1,2,3,4,5])
y = np.array([1,3,2,3,5])

reg1 = SimpleLinearRegression1.SimpleLinearRegression1()
reg1.fit(x,y)
print(reg1.predict(np.array([6])))

在这里插入图片描述

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页