• Python:实现linear regression线性回归算法(附完整源码)


    Python:实现linear regression线性回归算法

    
    import numpy as np
    import requests
    
    
    def collect_dataset():
        """Collect dataset of CSGO
        The dataset contains ADR vs Rating of a Player
        :return : dataset obtained from the link, as matrix
        """
        response = requests.get(
            "https://"
            + "The_Math_of_Intelligence/master/Week1/ADRvs"
            + "Rating.csv"
        )
        lines = response.text.splitlines()
        data = []
        for item in lines:
            item = item.split(",")
            data.append(item)
        data.pop(0)  # This is for removing the labels from the list
        dataset = np.matrix(data)
        return dataset
    
    
    def run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta):
      
        n = len_data
    
        prod = np.dot(theta, data_x.transpose())
        prod -= data_y.transpose()
        sum_grad = np.dot(prod, data_x)
        theta = theta - (alpha / n) * sum_grad
        return theta
    
    
    def sum_of_square_error(data_x, data_y, len_data, theta):
       
        prod = np.dot(theta, data_x.transpose())
        prod -= data_y.transpose()
        sum_elem = np.sum(np.square(prod))
        error = sum_elem / (2 * len_data)
        return error
    
    
    def run_linear_regression(data_x, data_y):
    
        iterations = 100000
        alpha = 0.0001550
    
        no_features = data_x.shape[1]
        len_data = data_x.shape[0] - 1
    
        theta = np.zeros((1, no_features))
    
        for i in range(0, iterations):
            theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta)
            error = sum_of_square_error(data_x, data_y, len_data, theta)
            print("At Iteration %d - Error is %.5f " % (i + 1, error))
    
        return theta
    
    
    def main():
        """Driver function"""
        data = collect_dataset()
    
        len_data = data.shape[0]
        data_x = np.c_[np.ones(len_data), data[:, :-1]].astype(float)
        data_y = data[:, -1].astype(float)
    
        theta = run_linear_regression(data_x, data_y)
        len_result = theta.shape[1]
        print("Resultant Feature vector : ")
        for i in range(0, len_result):
            print(f"{theta[0, i]:.5f}")
    
    
    if __name__ == "__main__":
        main()
    
    
    • 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
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
  • 相关阅读:
    pycharm使用运行Docker容器的python解释器
    HTTPS的工作过程
    一道python难题5
    nginx配置dav上传+展示页面
    160. 相交链表
    java基础 --泛型
    17条好用的 Python 技巧分享
    ISPE GAMP5 中文版
    Go-Excelize API源码阅读(三十九)——SetCellHyperLink
    软件测试需求分析
  • 原文地址:https://blog.csdn.net/it_xiangqiang/article/details/126062967