- 单变量线性回归: h(x)=theta0 + theta1* x 1
- 多变量线性回归: h(x)=theta0 + theta1* x 1 + theta2* x 2 + theta3* x 3
- 多项式回归: h(x)=theta0 + theta1* x 1 + theta2* (x2^2) + theta3* (x3^3)
import numpy as np
from pylab import *
def train_wb(X, y):
"""
:param X:N*D的数据
:param y:X对应的y值
:return: 返回(w,b)的向量
"""
if np.linalg.det(X.T * X) != 0:
wb = ((X.T.dot(X).I).dot(X.T)).dot(y)
return wb
def test(x, wb):
return x.T.dot(wb)
def getdata():
x = []; y = []
file = open("ex0.txt", 'r')
for line in file.readlines():
temp = line.strip().split("\t")
x.append([float(temp[0]),float(temp[1])])
y.append(float(temp[2]))
return (np.mat(x), np.mat(y).T)
def draw(x, y, wb):
#画回归直线y = wx+b
a = np.linspace(0, np.max(x)) #横坐标的取值范围
b = wb[0] + a * wb[1]
plot(x, y, '.')
plot(a, b)
show()
X, y = getdata()
wb = train_wb(X, y)
draw(X[:, 1], y, wb.tolist())
-
运行结果
-
手工造的数据
https://blog.youkuaiyun.com/u014028027/article/details/72667733
https://blog.youkuaiyun.com/weixin_40683253/article/details/81109129