28.04.2020 Views

Sách Deep Learning cơ bản

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

68 Chương 4. Logistic regression

# Load data từ file csv

data = pd.read_csv('dataset.csv').values

N, d = data.shape

x = data[:, 0:d-1].reshape(-1, d-1)

y = data[:, 2].reshape(-1, 1)

# Vẽ data bằng scatter

plt.scatter(x[:10, 0], x[:10, 1], c='red', edgecolors='none', s=30, label='cho vay')

plt.scatter(x[10:, 0], x[10:, 1], c='blue', edgecolors='none', s=30, label='từ chối')

plt.legend(loc=1)

plt.xlabel('mức lương (triệu)')

plt.ylabel('kinh nghiệm (năm)')

# Thêm cột 1 vào dữ liệu x

x = np.hstack((np.ones((N, 1)), x))

w = np.array([0.,0.1,0.1]).reshape(-1,1)

# Số lần lặp bước 2

numOfIteration = 1000

cost = np.zeros((numOfIteration,1))

learning_rate = 0.01

for i in range(1, numOfIteration):

# Tính giá trị dự đoán

y_predict = sigmoid(np.dot(x, w))

cost[i] = -np.sum(np.multiply(y, np.log(y_predict)) + \

np.multiply(1-y, np.log(1-y_predict)))

# Gradient descent

w = w - learning_rate * np.dot(x.T, y_predict-y)

print(cost[i])

# Vẽ đường phân cách.

t = 0.5

plt.plot((4, 10),(-(w[0]+4*w[1]+ np.log(1/t-1))/w[2], -(w[0] + 10*w[1]+ \

np.log(1/t-1))/w[2]), 'g')

plt.show()

4.9 Bài tập

1. Tại sao hàm sigmoid được chọn trong bài toán logistic regression? Dùng chain rule tính đạo

hàm hàm sigmoid

2. Dùng chain rule tính đạo hàm loss function của logistic regression với từng biến.

3. Biểu diễn dưới dạng ma trận cho bài toán logistic regression.

4. Chỉnh 1 số tham số như learning_rate (tăng, giảm), số iteration xem loss function sẽ thay đổi

thế nào.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!