Author: hdup
Email: huangdan@youhujia.com
In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from models import linear_model, mse_cost, mse_cost_dev, gd_update
%matplotlib inline
In [2]:
train_data = np.array(pd.read_csv('./data/ex1data1.txt', header=None))
train_X = train_data[:,0].reshape((train_data.shape[0], 1))
train_y = train_data[:,1].reshape((train_data.shape[0], 1))
feature_cnt = train_X.shape[1]
sample_cnt = train_X.shape[0]
In [3]:
def linear_model(X, W, b):
return np.matmul(X ,W) + b
def mse_cost(h, y):
diff = h - y
return (diff * diff).sum() / (2 * y.shape[0])
def mse_cost_dev(X, y, h):
diff = h - y
return ((diff * X).sum() / X.shape[0]), (diff.sum() / X.shape[0])
def gd_update(X, y, h, W, b, cost_dev_func, lr=0.01):
d_W, d_b = cost_dev_func(X, y, h)
return (W - lr * d_W), (b - lr * d_b)
def create_parameters(feature_size):
param_W = np.array([np.random.randn() for i in range(0, feature_size)]).reshape((feature_size, 1))
param_b = np.random.randn()
return param_W, param_b
def std_normalize(X):
stds = np.std(X, axis=0)
means = np.mean(X, axis=0)
for col in range(0, X.shape[1]):
X[:,col] = (X[:,col] - means[col]) / stds[col]
return stds, means
def expand_feature_poly(X):
expanded = np.ndarray((X.shape[0], 8), dtype=np.float32)
expanded[:,0:2] = X[:,0:2]
expanded[:,2] = X[:,0]**2
expanded[:,3] = X[:,1]**2
expanded[:,4] = X[:,0]**3
expanded[:,5] = X[:,1]**3
expanded[:,6] = X[:,0]*X[:,1]
expanded[:,7] = X[:,2]
return expanded
In [4]:
W, b = create_parameters(feature_cnt)
# batch learning
for epoch in range(0, 1000):
h = linear_model(train_X, W, b)
W, b = gd_update(train_X, train_y, h, W, b, mse_cost_dev, lr=0.01)
if (epoch + 1) % 100 == 0:
cur_cost = mse_cost(h, train_y)
print('epoch: {0}, cost:{1}, W:{2}, b:{3}'.format(epoch + 1, cur_cost, W, b))
# finish
predictions = linear_model(train_X, W, b)
final_cost = mse_cost(predictions, train_y)
print('training finished!')
print('final cost: {0}, W: {1}, b: {2}'.format(final_cost, W, b))
# then plot some curves
plt.plot(train_X, train_y, 'r+', label='training')
plt.plot(train_X, predictions, 'b--', label='fitted')
plt.grid(True)
plt.legend()
plt.show()
In [6]:
train_data = np.array(pd.read_csv('./data/ex1data2.txt', header=None), dtype=np.float32)
#train_data = expand_feature_poly(train_data)
std_normalize(train_data)
sample_cnt = train_data.shape[0]
feature_cnt = train_data.shape[1] - 1
train_X = train_data[:,0:feature_cnt].reshape((sample_cnt, feature_cnt))
train_y = train_data[:,feature_cnt:feature_cnt+1].reshape((sample_cnt, 1))
In [8]:
W, b = create_parameters(feature_cnt)
# batch learning
for epoch in range(0, 200):
h = linear_model(train_X, W, b)
W, b = gd_update(train_X, train_y, h, W, b, mse_cost_dev, lr=0.01)
if (epoch + 1) % 100 == 0:
cur_cost = mse_cost(h, train_y)
print('epoch: {0}, cost:{1}'.format(epoch + 1, cur_cost))
# finish
predictions = linear_model(train_X, W, b)
final_cost = mse_cost(predictions, train_y)
print('training finished!')
print('final cost: {0}'.format(final_cost))
# then plot some curves
plt.plot(train_X[:,0], train_y, 'r+', label='feature 1')
plt.plot(train_X[:,0], predictions, 'b+', label='fitted')
plt.grid(True)
plt.legend()
plt.show()
plt.plot(train_X[:,1], train_y, 'r+', label='feature 2')
plt.plot(train_X[:,1], predictions, 'b+', label='fitted')
plt.grid(True)
plt.legend()
plt.show()
In [ ]: