线性回归代码:
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 19 23:19:49 2019
@author: 86135
"""
import numpy as np
import matplotlib.pyplot as plt
class LinearRegression():
def_init_(self):
self._coef_=None
self.interception_=None
self._theta=None
def fit_normal(self,x_train,y_train):
assert x_train.shape[0]==y_train.shape[0],\
"the size of x_train must be equal to the size of y_train"
x_b=np.hatack([np.ones((len(x_train),1)),x_train])
self.theta=np.linalg.inv(x_b.T.dot(x_b)).dot(x_b.T).dot(y_train);
self.interception_=self.theta[0]
self.coef_=self._thta[1:]
return self
def predict(self,x_predict):
assert self.self.interception_is not None and self.coef is not None,\
"must fit before predict !"
assert x_predi