Logistic Regression
Generate training and testing date set:
1 | def generate_data(n, p=0.8): |
Model of logistic regression:
1 | def model(_x, _theta): |
Calculating the gradient vector:
1 | def gradient(_x, _y, _theta, _model): |
Result:
step: 0, theta: 0.951656 1.001376 -0.007945
training error rate: 0.506250, testing error rate: 0.450000
step: 100, theta: -0.314369 1.131259 -0.194332
training error rate: 0.043750, testing error rate: 0.175000
step: 200, theta: -0.361768 1.302411 -0.186887
training error rate: 0.043750, testing error rate: 0.150000
step: 300, theta: -0.395465 1.433055 -0.181936
training error rate: 0.043750, testing error rate: 0.150000
step: 400, theta: -0.422276 1.538629 -0.178686
training error rate: 0.043750, testing error rate: 0.150000
step: 500, theta: -0.444488 1.627198 -0.176557
training error rate: 0.050000, testing error rate: 0.150000
step: 600, theta: -0.463398 1.703425 -0.175217
training error rate: 0.050000, testing error rate: 0.150000
step: 700, theta: -0.479820 1.770257 -0.174460
training error rate: 0.050000, testing error rate: 0.150000
step: 800, theta: -0.494296 1.829684 -0.174151
training error rate: 0.050000, testing error rate: 0.150000
step: 900, theta: -0.507206 1.883114 -0.174194
training error rate: 0.050000, testing error rate: 0.150000