1 线性回归
def Linear_regression():
# get train data
data =np.loadtxt('data.csv',delimiter=',')
#define hyperparamters
#learning_rate is used for update gradient
#defint the number that will iteration
# define y =mx+b
learning_rate = 0.001
initial_b =0.0
initial_m = 0.0
num_iter = 1000
#train model
#print b m error
print 'initial variables:\n initial_b = {0}\n intial_m = {1}\n error of begin = {2} \n'\
.format(initial_b,initial_m,compute_error(initial_b,initial_m,data))
#optimizing b and m
[b ,m] = optimizer(data,initial_b,initial_m,learning_rate,num_iter)
#print final b m error
print 'final formula parmaters:\n b = {1}\n m={2}\n error of end = {3} \n'.format(num_iter,b,m,compute_error(b,m,data))
#plot result
plot_data(data,b,m)
- 最优化
def optimizer(data,starting_b,starting_m,learning_rate,num_iter):
b = starting_b
m = starting_m
#gradient descent
for i in range(num_iter):
#update b and m with the new more accurate b and m by performing
# thie gradient step
b,m =compute_gradient(b,m,data,learning_rate)
if i%100==0:
print 'iter {0}:error={1}'.format(i,compute_error(b,m,data))
return [b,m]