Introduction

This example is based on a synthetic dataset obtained from the Kaggle platform . The dataset contains fertility - related health, lifestyle, and medical indicators for couples . It includes variables such as female and male age, body mass index (BMI), menstrual regularity, polycystic ovary syndrome (PCOS) status, sperm count, sperm motility, stress levels, smoking habits, alcohol intake, treatment type, and duration of trying to conceive . The dataset is designed for machine learning applications, particularly for classification tasks such as predicting pregnancy outcomes (Success/Failure) . It is fully anonymized, making it suitable for research and educational purposes.
Infertility is a significant global health issue affecting millions of couples worldwide. It arises from a complex interaction of medical conditions, lifestyle factors, and physiological characteristics. Understanding these determinants is essential for advancing reproductive health research and developing reliable predictive models for pregnancy outcomes.
This study utilizes a carefully designed synthetic dataset that realistically represents fertility-related indicators in couples. The dataset includes a range of variables such as female and male age, body mass index (BMI), hormonal conditions (e.g., polycystic ovary syndrome, PCOS), lifestyle factors including smoking, alcohol consumption, and stress levels, as well as clinical indicators such as sperm count, sperm motility, type of treatment, and duration of attempting conception.
In terms of size and structure, the dataset consists of 800 observations (rows) and 14 features (columns). The target variable is Pregnancy_Outcome, which represents the result of conception attempts (Success or Failure). This structured format makes the dataset well-suited for supervised machine learning tasks, particularly classification problems.
To achieve a comprehensive analysis, five different machine learning models were implemented and comparatively evaluated based on their predictive performance. This comparative approach aims to identify the most effective model for predicting pregnancy outcomes, as well as to assess the relative importance of the contributing factors.
By integrating both male and female health indicators with lifestyle and treatment-related variables, this study provides a holistic perspective on reproductive health. The dataset serves as a robust foundation for exploratory data analysis, classification modeling, and the application of machine learning techniques in healthcare research.

Methodology

This study follows a structured machine learning workflow to analyze fertility - related factors and predict pregnancy outcomes . The methodology consists of multiple stages, including data preprocessing, model training, evaluation, and validation, as outlined below:
◼
  • Section 1 : Data Loading and Preprocessing
  • ◼
  • The dataset was imported into the Wolfram Mathematica environment and prepared for analysis . Preprocessing steps included handling missing or inconsistent values, encoding categorical variables, and scaling numerical features where necessary . These steps ensured that the dataset was clean, consistent, and suitable for machine learning applications.
  • ◼
  • Section 2 : Train/Test Split (80/20)
  • ◼
  • The dataset was divided into training (80 %) and testing (20 %) subsets . The training set was used to train the models, while the testing set was reserved for evaluating performance on unseen data, ensuring an unbiased assessment of model generalization.
  • ◼
  • Section 3 : Model Training
  • ◼
  • Five different machine learning models were implemented to perform the classification task of predicting pregnancy outcomes:
  • ◼
  • Model 1 : Auto (automatically selected model)
  • ◼
  • Model 2 : Balanced Priors
  • ◼
  • Model 3 : Random Forest
  • ◼
  • Model 4 : Gradient Boosted Trees
  • ◼
  • Model 5 : Logistic Regression
  • All models were trained using the same training dataset to ensure a fair and consistent comparison of their predictive capabilities.
    ◼
  • Section 4 : Evaluation—Individual Metrics
  • ◼
  • Each model was evaluated using multiple performance metrics, including accuracy, precision, recall, and F1 - score . These metrics provide a comprehensive assessment of the models’ ability to correctly classify pregnancy outcomes.
  • ◼
  • Section 5 : Confusion Matrices (Side by Side)
  • ◼
  • Confusion matrices were generated for all models and presented side by side to visually compare their classification performance . These matrices provide insight into true positives, true negatives, false positives, and false negatives.
  • ◼
  • Section 6 : 5 - Fold Cross - Validation (Best Model—Model 3)
  • ◼
  • The best - performing model, Model 3 (Random Forest), was further validated using 5 - fold cross - validation . This approach enhances reliability by evaluating model performance across multiple data splits and reduces the risk of overfitting.
  • ◼
  • Section 7: Feature Importance (Random Forest—Model 3)
  • ◼
  • Feature importance analysis was conducted using the Random Forest model to identify the most influential variables affecting pregnancy outcomes . This step improves model interpretability and highlights key contributing factors . Section 8 : Sample Predictions (Best Model—Model 3)
  • Finally, sample predictions were generated using the best - performing model (Model 3). This demonstrates the practical applicability of the model in predicting pregnancy outcomes based on input features.
    In addition to the Mathematica implementation, this project was also developed and tested on Kaggle using Python for further model analysis and comparison.
    In[]:=
    Hyperlink["My Project On Kaggle","https://www.kaggle.com/code/aishahamdy/nn-for-fertility-health-dataset"]
    My Project On Kaggle

    Section 1: Data Loading & Preprocessing

    In[]:=
    training=Import["D:\\projects\\Fertility Health Dataset\\Fertility_Health_Dataset_2026.csv","Data"];
    In[]:=
    Dimensions[training]
    Out[]=
    {801,14}
    Separate headers and rows
    headers=First[training];​​data=Rest[training];
    Fix function — handles Missing[], numerics, and strings
    fix[x_Missing]:=Missing[];​​fix[x_]:=If[NumericQ[x],x,ToString[x]];​​dataFixed=Map[fix,data,{2}];
    Build association list
    assocData=AssociationThread[headers,#]&/@dataFixed;
    Quick data summary
    Print["=== DATA SUMMARY ==="];​​Print["Total records : ",Length[assocData]];​​Print["Features : ",Length[headers]-1];​​Print["Target column : Pregnancy_Outcome"];​​Print["Class balance : ",​​Counts[assocData[[All,"Pregnancy_Outcome"]]]];
    === DATA SUMMARY ===
    Total records : 800
    Features : 13
    Target column : Pregnancy_Outcome
    === DATA SUMMARY ===
    Total records : 800
    Features : 13
    Target column : Pregnancy_Outcome
    Class balance : Success582,Failure218
    Class balance : Success582,Failure218

    Section 2: Train / Test Split (80 / 20)

    In[]:=
    SeedRandom[123];​​shuffled=RandomSample[assocData];​​n=Length[shuffled];​​nTrain=Round[0.8*n];​​​​{trainData,testData}=TakeDrop[shuffled,nTrain];​​​​Print["\n=== SPLIT ==="];​​Print["Train: ",Length[trainData]," | Test: ",Length[testData]];
    \n=== SPLIT ===
    Train: 640 | Test: 160
    Train: 640 | Test: 160

    Section 3: Model Training

    Model 1: Auto (Mathematica chooses best algorithm)
    Print["\nTraining model1 (Auto)..."];​​model1=Classify[​​trainData->"Pregnancy_Outcome"​​];
    \nTraining model1 (Auto)...
    Model 2: Balanced priors (equal class weights)
    Print["Training model2 (Balanced Priors)..."];​​model2=Classify[​​trainData->"Pregnancy_Outcome",​​ClassPriors-><|"Failure"->0.5,"Success"->0.5|>​​];
    Training model2 (Balanced Priors)...
    Model 3: Random Forest (explicit)
    Print["Training model3 (RandomForest)..."];​​model3=Classify[​​trainData->"Pregnancy_Outcome",​​Method->"RandomForest"​​];
    Training model3 (RandomForest)...
    Model 4: Gradient Boosted Trees
    Print["Training model4 (GradientBoostedTrees)..."];​​model4=Classify[​​trainData->"Pregnancy_Outcome",​​Method->"GradientBoostedTrees"​​];
    Training model4 (GradientBoostedTrees)...
    Model 5: Logistic Regression
    Print["Training model5 (LogisticRegression)..."];​​model5=Classify[​​trainData->"Pregnancy_Outcome",​​Method->"LogisticRegression"​​];
    Training model5 (LogisticRegression)...
    In[]:=
    Print["All models trained."];​​
    All models trained.

    Section 4: Evaluation — Individual Metrics

    cm1=ClassifierMeasurements[model1,testData];​​cm2=ClassifierMeasurements[model2,testData];​​cm3=ClassifierMeasurements[model3,testData];​​cm4=ClassifierMeasurements[model4,testData];​​cm5=ClassifierMeasurements[model5,testData];
    Helper: extract all key metrics for one cm object
    Comparison table

    Section 5: Confusion Matrices (side by side)

    Section 6: 5 - Fold Cross - Validation (on best model — model3)

    Section 7: Feature Importance (RandomForest — model3)

    Section 8: Sample Predictions (model3 — best model)

    The model correctly classified 4 out of 5 randomly selected test cases . However, it misclassified one failure case as success, indicating a bias toward the majority class.

    Conclusion

    This project applied machine learning techniques in Mathematica to predict fertility outcomes . The model achieved good accuracy and showed that factors such as female age and sperm characteristics play a significant role . However, the model displayed some bias toward the majority class . Future improvements can focus on handling class imbalance and testing more advanced models . The project was also explored on Kaggle using Python for further analysis.

    References and sources

    CITE THIS NOTEBOOK

    Fertility health dataset: improved classification analysis​
    by Aisha Hassan​
    Wolfram Community, STAFF PICKS, June 3, 2026
    https://community.wolfram.com/groups/-/m/t/3726721