jigyasu2's picture
Upload folder using huggingface_hub
a688549 verified
# -*- coding: utf-8 -*-
"""logistic_regression.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/18WbnQFC9t31wB5Ij0LJnr7Xf6Z8X8o2f
**Building a Classifier from Scratch** <br>
**Author: Jigyasu**
"""
import numpy as np
sigmoid = lambda x : 1/(1+np.exp(-1*x))
def cost_function(X, y, w, b):
m = len(X)
f = sigmoid(np.dot(X, w) + b)
# Clipping f to avoid log(0)
epsilon = 1e-15
f = np.clip(f, epsilon, 1 - epsilon)
loss = (-1 * y * np.log(f)) - ((1 - y) * np.log(1 - f))
cost = np.sum(loss) / m
return cost
def gradient(X, y, w, b):
m, n = X.shape
dw = np.zeros((n,))
db = 0.
for i in range(m):
f = sigmoid(np.dot(X[i], w) + b)
db += (f - y[i])
for j in range(n):
dw[j] += (f - y[i]) * X[i, j]
dw /= m
db /= m
return dw, db
def fit(X, y, alpha=0.01, epochs=1000):
m, n = X.shape
w = np.zeros((n, ))
b = 0.
for iteration in range(epochs):
dw, db = gradient(X, y, w, b)
w -= alpha * dw
b -= alpha * db
cost = cost_function(X, y, w, b)
if iteration % 100 == 0:
print(f"Epoch: {iteration}, Cost: {cost}")
return w, b
def predict(X, w, b):
f = sigmoid(np.dot(X, w) + b)
predictions = np.array([1 if f[i] > 0.6 else 0 for i in range(len(f))])
return predictions
def calculate_accuracy(y_true, y_pred):
correct_predictions = np.sum(y_true == y_pred)
accuracy = correct_predictions / len(y_true)
return accuracy
"""**Applying the Model to a Heart Failure Clinical Record Dataset**"""
import pandas as pd
data = pd.read_csv("heart_failure_clinical_records.csv")
X = data.drop(columns=["DEATH_EVENT"]).values
y = data["DEATH_EVENT"].values
#sklearn is used to split and scale the data
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X = scaler.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
w, b = fit(X_train, y_train)
predictions = predict(X_test, w, b)
accuracy = calculate_accuracy(y_test, predictions)
print(f"Accuracy: {accuracy * 100:.2f}%")
import gradio as gr
# Gradio interface
def predict_single_instance(age, anaemia, creatinine_phosphokinase, diabetes, ejection_fraction, high_blood_pressure, platelets, serum_creatinine, serum_sodium, sex, smoking, time):
# Prepare input
input_data = np.array([age, anaemia, creatinine_phosphokinase, diabetes, ejection_fraction, high_blood_pressure, platelets, serum_creatinine, serum_sodium, sex, smoking, time]).reshape(1, -1)
input_data = scaler.transform(input_data)
# Predict
prediction = predict(input_data, w, b)
return "Survived" if prediction[0] == 0 else "Death",
inputs = [
gr.Number(label="Age"),
gr.Number(label="Anaemia (0 for positive, 1 for negative)"),
gr.Number(label="Creatinine Phosphokinase"),
gr.Number(label="Diabetes (0 for positive, 1 for negative)"),
gr.Number(label="Ejection Fraction"),
gr.Number(label="High Blood Pressure (0 for positive, 1 for negative)"),
gr.Number(label="Platelets"),
gr.Number(label="Serum Creatinine"),
gr.Number(label="Serum Sodium"),
gr.Number(label="Sex (0 for Woman, 1 for Man)"),
gr.Number(label="Smoking (0 for positive, 1 for negative)"),
gr.Number(label="Time (Follow-up Period)")
]
outputs = gr.Textbox(label="Prediction")
gr.Interface(fn=predict_single_instance, inputs=inputs, outputs=outputs, title="Heart Stroke Survival Prediction").launch()