当前位置:   article > 正文

线性回归分析——含python代码_利用线性回归对数据进行分类 python代码

利用线性回归对数据进行分类 python代码

  假设样本空间为d个维度,用x={x1,x2,x3,...,xd}来表示一个样本点,线性回归的目标是用d+1个参数来拟合样本点的输入和输出。通常我们会将x扩充为d+1维的向量x={x0,x1,x2,x3,...,xd},第x0设为1作为偏置项。线性回归表达式如下:

f=i=0d+1θixi

用向量可以表示为:
f(x)=θTx

将表达式代入均方误差函数 (MSE)中:
J=1Ni=1N(yif(xi))2

令全部样本表示为: X={x1,x2,x3,...,xN} T,对应输出表示为Y={y1,y2,y3,...,yN}T,损失函数可以简化为:
J=1N||XθY||2

我们的目标是让损失函数最小,令其对θ的偏导数为0,则有:
Jθ=θ[1N(θTXTXθ2θTXTY+YTY)]=1N(2XTXθ2XTY)=0

解得:θ=(XTX)1XTY
  显然,XTX要是可逆的,通常情况下都能满足因为N>>d+1。事实上XTX也存在不可逆的情况,这种情况下我们可以选择求伪逆或者梯度下降法来解决问题。
  如果要利用线性回归来解决二分类问题,yi就不再是连续值,而是1(正样本)或者0/-1(负样本)。同样的求出权重向量后对测试样本进行预测,可以用0.5/0作为阈值来划分正负样本。

代码块

理解了线性回归和梯度下降的基本原理,用python撸出来也就10分钟的时间:

import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import scale
from random import random
from sklearn.model_selection import train_test_split

class LinearRegression(object):
    weight = np.array([])
    way = 'gd'
    def __init__(self, training_way = 'gd'):
        self.way = training_way
    def gradientDescent(self, X, Y, alpha, epoch):
        W = np.random.normal(0,1,size=(X.shape[1],))
        for i in range(epoch):
            W -= alpha*(X.T).dot(X.dot(W)-Y)/X.shape[0]
        return W

    def fit(self, train_data, train_target, alpha = 0.1, epoch = 300):
        X = np.ones((train_data.shape[0], train_data.shape[1]+1))
        X[:,0:-1] = train_data
        Y = train_target
        if self.way == 'gd':
            self.weight = self.gradientDescent(X, Y, alpha, epoch)
        else:
            self.weight = np.linalg.inv((X.T).dot(X)).dot(X.T).dot(Y)

    def predict(self, test_data):
        X = np.ones((test_data.shape[0], test_data.shape[1]+1))
        X[:,0:-1] = test_data
        return X.dot(self.weight)

    def evaluate(self, predict_target, test_target):
        predict_target[predict_target>=0.5] = 1
        predict_target[predict_target<0.5] = 0
        return sum(predict_target==test_target)/len(predict_target)

if __name__ == "__main__":
    cancer = load_breast_cancer()
    xtr, xval, ytr, yval = train_test_split(cancer.data, cancer.target, \
    test_size=0.2, random_state=7)
    linear = LinearRegression(training_way = 'gd')
    linear.fit(xtr, ytr, alpha = 0.05, epoch = 1000)
    predict = linear.predict(xval)
    print('linear regression accruacy:',linear.evaluate(predict, yval))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/705991
推荐阅读
相关标签
  

闽ICP备14008679号