线性回归的目标是最小化参数θ为特征函数的代价函数
假设
def computeCost(X,Y,theta):
h = X.dot(theta.T)
cost = np.power((h - Y),2)
j = np.sum(cost)/(2 * len(X))
return j
单变量线性回归梯度下降:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
path = 'ex1data1.txt'
df = pd.read_csv(path,header=None,names=["Population","Profit"])
print(df.head) #看数据的前五项
print(df.describe())
print(df.info)
def computeCost(X,Y,theta):
h = X.dot(theta.T)
cost = np.power((h-Y),2)
j = np.sum(cost)/(2*len(X))
return j
df.insert(0,"x_0",1)
X = df.loc[:,"x_0":"Population"]