首页 > 其他分享 >线性回归

线性回归

时间:2022-09-29 20:11:28浏览次数:40  
标签:plt 回归 np cost 线性 theta array data

线性回归

导入库

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

人工数据集

'''
n = 100
true_theta = np.array([[1], [1]])
X = np.insert(np.random.normal(5, 1, size=(n, 1)), 0, 1, 1)
y = X @ true_theta + np.random.normal(0, 0.04, size=(n, 1))
X,y
plt.scatter(X[:,1],y)
'''
'\nn = 100\ntrue_theta = np.array([[1], [1]])\nX = np.insert(np.random.normal(5, 1, size=(n, 1)), 0, 1, 1)\ny = X @ true_theta + np.random.normal(0, 0.04, size=(n, 1))\nX,y\nplt.scatter(X[:,1],y)\n'

导入ML-ex1data1数据


data = pd.read_csv("ex1data1.txt", names=['x','y'])
data.insert(0, "Ones", 1)
n = data.shape[0]
m = data.shape[1]-1
X = np.array(data.iloc[:,:-1])
y = np.array(data.y).reshape(-1,1)
data
plt.scatter(X[:,-1],y)

<matplotlib.collections.PathCollection at 0x7f3d028c97e0>

image

导入ML-ex1data2数据

'''
data = pd.read_csv("ex1data2.txt", names=['x1', 'x2', 'y'])
data.insert(0, "Ones", 1)
n = data.shape[0]
m = data.shape[1]-1
X = np.array(data.iloc[:,:-1])
y = np.array(data.y).reshape(-1,1)
n,m,X,y
plt.scatter(X[:,-2],y)
'''

损失函数

def compterCost(X, theta, y):
    delta = X @ theta - y
    return delta.T @ delta
compterCost(X, np.zeros([m,1]), y)
array([[6222.11037223]])

梯度

def getGradient(X, theta, y):
    return ((X@theta - y).T @ X).T
getGradient(X, np.zeros([m,1]), y)
array([[ -566.3961    ],
       [-6336.89842532]])

梯度下降

def gradientDescent(X, theta, y, alpha, iters):
    cost = np.zeros(iters + 1)
    cost[0] = compterCost(X, theta, y)
    print(f"loop {0}'s cost is {cost[0]}")
    for i in range(iters):
        theta = theta - getGradient(X, theta, y)*alpha
        cost[i+1] = compterCost(X, theta, y)
        print(f"loop {i+1}'s cost is {cost[i+1]}")
    plt.plot(range(iters+1), cost)
    #print(cost)
    return theta
theta = np.zeros([m, 1])
theta = gradientDescent(X, theta, y, 0.000003, 2000)

image

预测函数

def getMy(x):
    return np.insert(x, 0, 1, 1) @ theta
#getMy(np.array([[1],[2]]))
#theta

拟合图像

plt.scatter(X[:,-1], y)
#x = np.arange(5,23,0.5)
#plt.plot(x, getMy(x.reshape(-1,1)))
plt.axline([1,getMy(np.array([[1]]))[0][0]], xy2 = [2,getMy(np.array([[2]]))[0][0]])
#plt.plot(x, np.insert(x.reshape(-1,1), 0, 1, 1)@true_theta)
<matplotlib.lines._AxLine at 0x7f3d006718d0>

image

标签:plt,回归,np,cost,线性,theta,array,data
From: https://www.cnblogs.com/RanX2018/p/16742881.html

相关文章

  • PySpark 随机森林回归机器学习——一种实用的方法,第 7 部分
    PySpark随机森林回归机器学习——一种实用的方法,第7部分brilliantprogrammer大家好,在之前的博客中,我们学习了使用pyspark的线性回归算法,在本博客中,我们还将更具体......
  • ARC146C Even XOR(线性基,组合)
    ARC146CEvenXOR有多少集合\(S\),每个元素都在\([0,2^N)\)之间,且所有偶数大小的子集的异或和不为\(0\)。CODE奇数大小的子集\(\oplus\)和可以为\(0\),可是如果......
  • [转]逻辑回归和线性回归区别
    https://blog.csdn.net/qq_30354455/article/details/827976201)线性回归要求变量服从正态分布,logistic回归对变量分布没有要求。2)线性回归要求因变量是连续性数值变量,而lo......
  • python 线性代数:解多元一次方程
    因为在程序化交易策略中使用了网格算法进行交易,因为在网格中想设置动态资源大小的问题,所以就想到使用抛物线的分布方法来对网格资金配置进行分配。比如我的网格最大值设置......
  • 逻辑回归示例
    torch.rand(*sizes,out=None)→Tensor返回一个张量,包含了从区间[0,1)的均匀分布中抽取的一组随机数。张量的形状由参size定义。参数:sizes(int...)-整数序列,定......
  • 多分类logit回归案例分析
    在研究X对Y的影响时,因变量Y有时是分类变量,这时如果还想分析影响关系可以使用logit回归,常见的logit回归包括,二元logit回归(二项logit回归)、多分类logit回归以及有序logit回......
  • 有序logit回归案例分析
    我们经常会遇到因变量有多个取值而且有大小顺序的情况,如幸福感,开心程度等,这时,一般的线性回归分析无法准确地刻画变量之间的因果关系,需要用其他的回归分析方法来进行拟合模......
  • 线性表前期学习
         ......
  • UKF和EKF算法在非线性系统中的应用比较
    参考内容:书籍《卡尔曼滤波原理及应用------matlab仿真》这本书对kalman算法的解析很清晰,MATLAB程序很全,适合初学者(如有侵权,请联系删除(qq:1491967912))之前学习了EKF算法和......
  • 10种常见的回归算法总结和介绍
    线性回归是机器学习中最简单的算法,它可以通过不同的方式进行训练。在本文中,我们将介绍以下回归算法:线性回归、Robust回归、Ridge回归、LASSO回归、ElasticNet、多项式......