-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
35 lines (27 loc) · 1022 Bytes
/
Copy pathtrain_model.py
File metadata and controls
35 lines (27 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
import joblib # For saving model
# Load dataset
df = pd.read_csv("diabetes.csv")
# Features and target
X = df.drop("Outcome", axis=1)
y = df["Outcome"]
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Train the model
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
# Make predictions on test data
y_pred = model.predict(X_test)
# Evaluate the model
print("\n--- Model Evaluation ---")
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))
# Save model to a file for Django use later
joblib.dump(model, "diabetes_model.pkl")
print("Model saved as diabetes_model.pkl successfully!")