多项式线性回归总结

这里主要记录多项式线性回归,会附带一部分简单线性回归的讲解

线性回归其实是典型的参数训练的机器学习方法,而且专门解决回归问题

首先先讲下简单线性回归

简单线性回归

函数表达式

y=ax+b

其中

多项式线性回归总结_第1张图片

代码实现

import numpy as np
import matplotlib.pyplot as plt


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

    #模型训练
    def fit(self, x, y):
        assert x.ndim == 1, "illegle x"
        assert len(x) == len(y), "this size of x_train must be equal to the size of y_train"
        num = 0
        d = 0
        # 用for循环耗时长,需要消耗大量的计算资源
        # for x_i, y_i in zip(x, y):
        #     num += (x_i - np.mean(x))*(y_i - np.mean(y))
        #     d += (x_i - np.mean(x))**2
        # 用向量点乘的方式可以让运算时长变短
        num = (x-np.mean(x)).dot(y-np.mean(y))
        d = (x-np.mean(x)).dot(x-np.mean(x))

        self.a_ = num/d
        self.b_ = np.mean(y) - self.a_* np.mean(x)

        return self

    #数据预测
    def predict(self, x):
        assert x.ndim == 1, "illegle x"
        assert self.a_ is not None and self.b_ is not None, "must fit before predict"
        y_predict = [self.__predict(x_i) for x_i in x]
        return y_predict

    #私有函数,单个数据的预测
    def __predict(self, x):
        return self.a_* x + self.b_

    def __repr__(self):
        print("this is the SimpleLinearRegression")

多项式回归函数

函数表达式

多项式线性回归总结_第2张图片

整理一下变成如下

多项式线性回归总结_第3张图片

其中

多项式线性回归总结_第4张图片

代码如下

import numpy as np
from .Mertristics import Mertristics
class LinearRegression:
    def __init__(self):
        self.coef_ = None
        self.interception_ = None
        self.theta_ = None

    #训练模型
    def fit(self, x_trains, y_trains):
        assert x_trains.shape[0] == len(y_trains), \
            "the size of x_trains must be equal to the size of y_trians"

        X_b = np.hstack([np.ones([len(x_trains), 1]), x_trains])
        self.theta_ = ((np.linalg.inv(X_b.T.dot(X_b))).dot(X_b.T)).dot(y_trains)
        self.coef_ = self.theta_[0]
        self.interception_ = self.theta_[1:]
        return self

    #预测数据
    def predict(self, x_test):
        assert self.coef_ is not None and self.interception_ is not None, \
            "do predict must to do fit first"

        X_b = np.hstack([np.ones([len(x_test), 1]), x_test])
        return X_b.dot(self.theta_)

    def score(self, x_test, y_test):
        y_predict = self.predict(x_test)
        #return y_predict
        metristics = Mertristics()
        return metristics.r2_score(y_test, y_predict)

    def __repr__(self):
        return "LinearRegression"

关于多项式线性方程,我发现有个blog记录的笔记不错,转载一下,blog地址:https://blog.csdn.net/ayangann915/article/details/81184398

多项式线性回归总结_第5张图片

 

你可能感兴趣的:(python,机器学习)