# 3个非常重要的公式在此代码中#################################
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import time
np.random.seed(1)
X = 2*np.random.rand(5000, 1) # between 0-1
y = 10 + 2*X + np.random.randn(5000, 1)
plt.figure(figsize=(8, 6))
plt.scatter(X, y)
x_train, x_test, y_train, y_test = train_test_split(X, y) # 分训练与测试的数据,默认训练集是75%,测试集是25%。
plt.figure(figsize=(8, 6))
plt.subplot(1, 2, 1)
plt.scatter(x_train, y_train)
plt.subplot(1, 2, 2)
plt.scatter(x_test, y_test)
plt.pause(0.5) # 不要使用plt.show()
plt.close()
# 将训练数据加上1这一列,w与b合并了
x_train_ = np.column_stack((x_train, np.ones(x_train.shape[0]))) # equal: np.c_[x_train, np.ones(x_train.shape[0])]
x_test_ = np.column_stack((x_test, np.ones(x_test.shape[0])))
class LR:
def __init__(self):
self.W = None
def LSM(self, x, y):
start = time.time()
self.W = np.zeros((x.shape[1], 1))
self.W = np.dot(np.dot(np.linalg.inv(x.T.dot(x)), x.T), y) #######################################
loss = np.mean((np.dot(x, self.W)-y)**2)
end = time.time()
CostTime = end -start
print("LSM得到的loss为:%.4f\n" % loss)
print("花费的时间为:%.4f\n" % CostTime)
return self.W
def BGD(self, x, y, learning_rate, iter):
start = time.time()
self.W = np.zeros((x.shape[1], 1))
for i in range(iter):
x_pred = np.dot(x, self.W)
diff = 2*np.dot(x.T, (x_pred-y))/len(y) # x.T * (x_pred -y)##########################################
self.W -= learning_rate*diff
loss = np.mean((x_pred-y)**2) ######################################################
if i % 100 == 0:
print("After %d iter, loss is %.4f" % (i, loss))
end = time.time()
CostTime = end - start
print("花费的时间为:%.4f\n" % CostTime)
return self.W
def predict(self, x, w):
test_pred = np.dot(x,w)
return test_pred
if __name__ == "__main__":
lr = LR()
w_LSM = lr.LSM(x_train_, y_train)
w_GD = lr.BGD(x_train_, y_train, 0.05, 1000)
print(w_LSM)
print("\n")
print(w_GD)
print("\n")
test_pred_LSM = lr.predict(x_test_, w_LSM)
test_pred_GD = lr.predict(x_test_, w_GD)
loss_LSM = np.mean((test_pred_LSM-y_test)**2)
loss_GD = np.mean((test_pred_GD-y_test)**2)
print("LSM的预测损失为:%.4f" % loss_LSM)
print("GD的预测损失为:%.4f\n" % loss_GD)
fig3 = plt.figure(figsize=(8, 6))
plt.subplot(1, 2, 1)
plt.scatter(x_test, y_test)
plt.plot(x_test, test_pred_LSM, c='r')
plt.title("LSM_pred")
plt.subplot(1, 2, 2)
plt.scatter(x_test, y_test)
plt.plot(x_test, test_pred_GD, c='r')
plt.title("GD_pred")
plt.show()
python实现基本的机器学习算法系列(1):逻辑回归
最新推荐文章于 2022-07-30 18:03:52 发布