The objective of this repository is to create a model based on medical data and implement a client/server web interface using Flask to allow for predictions to be made based on the created model.
The data relates to pregnant patients and the presence or absence of the "placenta previa" pathology. Placenta previa is a condition in which the placenta attaches to the lower part of the uterus, partially or totally covering the internal cervical os. It affects about 1% of pregnancies and is associated with a risk of antepartum hemorrhage, preterm birth, cesarean section, and complications for both mother and fetus. The available dataset describes the clinical records of pregnant patients and whether they developed placenta previa.
- Age
- NumberOfPreviousPregnancies
- NumberOfPreviousCesareanSections
- PreviousPlacentaPrevia (0 or 1)
- SecondTrimesterBleeding (0 or 1)
- TwoHourSerumInsulin (microunits per milliliter of blood)
- BodyMassIndex (weight in kilograms divided by the square of the height in meters)
- AssistedFertilization (0 or 1)
- PatientAffectedByPlacentaPrevia (0 or 1 -> target column)
- Load the JSON data and check for any missing data, outliers, or inconsistencies. Replace missing/anomalous data.
- Calculate descriptive statistics (mean, median, standard deviation) for the variables.
- Analyze correlations to find the variables most associated with placenta previa.
- Encode any categorical variables and Normalize/Standardize numerical variables.
- Split the data into training/testing sets (80%/20%).
- Choose a classification algorithm and perform a search for the best parameters.
- Evaluate the final model and create a
PredictPlacentaPrevia(...)function that takes the variables as input and returns the prediction (0 or 1) and the probability. - Implement a Web API with a
/predictendpoint that accepts a POST request with the patient's data in JSON (raw json), calls thePredictPlacentaPrevia(...)function, and returns the data in JSON. - Create a web page (HTML + JavaScript) to enter the patient's data. Add a button to send the data to the Flask endpoint via an AJAX/fetch API call. Display the prediction (probability of placenta previa) on the page.
This section provides instructions on how to set up and run the project, and how to use its different components.
- Python 3.8 or higher
- pip (Python package installer)
-
Clone the repository:
git clone <repository-url> cd repository-name
-
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install the required dependencies:
A
requirements.txtfile should be created to list all the necessary libraries.
Once the dependencies are installed, you can start the Flask web server.
-
Navigate to the
srcdirectory:cd src -
Run the Flask application:
python app.py
The application will start on
http://127.0.0.1:5000.
The application provides a RESTful API for making predictions.
-
Method:
POST -
Description: Receives patient data in JSON format and returns a prediction for the risk of placenta previa.
-
Request Body: A JSON object containing the patient's data.
Example Request Body:
{ "Eta": 25, "NumeroGravidanzePregresse": 2, "NumeroTagliCesareiPregressi": 2, "PrecedentePlacentaPrevia": 1, "SanguinamentiNelSecondoTrimestre": 1, "InsulinaSiericaDueOre": 110, "IndiceDiMassaCorporea": 32.5, "FecondazioneAssisitita": 0 } -
Response: A JSON object containing the prediction and the probability of having placenta previa.
prediction:1if the risk is high,0if the risk is low.probability: The probability of the positive class (placenta previa), as a float between 0 and 1.
Example Response:
{ "prediction": 1, "probability": 0.6275 }
The project includes a simple web interface to interact with the prediction model.
- Open your web browser and navigate to
http://127.0.0.1:5000. - Fill in the form with the patient's data. All fields are required.
- Click the "Calcola Rischio" (Calculate Risk) button.
- The prediction result will be displayed below the button, showing the probability of placenta previa and a final outcome (e.g., "POSITIVE (High Risk)" or "NEGATIVE (Low Risk)").