0% found this document useful (0 votes)
4 views3 pages

1rst Exp

This document outlines a Python script that uses logistic regression to predict diabetes based on a dataset. It includes steps for loading data, preprocessing features, training the model, evaluating its accuracy, and making predictions based on user input. Additionally, it visualizes the glucose distribution in diabetic patients using a histogram.

Uploaded by

ebinezerm004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

1rst Exp

This document outlines a Python script that uses logistic regression to predict diabetes based on a dataset. It includes steps for loading data, preprocessing features, training the model, evaluating its accuracy, and making predictions based on user input. Additionally, it visualizes the glucose distribution in diabetic patients using a histogram.

Uploaded by

ebinezerm004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exp 01:

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import seaborn as sns

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score, confusion_matrix

# Load dataset

df = pd.read_csv('diabetes.csv')

print(df.info())

# Separate features and target

X, y = df.drop('Outcome', axis=1), df['Outcome']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Scale features

scaler = StandardScaler()

X_train_scaled, X_test_scaled = scaler.fit_transform(X_train), scaler.transform(X_test)

# Train model

model = LogisticRegression(random_state=42, max_iter=200)


model.fit(X_train_scaled, y_train)

# Evaluate model

y_pred = model.predict(X_test_scaled)

print(f"Model Accuracy: {accuracy_score(y_test, y_pred) * 100:.2f}%")

print("Confusion Matrix:")

print(confusion_matrix(y_test, y_pred))

# Get user input

def get_input(prompt, is_int=False):

while True:

try:

return int(input(prompt)) if is_int else float(input(prompt))

except ValueError:

print("Invalid input. Enter a numeric value.")

values = [get_input(prompt, is_int) for prompt, is_int in [

("Pregnancies: ", True), ("Glucose: ", False), ("Blood Pressure: ", False),

("Skin Thickness: ", False), ("Insulin: ", False), ("BMI: ", False),

("Diabetes Pedigree Function: ", False), ("Age: ", True)

]]

# Predict diabetes

new_sample = scaler.transform([values])

prediction = model.predict(new_sample)[0]

probability = model.predict_proba(new_sample)[0].max()
result = "DIABETIC" if prediction else "NOT DIABETIC"

print(f"\n** Prediction: {result} (Confidence: {probability * 100:.2f}%) **")

# Plot glucose distribution

sns.histplot(df[df['Outcome'] == 1]['Glucose'], bins=20, kde=True, color='salmon')

plt.title('Glucose Distribution in Diabetic Patients')

plt.xlabel('Glucose')

plt.ylabel('Frequency')

plt.show()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy