Machine Learning project 14
Machine Learning Diabetes Prediction Project
Build and evaluate a complete diabetes prediction workflow with reproducible Python code, explicit metrics, and honest limitations.
Explore Machine Learning training in VizagView all project ideas
Dataset and objective
Scikit-learn’s built-in diabetes regression dataset with 442 samples and a continuous one-year progression target.
Skills practised
- Regularised regression
- RidgeCV
- Continuous targets
- Medical wording accuracy
Requirements
- Python 3.10 or later
- A terminal or command prompt
python -m pip install scikit-learn- About 45-60 minutes to build and review
Machine Learning workflow
- Data: Scikit-learn’s built-in diabetes regression dataset with 442 samples and a continuous one-year progression target.
- Preprocessing: A random holdout split and StandardScaler inside a pipeline prepare the ten numeric features.
- Model: RidgeCV selects regularisation strength from four candidate alpha values.
- Evaluation: MAE, RMSE, and R-squared on the untouched holdout rows.
Complete Python code
Save the code as ml_diabetes_prediction.py. The random state and data handling are included so the result can be reproduced and reviewed.
"""Predict a continuous diabetes-progression score for teaching regression."""
from __future__ import annotations
from math import sqrt
from sklearn.datasets import load_diabetes
from sklearn.linear_model import RidgeCV
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
def train_model(random_state: int = 42):
dataset = load_diabetes(as_frame=True)
x_train, x_test, y_train, y_test = train_test_split(
dataset.data, dataset.target, test_size=0.25, random_state=random_state
)
model = make_pipeline(StandardScaler(), RidgeCV(alphas=(0.1, 1.0, 10.0, 100.0)))
model.fit(x_train, y_train)
predictions = model.predict(x_test)
metrics = {
"mae": mean_absolute_error(y_test, predictions),
"rmse": sqrt(mean_squared_error(y_test, predictions)),
"r2": r2_score(y_test, predictions),
}
return model, dataset, metrics
def main() -> None:
_, dataset, metrics = train_model()
print("Diabetes Progression Regression")
print(f"Samples: {len(dataset.data)}")
print(f"MAE: {metrics['mae']:.2f}")
print(f"RMSE: {metrics['rmse']:.2f}")
print(f"R-squared: {metrics['r2']:.3f}")
print("The target is a one-year disease-progression measure—not diabetes diagnosis or patient advice.")
if __name__ == "__main__":
main()
How the pipeline works
A random holdout split and StandardScaler inside a pipeline prepare the ten numeric features.
RidgeCV selects regularisation strength from four candidate alpha values. MAE, RMSE, and R-squared on the untouched holdout rows.
Run the project
- Create and activate a virtual environment.
- Install the dependencies with
python -m pip install scikit-learn. - Run
python ml_diabetes_prediction.py. - Review every printed metric together with the limitation below; a single score never proves deployment readiness.
How to interpret the evaluation
Classification projects report class-aware metrics so majority classes do not hide weak performance. Regression projects report errors in target units and include R-squared or a simple baseline where appropriate. Always verify the split strategy matches how new data will arrive.
Accuracy, ethics, and safety limits
Despite the short card title, this code does not diagnose diabetes. It predicts a research dataset’s progression measure and is not clinical advice or a patient-care tool.
Ways to extend the project
Use nested cross-validation, inspect coefficients, assess stability, compare Elastic Net, and study the dataset documentation before interpreting any feature.
Continue learning Machine Learning
Try the next project, return to the Softenant project library, or explore the Machine Learning course in Vizag for guided data preparation, model evaluation, and portfolio feedback.