Created
October 2, 2023 06:04
-
-
Save newtoallofthis123/be1f0100da56a0160c4b07ba118611b9 to your computer and use it in GitHub Desktop.
Logistic Regression Manually
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.model_selection import train_test_split | |
import pandas as pd | |
df = pd.read_csv( | |
'https://raw.githubusercontent.com/johnmyleswhite/ML_for_Hackers/master/02-Exploration/data/01_heights_weights_genders.csv') | |
df.head() | |
BinaryGenders = np.zeros(len(df)) | |
for i in range(len(df)): | |
if df.iloc[i, 0] == "Male": | |
BinaryGenders[i] = 1 | |
else: | |
BinaryGenders[i] = 0 | |
df['BinaryGenders'] = BinaryGenders | |
df = df.sample(frac=1).reset_index(drop=True) | |
Y = df['BinaryGenders'].values | |
X = df[['Height', 'Weight']].values | |
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33) | |
n_samples, n_features = X.shape | |
def sigmoid(x): | |
return 1 / (1 + np.exp(-x)) | |
w = np.zeros(n_features) | |
b = 0 | |
def cost_function(X, Y, w, b): | |
m = len(Y) | |
dz = np.dot(X, w) + b | |
A = sigmoid(dz) | |
cost = (-1 / m) * np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A)) | |
return cost | |
for _ in range(1000): | |
y_pred = sigmoid(np.dot(X_train, w) + b) | |
w -= 0.1 * np.dot(X_train.T, y_pred - Y_train) / n_samples | |
b -= 0.1 * (y_pred - Y_train).sum() / n_samples | |
# Predictions | |
y_pred = sigmoid(np.dot(X_test, w) + b) | |
y_pred_class = np.where(y_pred > 0.5, 1, 0) | |
# Accuracy | |
print("Accuracy: ", (y_pred_class == Y_test).sum() / len(Y_test)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment