In [1]:
import numpy as np

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
In [2]:
def sigmoid(z):
    a = 1 / (1 + np.exp(-z))
    return a
In [3]:
def initialize_with_zeros(dim):
    w = np.zeros((dim, 1))  # [Dim, 1]
    b = 0
    return w, b
In [4]:
def propagate(w, b, x, y):
    num = x.shape[0]

    z = np.dot(x, w) + b  # [N, 1]
    h = sigmoid(z)  # [N, 1]
    cost = -(np.mean(y * np.log(h) + (1 - y) * np.log(1 - h)))

    dz = (h - y)  # [N, 1]
    dw = np.dot(x.T, dz) / num  # [Dim, 1]
    db = np.mean(dz)

    grads = {'dw': dw, 'db': db}

    return cost, grads
In [5]:
def optimize(w, b, X, Y, num_iterations, learning_rate):
    costs = []

    for i in range(num_iterations):
        cost, grads = propagate(w,b,X,Y)

        dw = grads["dw"]
        db = grads["db"]
        w = w - learning_rate * dw
        b = b - learning_rate * db

        if i % 100 == 0:
            costs.append(cost)
            print(f'[{i:3d}] Cost: {cost:.6f}')
            
    params = {'w': w, 'b': b}
    grads = {'dw': dw, 'db': db}
    
    return costs, grads, params

def predict(w, b, x):
    h = sigmoid(np.dot(x, w) + b)
    h[h >= 0.5] = 1
    h[h < 0.5] = 0

    return h
In [6]:
def logistic_model(train_x, train_y,test_x,test_y,learning_rate=0.1,num_iterations=2000):
    dim = train_x.shape[1]
    w, b = initialize_with_zeros(dim)

    costs, grads, params = optimize(w, b, train_x, train_y, num_iterations, learning_rate)
    w = params['w']
    b = params['b']

    prediction_train = predict(w, b, train_x)
    prediction_test = predict(w, b, test_x)

    accuracy_train = 1 - np.mean(np.abs(prediction_train - train_y))
    accuracy_test = 1 - np.mean(np.abs(prediction_test - test_y))
    print("Accuracy on train set:",accuracy_train )
    print("Accuracy on test set:",accuracy_test )

    d = {"costs": costs,
         "Y_prediction_test": prediction_test , 
         "Y_prediction_train" : prediction_train , 
         "w" : w, 
         "b" : b,
         "learning_rate" : learning_rate,
         "num_iterations": num_iterations,
         "train_acy":accuracy_train,
         "test_acy":accuracy_test
        }
    return d
In [7]:
iris = load_iris()
train_x, test_x, train_y, test_y = train_test_split(iris['data'], iris['target'], random_state=0)

# 为方便计算,把本来的 3 个类改成 2 个类
train_y = train_y[:, np.newaxis] // 2
test_y = test_y[:, np.newaxis] // 2

print(f'train_x.shape: {train_x.shape}')
print(f'train_y.shape: {train_y.shape}')
print(f'test_x.shape:  {test_x.shape}')
print(f'test_y.shape:  {test_y.shape}')
train_x.shape: (112, 4)
train_y.shape: (112, 1)
test_x.shape:  (38, 4)
test_y.shape:  (38, 1)
In [8]:
d = logistic_model(train_x, train_y, test_x, test_y, num_iterations=1000, learning_rate=0.1)
[  0] Cost: 0.693147
[100] Cost: 0.272389
[200] Cost: 0.217307
[300] Cost: 0.186085
[400] Cost: 0.165069
[500] Cost: 0.149789
[600] Cost: 0.138120
[700] Cost: 0.128888
[800] Cost: 0.121382
[900] Cost: 0.115146
Accuracy on train set: 0.9821428571428571
Accuracy on test set: 0.9473684210526316
In [ ]: