机器学习——吴恩达——编程作业ex2——MATLAB + Python

    技术2022-07-11  96

    文章目录

    MatlabMATLAB源码:运行效果 Python代码运行结果:

    Matlab

    需要补充的文件:

    MATLAB源码:

    前两个略过不表,一个已经给出,另一个也很简单。 costFunction.m

    % ====================== YOUR CODE HERE ====================== % Instructions: Compute the cost of a particular choice of theta. % You should set J to the cost. % Compute the partial derivatives and set grad to the partial % derivatives of the cost w.r.t. each parameter in theta % % Note: grad should have the same dimensions as theta % h = sigmoid(X * theta); grad =(1/m)* X' * (h - y); J = (-1 / m) .* [y;1-y]' * [log(h);log(1 - h)]; % =============================================================

    predict.m

    % ====================== YOUR CODE HERE ====================== % Instructions: Complete the following code to make predictions using % your learned logistic regression parameters. % You should set p to a vector of 0's and 1's % G = sigmoid(X * theta); for i = 1:m if G(i) < 0.5 p(i) = 0; else p(i) = 1; end end % =========================================================================

    costFunctionReg.m

    % ====================== YOUR CODE HERE ====================== % Instructions: Compute the cost of a particular choice of theta. % You should set J to the cost. % Compute the partial derivatives and set grad to the partial % derivatives of the cost w.r.t. each parameter in theta h = sigmoid(X * theta); A = eye(size(theta,1)-1); B = zeros(size(theta,1)-1,1); L = [B,A]; C = zeros(1,size(L,2)); L = [C;L]; grad = (1/m)* X' * (h - y) + (lambda / m) * (L * theta); J = -(1 / m) .* [y;1-y]' * [log(h);log(1 - h)] + (lambda / (2 * m)) * (L * theta)'* (L * theta); % =============================================================

    运行效果

    命令行显示:

    正则化 图片: 正则化;

    Python

    代码

    目前只完成第一部分: main.py

    import numpy as np import functions import scipy.optimize as op # 读取数据 filename = 'ex2data1.txt' data = np.loadtxt(filename, delimiter=',') # 由于文件用逗号分割数据,故指定分割数据的符号 # 给矩阵x,y赋值 x = data[:, :2] y = data[:, 2] # 调用函数绘图 functions.plot_data(x, y) # 补充X的第一列为1 x = np.c_[np.ones([len(x), 1]), x] # 初始化参数 initial_theta = np.zeros(np.size(x, 1)) j = functions.compute_cost(initial_theta, x, y) grad = functions.update_grad(initial_theta, x, y) print('代价函数的值为', j) print('梯度', grad) # 调用高级函数优化 result = op.minimize(fun=functions.compute_cost, x0=initial_theta, args=(x, y), method='TNC', jac=functions.update_grad) print(result) # 绘制决策边界 functions.plot_decision_boundary(result.x, x, y) # 回归评估 prob = functions.sigmoid([1, 45, 85] @ result.x) print(prob) p = functions.predict(result.x, x) accuracy = np.mean(np.double(p == y)) * 100 print(accuracy) # TODO:正则化逻辑回归

    functions.py

    import matplotlib.pyplot as plt import numpy as np # 解决中文显示问题 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False def plot_data(x, y): pos = np.where(y == 1) neg = np.where(y == 0) plt.title('分数分布') plt.xlabel('考试1 分数') plt.ylabel('考试2 分数') plt.plot(x[pos, 0], x[pos, 1], 'k+') plt.plot(x[neg, 0], x[neg, 1], 'ro') # plt.show() return plt def sigmoid(x): return 1 / (1 + np.exp(- x)) def compute_cost(theta, x, y): m = len(y) h = sigmoid(x @ theta) # 这里的theta初始化时要注意,参数给多算出的h的shape会为(100,1) # 把等式向量化 yy = np.hstack((y, 1 - y)) ll = np.hstack((np.log(h), np.log(1 - h))) j = (-1 / m) * (yy @ ll) return j def update_grad(theta, x, y): m = len(y) h = sigmoid(x @ theta) grad = (1 / m) * (x.T @ (h - y)) return grad def plot_decision_boundary(theta, x, y): x1_min = np.min(x[:, 1]) x1_max = np.max(x[:, 1]) x1 = np.arange(x1_min, x1_max, 0.5) x2 = -(theta[0] + theta[1] * x1) / theta[2] plt.plot(x1, x2, '-') # plt.legend(['decision boundary', 'Admitted', 'not Admitted'], loc='upper right') def predict(theta, x): m = len(x) p = np.zeros(m) g = sigmoid(x @ theta) for i in range(m): if g[i] < 0.5: p[i] = 0 else: p[i] = 1 return p

    运行结果:

    图片:

    控制台: 正则不想做了呢。。。。。。。。

    Processed: 0.015, SQL: 9