【机器学习】【线性回归】scipy最小二乘法求解线性回归的最优解

1拟合h(x) = w0 + w1*x

话不多说,直接上code

1.1代码

# -*- coding: utf-8 -*-
"""
@author: 蔚蓝的天空TOM
Talk is cheap, show me the code
Aim:最小二乘法库函数leastsq使用示例详解
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq

#hypothesis function
def hypothesis_func(w, x):
    w1,w0 = w
    return w1*x + w0
    
#error function
def error_func(w, train_x, train_y, msg):
    print(msg)
    return hypothesis_func(w, train_x) - train_y

def dump_fit_func(w_fit):
    w1,w0=w_fit
    print("fitting line=",str(w1)+"*x + "+str(w0))
    return
    
#square error平方差函数
def dump_fit_cost(w_fit, train_x, train_y):
    error = error_func(w_fit, train_x, train_y, "")
    square_error = sum(e*e for e in error)
    print('fitting cost:',str(square_error))
    return square_error
    
#main function
if __name__=="__main__":
    #train set
    train_x = np.array([8.19,2.72,6.39,8.71,4.7,2.66,3.78])
    train_y = np.array([7.01,2.78,6.47,6.71,4.1,4.23,4.05])

    #linear regression by leastsq
    msg = "invoke scipy leastsq"
    w_init = [20, 1]#weight factor init
    fit_ret = leastsq(error_func, w_init, args=(train_x, train_y, msg))
    w_fit = fit_ret[0]

    #dump fit result
    dump_fit_func(w_fit)
    fit_cost = dump_fit_cost(w_fit, train_x, train_y)
    
    #test set
    test_x = np.array(np.arange(train_x.min(), train_x.max(), 1.0))
    test_y = hypothesis_func(w_fit, test_x)

    #show result by figure
    plt.figure(1)
    plt.figure(figsize=(8,6))#指定图像比例: 8:6
    plt.title('linear regression by scipy leastsq')
    plt.scatter(train_x, train_y, color='b', label='train set')
    plt.scatter(test_x, test_y, color='r', marker = '^', label='test set', linewidth=2)
    plt.plot(test_x, test_y, color='r', label='fitting line')
    plt.legend(loc='lower right')#label面板放到figure的右下角
    
    plt.show()
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值