import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
path = 'ex1data1.txt'
data = pd.read_csv(path, header = None, names = ['Population','Profit'])
data.head()
data.describe()
data.plot(kind = 'scatter',x='Population',y='Profit',figsize=(4,3)) #散点图
plt.plot()
def computeCost(X, y, theta):
inner = np.power(((X * theta.T) - y), 2)
return np.sum(inner)/(2 * len(X))
data.insert(0, 'Ones', 1)
cols = data.shape[1]
X = data.iloc[:,0:cols-1]
y = data.iloc[:,cols-1:cols]
X = np.matrix(X.values)
y = np.matrix(y.values)
theta = np.matrix(np.array([0,0]))
computeCost(X,y,theta)
def gradientDescent(X,y,theta,alpha,iters):#iters 迭代次数
temp = np.matrix(np.zeros(theta.shape))
parameters = int(theta.ravel().shape[1])
cost = np.zeros(iters)
for i in range(iters):
error = (X * theta.T) - y
for j in range(parameters):
term = np.multiply(error,X[:,j])
temp[0,j] = theta[0,j] - ((alpha / len(X)) * np.sum(term))
theta = temp
cost[i] = computeCost(X,y,theta)
return theta, cost
x = np.linspace(data.Population.min(), data.Population.max(),100)
f = g[0,0] + (g[0,1] * x)
fig,ax = plt.subplots(figsize=(8,6))
ax.plot(x,f,'r',label='Prediction')
ax.scatter(data.Population,data.Profit,label='Traning Data')
ax.le()gend(loc=2)
ax.set_xlabel('Population')
ax.set_ylabel('Profit')
ax.set_title('Predicted Profit vs.Population Size')
plt.show
fig,ax = plt.subplots(figsize=(8,6))
ax.plot(np.arange(iters),cost,'r')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost')
ax.set_title('Error vs. Training Epoch')
plt.show()
path = 'ex1data2.txt'
data2 = pd.read_csv(path, header = None,names=['Size','Bedroom','Price'])
data2.head()
data2 = (data2 - data2.mean())/data2.std()
data2.head()
cols = data2.shape[1]
X2 = data2.iloc[:,0:cols-1]
y2 = data2.iloc[:,cols-1:cols]
#将X2 y2 转换为numpy矩阵 并 初始化theta2
X2 = np.matrix(X2.values)
y2 = np.matrix(y2.values)
theta2 = np.matrix(np.array([0,0,0]))
g2, cost2 = gradientDescent(X2, y2, theta2, alpha, iters)
computeCost(X2, y2, g2)
-查看训练进程:
fig2,ax = plt.subplots(figsize=(4,3)) #subplots 加s!!!!!!
ax.plot(np.arange(iters), cost2, 'r')
ax.set_xlabel('Iteration')
ax.set_ylabel('Cost')
ax.set_title('Error vs. Training Epoch2')
plt.show()