Exemple #1
0
def cost_function_reg(theta, x, y, lamb):
    m = len(x)
    h = sigmoid(x.dot(theta))
    shape = (h.shape[0], 1)
    j = (-1 / m) * np.sum((y * np.log(h).reshape(shape) +
                           (1 - y) * np.log(1 - h).reshape(shape))) + np.sum(
                               (lamb / (2 * m)) * np.square(theta))
    return j
Exemple #2
0
def predict(theta, x):
    m = len(x)
    p = np.zeros(m)

    h = sigmoid(x.dot(theta))
    for i in range(m):
        if h[i] >= 0.5:
            p[i] = 1
    return p
def gradient_function_reg(theta, x, y, lamb):
    m = len(x)
    n = len(theta)
    grad = np.zeros(n)
    h = sigmoid(x.dot(theta))
    grad[0] = (1 / m) * (np.sum(x.T[0, :].dot(h) - x.T[0, :].dot(y)))
    for j in range(1, n):
        grad[j] = (1 / m) * (np.sum(x.T[j, :].dot(h) - x.T[j, :].dot(y)) +
                             lamb * theta[j])
    return grad
def cost_function(theta, x, y):
    # 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

    m = len(y)

    h = sigmoid(x.dot(theta))
    j = (-1 / m) * (np.log(h).T.dot(y) + np.log(1 - h).T.dot(1 - y))
    if np.isnan(j[0]):
        return np.inf
    return j[0]
def gradient_function(theta, x, y):
    m = len(x)
    h = sigmoid(x.dot(theta.reshape(-1, 1)))
    grad = (1 / m) * x.T.dot(h - y)
    return grad.flatten()
		optimal parameters theta.
	"""
	res = minimize(cost_function, initial_theta, args=(x,y), method=None, jac=gradient_function, options={'maxiter':400})
	print('Cost at theta found by fminunc: %s' % res.fun)
	print('Expected cost (approx): 0.203')
	print('theta: %s' % res.x)
	print('Expected theta (approx):')
	print(' -25.161\n 0.206\n 0.201')

	"""
		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.py
		Predict probability for a student with score 45 on exam 1 and score 85 on exam 2
	"""
	theta = res.x
	prob = sigmoid(np.array([1, 45, 85]).dot(theta))
	print('For a student with scores 45 and 85, we predict an admission probability of %s' % prob)
	print('Expected value: 0.775 +/- 0.002')

	p = predict(theta, x)

	acc = 100*sum(p == y.ravel()) / p.size
	print('Train Accuracy: %s' % acc)
	print('Expected accuracy (approx): 89.0')