I've got this code of logistic regression but I can't seem to use gradient descent instead of the fmin_tnc
function.
The problem I'm trying to fix is that I want to create it from scratch and gradient descent seems like a viable way to do it. fmin_tnc
is a straightforward way to get the optimal theta values but that isn't very intuitive. Here's the code:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import random
data = pd.read_csv("train.txt", header=None)
testing_data = pd.read_csv("test.txt")
# X = feature values, all the columns except the last column
X = data[data.columns[:-1]]
# y = target values, last column of the data frame
y = data.iloc[:, -1]
X = np.c_[np.ones((X.shape[0], 1)), X]
y = y[:, np.newaxis]
theta = np.zeros((X.shape[1], 1))
def sigmoid(x):
return (1 / (1 + np.exp(-x)))
def net_input(theta, x):
return np.dot(x, theta)
def probability(theta, x):
return sigmoid(net_input(theta, x))
def cost_function(theta, x, y):
m = x.shape[0]
total_cost = -(1 / m) * np.sum(
y * np.log(probability(theta, x)) + (1 - y) * np.log(
1 - probability(theta, x)))
return total_cost
def gradient(theta, x, y):
# Computes the gradient of the cost function at the point theta
m = x.shape[0]
return (1 / m) * np.dot(x.T, sigmoid(net_input(theta, x)) - y)
def fit(x, y, theta):
opt_weights = fmin_tnc(func=cost_function, x0=theta,
fprime=gradient,args=(x, y.flatten()))
return opt_weights[0]
parameters = fit(X, y, theta)
def predict(x):
theta = parameters[:, np.newaxis]
return probability(theta, x)
def accuracy(x, actual_classes, probab_threshold=0.5):
predicted_classes = (predict(x) >=
probab_threshold).astype(int)
predicted_classes = predicted_classes.flatten()
accuracy = np.mean(predicted_classes == actual_classes)
return accuracy * 100
accuracy(X, y.flatten())