linear_regression.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:astrology 作者: mattsgithub 项目源码 文件源码
def train(self, X, y, **kwargs):
        features = kwargs.get('features')
        self.fit_intercept = kwargs.get('fit_intercept')

        self.y = y
        self.N = y.shape[0]
        # Ignore column vector
        if self.fit_intercept:
            self.features = ['bias'] + features
            X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)
            self.p = X.shape[1] - (1 if self.fit_intercept else 0)
        else:
            self.features = features
            self.p = X.shape[1]

        self.X = X
        XT = X.T
        std_error_matrix = inv(XT.dot(X))
        self.beta = std_error_matrix.dot(XT).dot(y)

        # Prediction
        self.y_hat = X.dot(self.beta)

        # Residual sum of squares
        self.rss = np.sum((y - self.y_hat)**2)

        # Estimated variance
        self.df = (self.N - self.p - 1)
        self.pop_var = self.rss / self.df

        # Standard error
        self.std_error = np.sqrt(std_error_matrix.diagonal() * self.pop_var)

        # t scores
        self.t = self.beta / self.std_error
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号