defplot_data(X, y): """This function plots the data points X and y into a new figure. It plots the data points with red + for the positive examples, and blue o the negative examples. X is assumed to be a Mx2 matrix. X: shape:nx2 y: shape:nx1 """
plt.figure() # ====================== YOUR CODE HERE ====================== m, _ = X.shape flag_Admitted = 1# 图例的标志位 flag_Not_Admitted = 1 for i inrange(m): if y[i]==1: if flag_Admitted: plt.plot(X[i,0], X[i,1], 'r+', label='Admitted') flag_Admitted = 0 else: plt.plot(X[i,0], X[i,1], 'r+') else: if flag_Not_Admitted: plt.plot(X[i,0], X[i,1], 'b.', label='Not Admitted') flag_Not_Admitted = 0 else: plt.plot(X[i,0], X[i,1], 'b.')
defpredict(theta, X): """Predict whether the label is 0 or 1 using learned logistic regression parameters theta. input: theta:model's parameters X: input samples output:0 or 1 """ m, _ = X.shape pred = np.zeros((m, 1), dtype=np.bool)
# ====================== YOUR CODE HERE ====================== g = sigmoid(np.dot(theta, X.T)) for i inrange(m): if g[i] > 0.5: pred[i] = 1 else: pred[i] = 0 # ============================================================
# 预测考试一得45分,考试二得85分的学生的录取概率 x_test = np.array([1, 45, 85.0]) prob = sigmoid(np.dot(theta_opt, x_test)) print('For a student with scores 45 and 85, we predict an admission probability of: ', prob)
logistic_regression()
Cost at initial theta (zeros): nan
Gradient at initial theta (zeros): [ 0.4 20.81292044 21.84815684]
Optimization terminated successfully.
Current function value: 0.203498
Iterations: 37
Function evaluations: 111
Gradient evaluations: 111
theta_op:
[-25.15777637 0.20620326 0.20144282]
For a student with scores 45 and 85, we predict an admission probability of: 0.7762612158589474
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/ipykernel_launcher.py:7: RuntimeWarning: divide by zero encountered in log
import sys
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/ipykernel_launcher.py:7: RuntimeWarning: invalid value encountered in multiply
import sys
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/ipykernel_launcher.py:8: RuntimeWarning: overflow encountered in exp
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/ipykernel_launcher.py:8: RuntimeWarning: overflow encountered in exp
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/ipykernel_launcher.py:7: RuntimeWarning: divide by zero encountered in log
import sys
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/ipykernel_launcher.py:7: RuntimeWarning: invalid value encountered in multiply
import sys
正则化的逻辑回归
数据可视化
调用函数plot_data可视化第二组数据 LR_data2.txt 。 正确的输出如下:
1 2 3 4 5 6 7 8 9
# 加载数据 data = np.loadtxt("LR_data2.txt", delimiter=",") X, y = data[:, :2], data[:, 2]
# 可视化数据 # ====================== YOUR CODE HERE ================ plot_data(X, y) # ====================================================== plt.show()
defmap_feature(X1, X2, degree=6): """Feature mapping function to polynomial features.""" m = len(X1) assertlen(X1) == len(X2) n = int((degree+2)*(degree+1)/2)
out = np.zeros((m, n))
idx = 0 for i inrange(degree+1): for j inrange(i+1): # print i-j, j, idx out[:, idx] = np.power(X1, i-j)*np.power(X2, j) idx += 1
# 计算并打印初始参数对应的代价与梯度 cost = cost_function_reg(theta_initial, X, y, lmb=lmb) grad = cost_gradient_reg(theta_initial, X, y, lmb=lmb) print("Cost at initial theta (zeros): ", cost) print("Gradient at initial theta (zeros): \n", grad)
# 使用 scipy.optimize.fmin_cg 优化模型参数 args = (X, y, lmb) maxiter = 200 # ====================== YOUR CODE HERE ====================== ret = op.fmin_cg(cost_function_reg, theta_initial, cost_gradient_reg, args) # ============================================================ theta_opt = ret #print("Cost at theta found by fmin_cg: ", cost_min) print("theta_op: \n", theta_opt)