<jupyter_start><jupyter_text>$Linear Regression$理想的函数：$y = w * x + b$ 存在噪音：$y = w * x + b + \epsilon $$\epsilon \in N(0.01,1)$优化函数转化为：$loss = (WX + b - y)^2$<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import torch
import pandas as pd

%matplotlib inline
%config InlineBackend.figure_format = 'svg'
points = np.genfromtxt('./datasets/l3_data.csv',delimiter = ',')
plt.scatter(points[:,0],points[:,1],c = '',edgecolors = 'b',s = 15)<jupyter_output>/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ipykernel_launcher.py:2: MatplotlibDeprecationWarning: Using a string of single character colors as a color sequence is deprecated. Use an explicit list instead.<jupyter_text>$loss = (WX + b - y)^2 $<jupyter_code># y = wx + b
def compute_error_for_line_given_points(b, w, points):
    """
        计算给定超参数[w,b]的误差值
    """
    totalError = 0
    for i in range(0, len(points)):
        x = points[i, 0]
        y = points[i, 1]
        totalError += (y - (w * x + b)) ** 2
    return totalError / float(len(points))<jupyter_output><empty_output><jupyter_text>$b_{new} = b - lr  \frac{\Delta loss}{\Delta b} = b - lr\times 2(WX + b -y) $ $w_{new} = w - lr \frac{\Delta loss}{\Delta w} = w - lr\times 2(WX + b -y) X$<jupyter_code>def step_gradient(b, w, points, lr):
    b_gradient = 0
    w_gradient = 0
    N = float(len(points))
    for i in range(len(points)):
        x = points[i, 0]
        y = points[i, 1]
        b_gradient += -(2/N) * (y - ((w * x) + b))
        w_gradient += -(2/N) * x * (y - ((w * x) + b))
    b_new = b - (lr * b_gradient)
    w_new = w - (lr * w_gradient)
    return [b_new,w_new]
def gradient_descent_runner(points,b,w,lr,iterations):
    """
        梯度下降
    """
    for i in range(iterations):
        b,w = step_gradient(b,w,np.array(points),lr)
        
    return [b,w]
def run():
    points = np.genfromtxt('./datasets/l3_data.csv',delimiter = ',')    
    lr = 0.0001
    initial_b = 0
    initial_w = 0
    iterations = 1000
    print(f"Starting project descent at b = {initial_b}, w = {initial_w},error = {compute_error_for_line_given_points(initial_b,initial_w,points)}")
    print('\nRunning...')
    [b,w] = gradient_descent_runner(points,initial_b,initial_w,lr,iterations)
    print(f"\nAfter project descent at b = {b}, w = {w},error = {compute_error_for_line_given_points(b,w,points)}")
    print('\nb:{}，w:{}'.format(b, w))
if __name__ == '__main__':
    run()
x = points[:,0]
w = 1.4777440851894448
b = 0.08893651993741346
y = w * x + b

plt.scatter(points[:,0],points[:,1],c = '',edgecolors = 'b',s = 15,label = 'orginal')
plt.plot(x,y,c = 'black',label = 'predict',linestyle = ':')
plt.legend()<jupyter_output>/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ipykernel_launcher.py:6: MatplotlibDeprecationWarning: Using a string of single character colors as a color sequence is deprecated. Use an explicit list instead.
{'fraction_non_alphanumeric': 0.16813858695652173, 'fraction_numerical': 0.029211956521739132, 'mean_word_length': 3.623233908948195, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '617580', 'license': 'MIT', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1156, 'n_words': 246, 'path': 'Pytorch/Pytorch_learn_by_dragen1860/lesson03-线性回归问题.ipynb', 'repo_name': 'tsuirak/skills', 'size': '2944'}