Professional Documents
Culture Documents
Fetal Health Analysis Dense Model: # Importing The Dependince
Fetal Health Analysis Dense Model: # Importing The Dependince
# Data
import pandas as pd
# Data Visualization
import seaborn as sns
import matplotlib.pyplot as plt
# Data Preprocessing
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import StratifiedShuffleSplit, train_test_
# Model
from keras import Sequential
from keras.layers import Dense, Dropout
# Callbacks
from keras.callbacks import EarlyStopping, ModelCheckpoint
# Model Performance
from sklearn.metrics import accuracy_score, precision_score, recall_sco
Data Collection
In [3]: #loading the dataset in pandas datafram
file_path = 'fetal_health.csv'
data = pd.read_csv(file_path)
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 1 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
Out[4]:
baseline
accelerations fetal_movement uterine_contractions light_decelerations severe_dece
value
5 rows × 22 columns
Out[5]:
baseline
accelerations fetal_movement uterine_contractions light_decelerations severe_d
value
5 rows × 22 columns
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 2 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
Out[7]:
baseline
accelerations fetal_movement uterine_contractions light_decelerations
value
8 rows × 22 columns
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2126 entries, 0 to 2125
Data columns (total 22 columns):
# Column Non-N
ull Count Dtype
--- ------ -----
--------- -----
0 baseline value 2126
non-null float64
1 accelerations 2126
non-null float64
2 fetal_movement 2126
non-null float64
3 uterine_contractions 2126
non-null float64
4 light_decelerations 2126
non-null float64
5 severe_decelerations 2126
non-null float64
6 prolongued_decelerations 2126
non-null float64
7 abnormal_short_term_variability 2126
non-null float64
8 mean_value_of_short_term_variability 2126
non-null float64
9 percentage_of_time_with_abnormal_long_term_variability 2126
non-null float64
10 mean_value_of_long_term_variability 2126
non-null float64
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 3 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
non-null float64
11 histogram_width 2126
non-null float64
12 histogram_min 2126
non-null float64
13 histogram_max 2126
non-null float64
14 histogram_number_of_peaks 2126
non-null float64
15 histogram_number_of_zeroes 2126
non-null float64
16 histogram_mode 2126
non-null float64
17 histogram_mean 2126
non-null float64
18 histogram_median 2126
non-null float64
19 histogram_variance 2126
non-null float64
20 histogram_tendency 2126
non-null float64
21 fetal_health 2126
non-null float64
dtypes: float64(22)
memory usage: 365.5 KB
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 4 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
Data Visualization
In [13]: for i in data.columns:
plt.figure()
plt.hist(data[i])
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 5 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
plt.figure(figsize=(15,10))
sns.heatmap(corr, annot=True)
plt.show()
Their are many relations let's have a look at the most strongest ones.
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 6 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
plt.figure(figsize=(15,10))
plt.title("Strong Positive Relation")
sns.heatmap(corr, annot=True)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 7 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
plt.figure(figsize=(15,10))
plt.title("Strong Negative Relation")
sns.heatmap(corr, annot=True)
plt.show()
You can change the Threshold Values if you want to, but I consider that the relation
should have a score >0.7(70%). Let's plot these relations. Before plotting the relations
we will also plot the individual column.
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 8 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [217]: plt.figure(figsize=(10,8))
sns.histplot(data['histogram_mode'], kde=True)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 9 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [218]: sns.catplot(
data=data,
y='histogram_mode',
x='fetal_health',
kind='box',
aspect=1.5
)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 10 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [219]: plt.figure(figsize=(10,8))
sns.histplot(data['baseline value'], kde=True)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 11 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [220]: sns.catplot(
data=data,
y='baseline value',
x='fetal_health',
kind='box',
aspect=1.5
)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 12 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [221]: plt.figure(figsize=(10,8))
sns.scatterplot(
data=data,
x='histogram_mode',
y='baseline value',
hue='fetal_health'
)
plt.show()
Clearly they have a Positive Linear Relationship, the interesting this is the *class
distribution :
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 13 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [222]: plt.figure(figsize=(10,8))
sns.histplot(data['histogram_mean'], kde=True)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 14 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [223]: sns.catplot(
data=data,
y='histogram_mean',
x='fetal_health',
kind='box',
aspect=1.5
)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 15 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [224]: plt.figure(figsize=(10,8))
sns.histplot(data['histogram_median'], kde=True)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 16 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [225]: sns.catplot(
data=data,
y='histogram_median',
x='fetal_health',
kind='box',
aspect=1.5
)
plt.show()
Both belong to the same distribution that's why the data distribution range is roughly
the same.
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 17 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [226]: plt.figure(figsize=(10,8))
sns.scatterplot(
data=data,
x='histogram_mean',
y='baseline value',
hue='fetal_health'
)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 18 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [227]: plt.figure(figsize=(10,8))
sns.scatterplot(
data=data,
x='histogram_median',
y='baseline value',
hue='fetal_health'
)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 19 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [228]: plt.figure(figsize=(10,8))
sns.histplot(data['histogram_width'], kde=True)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 20 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [229]: plt.figure(figsize=(10,8))
sns.histplot(data['histogram_number_of_peaks'], kde=True)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 21 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [230]: plt.figure(figsize=(10,8))
sns.scatterplot(
data=data,
x='histogram_number_of_peaks',
y='histogram_width',
hue='fetal_health'
)
plt.show()
The Linear Relation is clear but the classes are not clearly seperable.
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 22 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [231]: sns.catplot(
data=data,
y='histogram_width',
x='fetal_health',
kind='box',
aspect=1.5
)
plt.show()
This confirms that the classes are overlapping each other. The overlapping is high, this
variable alone cannot decide the class.
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 23 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [232]: plt.figure(figsize=(10,8))
sns.scatterplot(
data=data,
x='histogram_mode',
y='histogram_mean',
hue='fetal_health'
)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 24 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [233]: plt.figure(figsize=(10,8))
sns.scatterplot(
data=data,
x='histogram_mode',
y='histogram_median',
hue='fetal_health'
)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 25 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [234]: plt.figure(figsize=(10,8))
sns.scatterplot(
data=data,
x='histogram_median',
y='histogram_mean',
hue='fetal_health'
)
plt.show()
This had to be the case after-all they are statically related. The classes are seperable,
thus it can be easy for the model to detect the hidden pattern.
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 26 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [235]: plt.figure(figsize=(10,8))
sns.histplot(data['histogram_min'], kde=True)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 27 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [236]: sns.catplot(
data=data,
y='histogram_min',
x='fetal_health',
kind='box',
aspect=1.5
)
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 28 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [237]: plt.figure(figsize=(10,8))
sns.scatterplot(
data=data,
x='histogram_min',
y='histogram_width',
hue='fetal_health'
)
plt.show()
The Negative Relationship can easily be noted, however the classes are not easily
seperable. We are done with data understanding it's time for the model to understand
the data. But before doing that, we need to prepared the data.
Data Splitting
The Features are not on the same scale, let's bring them all to one scale.
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 29 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
Let's split the data into training, testing and validation data. To maintain the Class
Distribution, we will using Stratified Splitting.
Feature Selection
This is an optional step, as we will be using a Dense Model it will automatically perform
Feature Selection, but if you are using a Machine Learning Model you can use it.
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 30 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
In [244]: plt.figure(figsize=(10,8))
sns.barplot(
x=feature_names,
y=feature_imps
)
plt.axhline(np.mean(feature_imps), color='k', linestyle="--", alpha
plt.xticks(rotation=90)
plt.legend()
plt.show()
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 31 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
Many features are below the Mean Importance, you can drop them it will no affect the
model much. Dense Models can do this on their own, so we don't have to worry about
it. You can do multiple runs and get the mean, that will be a Robust Prediction but it
will generally be the same.
Dense Model
In [325]: name = "dense"
# Model Architecture
model = Sequential([
Dense(32, activation='relu', kernel_initializer='he_normal'),
Dense(64, activation='relu', kernel_initializer='he_normal'),
Dense(128, activation='relu', kernel_initializer='he_normal'),
Dropout(0.2),
Dense(3, activation='softmax'),
], name=name)
# Compile
model.compile(
loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
# Callbacks
cbs = [EarlyStopping(patience=3, restore_best_weights=True), ModelCheck
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 32 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
Epoch 1/15
48/48 [==============================] - 1s 7ms/step - loss: 0.627
3 - accuracy: 0.7876 - val_loss: 0.3977 - val_accuracy: 0.8590
Epoch 2/15
48/48 [==============================] - 0s 3ms/step - loss: 0.380
6 - accuracy: 0.8497 - val_loss: 0.3139 - val_accuracy: 0.8721
Epoch 3/15
48/48 [==============================] - 0s 3ms/step - loss: 0.313
9 - accuracy: 0.8778 - val_loss: 0.2940 - val_accuracy: 0.8825
Epoch 4/15
48/48 [==============================] - 0s 3ms/step - loss: 0.283
3 - accuracy: 0.8869 - val_loss: 0.2761 - val_accuracy: 0.8825
Epoch 5/15
48/48 [==============================] - 0s 3ms/step - loss: 0.245
8 - accuracy: 0.8987 - val_loss: 0.2667 - val_accuracy: 0.8930
Epoch 6/15
48/48 [==============================] - 0s 3ms/step - loss: 0.224
5 - accuracy: 0.9033 - val_loss: 0.2658 - val_accuracy: 0.8956
Epoch 7/15
48/48 [==============================] - 0s 5ms/step - loss: 0.219
1 - accuracy: 0.9111 - val_loss: 0.2634 - val_accuracy: 0.9008
Epoch 8/15
48/48 [==============================] - 0s 3ms/step - loss: 0.203
4 - accuracy: 0.9255 - val_loss: 0.2414 - val_accuracy: 0.9034
Epoch 9/15
48/48 [==============================] - 0s 3ms/step - loss: 0.194
6 - accuracy: 0.9255 - val_loss: 0.2468 - val_accuracy: 0.9060
Epoch 10/15
48/48 [==============================] - 0s 3ms/step - loss: 0.182
9 - accuracy: 0.9261 - val_loss: 0.2375 - val_accuracy: 0.9034
Epoch 11/15
48/48 [==============================] - 0s 3ms/step - loss: 0.171
2 - accuracy: 0.9294 - val_loss: 0.2466 - val_accuracy: 0.8982
Epoch 12/15
48/48 [==============================] - 0s 3ms/step - loss: 0.158
2 - accuracy: 0.9320 - val_loss: 0.2364 - val_accuracy: 0.9086
Epoch 13/15
48/48 [==============================] - 0s 3ms/step - loss: 0.145
5 - accuracy: 0.9418 - val_loss: 0.2488 - val_accuracy: 0.9060
Epoch 14/15
48/48 [==============================] - 0s 3ms/step - loss: 0.154
1 - accuracy: 0.9366 - val_loss: 0.2660 - val_accuracy: 0.8956
Epoch 15/15
48/48 [==============================] - 0s 3ms/step - loss: 0.144
5 - accuracy: 0.9412 - val_loss: 0.2246 - val_accuracy: 0.9138
Evaluation
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 33 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
The training is promising, let's check model's performance on the data ir has never seen
before.
Performance Analysis
Let's see model's performance on various metrics.
# Calculate Score
acc_score = accuracy_score(y_test, pred)
pre_score = precision_score(y_test, pred, average='macro')
re_score = recall_score(y_test, pred, average='macro')
f_score = f1_score(y_test, pred, average='macro')
# Show Prediction
print("Accuracy Score : {:.6}".format(acc_score))
print("Precision Score : {:.6}".format(pre_score))
print("Recall Score : {:.6}".format(re_score))
print("f1 Score : {:.6}".format(f_score))
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 34 of 35
fetal-health-analysis-dense-model-acc-94 - Jupyter Notebook 28/02/23, 11:07 PM
http://localhost:8888/notebooks/Downloads/fetal-health-analysis-dense-model-acc-94.ipynb#Data Page 35 of 35