import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('ex1data2.txt',names = ['size','bedroom','price']) #打开文件
def normalize_feature(data): #均值归一化
return (data - data.mean()) / data.std()
data = normalize_feature(data)
data.insert(0,'ones',1)#插入一列
X = data.iloc[:,0:-1] #构造数据集
Y = data.iloc[:,-1]
X = np.array(X) #将dataframe转成数组,方便运算
Y = np.array(Y)
Y.shape = (47,1)
def costFunction(X,Y,theta):#损失函数
inner = np.power(X @ theta - Y, 2)
return np.sum(inner) /(2 * len(X))
theta = np.zeros((3,1))
cost_init = costFunction(X,Y,theta)
def gradientDescent(X,Y,theta,alpha,iters,isprint = False): #梯度下降函数
costs = []
for i in range(iters):
theta = theta - (X.T @ (X @ theta - Y)) * alpha / len(X)
cost = costFunction(X,Y,theta)
costs.append(cost)
if i % 100 == 0:
if isprint:
print(cost)
return theta,costs
candidate_alpha= [0.0003,0.003,0.03,0.0001,0.001,0.01]#不同alpha迭代速度不同
iters = 2000
fig,ax = plt.subplots()
for alpha in candidate_alpha:
_,costs = gradientDescent(X,Y,theta,alpha,iters)
ax.plot(np.arange(iters),costs,label = alpha)
ax.legend()
ax.set(xlabel = 'iters',ylabel = 'cost',title = 'cost vs iters')
plt.show()
def normaEquation(X,Y): #正规方程求解theta
theta = np.linalg.inv(X.T @ X) @ X.T @ Y
return theta
吴恩达机器学习练习题python实现ex1(Linear regression with multiple variables)
最新推荐文章于 2021-08-16 21:12:31 发布