作者: Sam (甄峰) sam_code@hotmail.com
实例如下:
import numpy as np
import matplotlib.pyplot as plt
data_set =
np.loadtxt('Logistic_Data/testSet.txt',dtype='float')
print("house_data shape is:", data_set.shape)
#print(data_set[:3])
#迭代次数
epochs = 10000
#epochs = 1
#学习率
lr = 0.01
X_Data = data_set[:, 0:-1]
Y_Data = data_set[:, -1]
#print("X_Data type is:",type(X_Data))
#print("X_Data shape is:",X_Data.shape)
#print(X_Data[:3])
#print(type(Y_Data))
#print("Y_Data shape is:",Y_Data.shape)
#print(Y_Data[:3])
X_Data = np.insert(X_Data, 0, 1, axis=1)
#print(X_Data[:3])
Test_Number = int(len(X_Data) * 0.75)
X_Training = X_Data[:Test_Number]
Y_Training = Y_Data[:Test_Number]
X_Test = X_Data[Test_Number:]
Y_Test = Y_Data[Test_Number:]
Init_theta = [1] * X_Training.shape[1]
def sigmoid(z):
return 1 / (1 +
np.exp(-z))
def hypothesis(theta, x_dat):
medv = map(lambda
x,y:x*y, theta, x_dat)
s_dat = sum(medv)
return
sigmoid(s_dat)
def loss_funtion(X_dat, Y_dat, theta):
m, n =
np.shape(X_dat)
# m =
X_dat.shape[0]
loss = 0.0
for i in range(m):
sum_theta_x = 0.0
sum_theta_x += hypothesis(theta, X_dat[i])
loss += -Y_dat[i] * np.log(sum_theta_x) - (1 -
Y_dat[i]) * np.log(1 - sum_theta_x)
return loss
def update_theta(theta, X_dat, Y_dat):
up_theta =
theta.copy()
for i in
range(len(up_theta)):
temp = 0
for X_row,y in zip(X_dat,Y_dat):
temp +=
(hypothesis(theta,X_row) - y) * X_row[i]
temp /= len(X_dat)
up_theta[i] = theta[i] - (lr*temp)
return up_theta
def plotloss(loss_array):
n =
len(loss_array)
plt.xlabel("iteration
num")
plt.ylabel("loss")
plt.scatter(range(1,
n+1), loss_array)
plt.show()
J_list = [loss_funtion(X_Training, Y_Training,
Init_theta)]
for i in range(epochs):
theta_new =
update_theta(Init_theta, X_Training, Y_Training)
J_list.append(loss_funtion(X_Training, Y_Training,theta_new))
#print("%d" % i)
#print(loss_funtion(X_Training, Y_Training, theta_new))
Init_theta =
theta_new
print(J_list[-1])
plotloss(J_list)