def costFunctionReg(theta, X, y, lambd): m = np.size(y) grad = np.zeros(np.size(theta)) g1 = sigmoid(np.dot(X, theta)) grad1 = 1 / m * np.dot(X.T, g1 - y) J1 = -1 / m * sum( np.multiply(y, np.log(g1)) + np.multiply(1 - y, np.log(1 - g1))) J = J1 + lambd / (2 * m) * (sum(np.power(theta, 2)) - theta[0]**2) grad[0] = grad1[0] for i in range(1, np.size(theta)): grad[i] = grad1[i] + lambd / m * theta[i] return J, grad
def predict(theta, X, y): m = np.shape(X)[0] p = np.zeros((m, 1)) z = np.dot(X, theta) Y = sigmoid(z) for i in range(m): if Y[i] >= 0.5: p[i] = 1 else: p[i] = 0 s = np.zeros(m) for i in range(m): if p[i] == y[i]: s[i] = 1 return s
def classify(nnParams, Input_layer_size, First_hidden_layer_size, Second_hidden_layer_size, num_labels, X): W2 = np.reshape(nnParams[0], (First_hidden_layer_size, Input_layer_size + 1), order='F').T W3 = np.reshape(nnParams[1], (Second_hidden_layer_size, First_hidden_layer_size + 1), order='F').T W4 = np.reshape(nnParams[2], (num_labels, Second_hidden_layer_size + 1), order='F').T # Number of samples m = np.size(X, axis=0) # Feed Forward X = np.concatenate((np.ones((m, 1)), X), axis=1) O1 = sigmoid(W2.T @ X.T) O1 = np.concatenate((np.ones((np.size(O1.T, axis=0), 1)), O1.T), axis=1) O2 = sigmoid(W3.T @ O1.T) O2 = np.concatenate((np.ones((np.size(O2.T, axis=0), 1)), O2.T), axis=1) h = sigmoid(W4.T @ O2.T).T testClass = np.argmax(h, axis=1) + 1 return (testClass)
''' %% ============== Part 4: Predict and Accuracies ============== % After learning the parameters, you'll like to use it to predict the outcomes % on unseen data. In this part, you will use the logistic regression model % to predict the probability that a student with score 45 on exam 1 and % score 85 on exam 2 will be admitted. % % Furthermore, you will compute the training and test set accuracies of % our model. % % Your task is to complete the code in predict.m % Predict probability for a student with score 45 on exam 1 % and score 85 on exam 2 ''' prob = sigmoid(np.array([1,45,85]).dot(theta)) print('For a student with scores 45 and 85, we predict an admission probability of ', prob) # Compute accuracy on our training set p = predict(theta, X) p = p.reshape((-1,1)) #p_y = np.hstack((p,y)) #print(p_y) print('Train Accuracy: ', np.mean((p == y) * 100.))
def predict(theta, X): return 0.5 <= sigmoid(X.dot(theta))
plotDecisionBoundary(theta, X, y) plt.show() ''' %% ============== Part 4: Predict and Accuracies ============== % After learning the parameters, you'll like to use it to predict the outcomes % on unseen data. In this part, you will use the logistic regression model % to predict the probability that a student with score 45 on exam 1 and % score 85 on exam 2 will be admitted. % % Furthermore, you will compute the training and test set accuracies of % our model. % % Your task is to complete the code in predict.m % Predict probability for a student with score 45 on exam 1 % and score 85 on exam 2 ''' prob = sigmoid(np.array([1, 45, 85]).dot(theta)) print( 'For a student with scores 45 and 85, we predict an admission probability of ', prob) # Compute accuracy on our training set p = predict(theta, X) p = p.reshape((-1, 1)) #p_y = np.hstack((p,y)) #print(p_y) print('Train Accuracy: ', np.mean((p == y) * 100.))
# Run fmin_bfgs to obtain the optimal theta #theta, cost, *unused = opt.fmin_bfgs(f=cost_func, fprime=grad_func, x0=initial_theta, # maxiter=400, full_output=True, disp=False) result = opt.minimize(fun=cost_func, x0=initial_theta, method='BFGS', jac=grad_func, options={'disp': True}) # Print theta to screen print('Cost at theta found by fminunc:', result.fun) #print('Expected cost (approx): 0.203\n'); theta = np.reshape(result.x, (n + 1, 1)) print(theta) print(' -25.161\n 0.206\n 0.201\n') # Plot Boundary plt.figure(1) plotDecisionBoundary(theta, X, y) #============== Part 4: Predict and Accuracies ============== temp = [1, 45, 85] prob = sigmoid(np.dot(temp, theta)) print('we predict an admission probability of ', prob) #0.775 +/- 0.002 # Compute accuracy on our training set s = predict(theta, X, y) result = np.mean(s) * 100 print('Train Accuracy:', result)