实现线性回归算法
1.主要程序代码
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# 参考链接博客地址 :http://blog.csdn.net/u013733326/article/details/79639509
import numpy as np
import matplotlib.pyplot as plt
import h5py
from lr_utils import load_dataset
# 1.加载数据
train_set_x_orig,train_set_y,test_set_x_orig,test_set_y,classes=load_dataset()
# 2.预处理
# 2.1数据重新构造-数据平铺
train_set_x_flatten=train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T
test_set_x_flatten=test_set_x_orig.reshape(test_set_x_orig.shape[0],-1).T
# 2.2标准化数据集
train_set_x=train_set_x_flatten/255
test_set_x=test_set_x_flatten/255
# 2.3实现一次迭代中的前向传播和反向求梯度的过程
def sigmoid(z):
return 1/(1+np.exp(-z))
def initial_with_zeros(dim):
w=np.zeros(shape=(dim,1))
b=0
return (w,b)
def propagate(w, b, X, Y):
m = X.shape[1]
# 正向传播
A = sigmoid(np.dot(w.T, X)+b)
cost = (-1/m)*np.sum(Y*np.log(A)+(1-Y)*np.log(1-A))
# 反向求梯度
dw = (1/m)*np.dot(X,(A-Y).T)
db = (1/m)*np.sum(A-Y)
grad = {
"dw":dw,
"db":db
}
cost = np.squeeze(cost)
return (grad , cost)
def predict(w, b, X):
m = X.shape[1]
Y_prediction=np.zeros(shape=(1,m))
A=sigmoid(np.dot(w.T,X)+b)
for i in range(A.shape[1]):
Y_prediction[0,i] = 1 if A[0,i]>0.5 else 0
return Y_prediction
def optimize(w,b,X,Y,num_iterations,learn_rate,print_cost=False):
costs = []
for i in range(num_iterations):
grad,cost = propagate(w,b,X,Y)
w = w-learn_rate*grad["dw"]
b = b-learn_rate*grad["db"]
if i % 100 == 0:
costs.append(cost)
if print_cost and (i % 100 == 0):
print("迭代次数:%i,误差值:%f" % (i, cost))
params = {
"w": w,
"b": b
}
grad = {
"dw": grad["dw"],
"db": grad["db"]
}
return params, grad, costs
def model(X_train,Y_train,X_test,Y_test,num_iterations=2000,learn_rate=0.5,print_cost=False):
w,b=initial_with_zeros(X_train.shape[0])
paramteres,grads,costs=optimize(w,b,X_train,Y_train,num_iterations,learn_rate,print_cost)
w,b=paramteres["w"],paramteres["b"]
Y_prediction_test=predict(w,b,X_test)
Y_prediction_train=predict(w,b,X_train)
print("训练集准确性:",format(100-np.mean(np.abs(Y_prediction_train-Y_train))*100))
print("测试集准确性:",format(100-np.mean(np.abs(Y_prediction_test-Y_test))*100))
d = {
"costs":costs,
"Y_prediction_test":Y_prediction_test,
"Y_prediction_train":Y_prediction_train,
"w":w,
"b":b,
"learning_rate":learn_rate,
"num_iterations":num_iterations
}
return d
d = model(train_set_x,train_set_y,test_set_x,test_set_y,num_iterations=2000,learn_rate=0.005,print_cost=False)
# 绘制图
# costs = np.squeeze(d["costs"])
# plt.plot(costs)
# plt.ylabel('cost')
# plt.xlabel('iterations(per hundreds)')
# plt.title("Learning rate = " + str(d["learning_rate"]))
# plt.show()
learn_rate = [0.01, 0.001, 0.0001]
models = {}
for i in learn_rate:
print("learn_rate is "+str(i))
models[str(i)] = model(train_set_x,train_set_y,test_set_x,test_set_y,num_iterations=1500,learn_rate=i,print_cost=False)
print("\n"+"-----------------------"+"\n")
for i in learn_rate:
plt.plot(np.squeeze(models[str(i)]["costs"]),label = str(models[str(i)]["learning_rate"]))
plt.ylabel('cost')
plt.xlabel('iterations')
legend=plt.legend(loc='upper center', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.show()