Python机器学习:线型回归法008实现多元线性回归

使用封装的:LinearRegression

import numpy as np
from Simple_linear_Regression.metrics import r2_score
class LinearRegression:

    def __init__(self):
        """初始化Linear Regression 模型"""
        self.coef_ = None
        self.interception_ = None
        self._theta = None
    def fit_normal(self,X_train,y_train):
        """根据训练数据集X_train,y_train训练Linear Regression"""
        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.hstack([np.ones((X_train.shape[0],1)),X_train])
        #X_b = np.hstack([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._theta[1:]

        return self

    def predict(self,X_predict):
        """给定待测数据集X_predict,返回表示X_predict 的结果向量"""
        assert self.interception_ is not None and self.coef_ is not None,\
            "must fit before predict"
        assert X_predict.shape[1] == len(self.coef_),\
            "the feature number of X_predict must be equal to X_train"
        X_b = np.hstack([np.ones((X_predict.shape[0], 1)), X_predict])

        return X_b.dot(self._theta)

    def score(self,X_test,y_test):
        """根据测试数据集X_test和y_test确定当前模型的准确度"""

        y_predict = self.predict(X_test)

        return r2_score(y_test,y_predict)

    def __repr__(self):
        return "LinearRegression()"
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
依然房价数据
boston = datasets.load_boston()
X = boston.data
y = boston.target

X = X[y < 50]
y = y[y < 50]
from Simple_linear_Regression.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,seed = 666)
LinearRegression()
from Simple_linear_Regression.LinearRegression import LinearRegression
reg = LinearRegression()
reg.fit_normal(X_train,y_train)

截距和参数

print(reg.interception_)
print(reg.coef_)
34.117399723204585
[-1.20354261e-01  3.64423279e-02 -3.61493155e-02  5.12978140e-02
 -1.15775825e+01  3.42740062e+00 -2.32311760e-02 -1.19487594e+00
  2.60101728e-01 -1.40219119e-02 -8.35430488e-01  7.80472852e-03
 -3.80923751e-01]

与一元线性模型比较,预测准确率上升了

reg.score(X_test,y_test)

0.8129794056212895

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