吴恩达Deeplearning.ai课程:Logistic回归Python实验代码(一)

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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值