import numpy as np
import matplotlib.pyplot as plt
def showLogRegres(weights, train_x, train_y, b):
# notice: train_x and train_y is mat datatype
numFeatures, numSamples = np.shape(train_x)
if numFeatures != 2:
print ("Sorry! I can not draw because the dimension of your data is not 2!")
return 1
# draw all samples
for i in range(numSamples):
if int(train_y[0, i]) == 0:
plt.plot(train_x[0, i], train_x[1, i], 'or')
elif int(train_y[0, i]) == 1:
plt.plot(train_x[0, i], train_x[1, i], 'ob')
# draw the classify line
train_x = np.array(train_x)
min_x = min(train_x[0,: ])
max_x = max(train_x[0,: ])
print(min_x,max_x)
weights = np.array(weights) # convert mat to array
y_min_x = float(-b - weights[0] * min_x) / weights[1] # y = sigmod( w1*x1 + w2*x2 + b ),分类边界即为 w1*x1 +w2*x2 + b = 0
y_max_x = float(-b - weights[0] * max_x) / weights[1] #参见微博http://blog.csdn.net/xinzhi8/article/details/67639031
plt.plot([min_x, max_x], [y_min_x, y_max_x], '-g')
plt.xlabel('X1'); plt.ylabel('X2')
plt.show()
class LogisticRegression:
def __init__(self, m, n, X, y, alpha, iters):
self.m = m #样本数量:列数
self.n = n #'特征数量:行数'
self.X = X #'给定输入X'
self.y = y #'真值标签,类别向量1*m'
self.alpha = alpha #'学习率'
self.iters = iters #'迭代次数'
self.w = np.zeros((n,1)) #'参数向量n*1维,wt*X输出输入的数量的概率,1*n * n*m输出1*m的向量'
self.b = 0
return
def sigmod(self, x):
return 1 / (1 + np.exp(-x))
def train(self):
for i in range(self.iters):
J = 0
dw = np.zeros((self.n,1))
db = 0
Z = np.dot(self.w.T, self.X) + self.b
A = self.sigmod(Z) #sigmod函数
J = -np.sum((np.dot(self.y, np.log(A).T) + np.dot(1 - self.y, np.log(1 - A).T)))/self.m
dz = A - self.y
dw = np.dot(self.X, dz.T)/self.m
db = np.sum(dz, axis=1)/self.m
self.w = self.w - self.alpha * dw
self.b = self.b - self.alpha * db
if((i+1)%100 == 0):
print(str(i+1)+" times loss = "+str(J))
return self.w,self.b
def test(self,test_x, test_y):
numFeatures, numSamples = test_x.shape
matchCount = 0
Z = np.dot(self.w.T, self.X) + self.b
A = 1 / (1 + np.exp(-Z))
A[A > 0.5] = 1
A[A < 0.5] = 0
accuracy =(A==test_y)
accuracy = np.sum(accuracy) / self.m
return accuracy
if __name__ == '__main__':
X = []
y = []
fileIn = open('G:/testSet.txt')
for line in fileIn.readlines():
lineArr = line.strip().split()
X.append([float(lineArr[0]), float(lineArr[1])])
y.append(float(lineArr[2]))
X = np.mat(X).T
y = np.mat(y)
n ,m = X.shape
alpha = 0.1 # 设置更新速率
iters = 1000 # 设置迭代停止条件
lr = LogisticRegression(m, n, X, y, alpha, iters)
W,b= lr.train()
showLogRegres(W, X, y, b)
accuracy = lr.test(X, y)
print("accuracy = " + str(accuracy))
吴恩达Deeplearning.ai课程:Logistic回归Python实验代码(一)
最新推荐文章于 2020-11-05 20:07:38 发布