<jupyter_start><jupyter_text>**Stroke Predictor**A stroke, also known as a cerebrovascular accident or CVA is when part of the brain loses its blood supply and the part of the body that the blood-deprived brain cells control stops working. This loss of blood supply can be ischemic because of lack of blood flow, or hemorrhagic because of bleeding into brain tissue.The model designed in the following notebook studies and analysis data of 29065 humans have of the following features1.Gender2.Age 3.Hypertension4.Heart_disease5.Ever_married	6.Work_type	7.Residence_type8.Avg_glucose_level9.BMI10.Smoking_status.In the dataset along with the 10 above features the  column at the last specifies if the person suffered with a stoke or not.(Yes -> 1 and No -> 0)After analysing the 29065 rows of the dataset and it coreesponding values and results the model can predict for a person with aset of new values if will suffer with a stroke or not.<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv('.\stroke_data.csv')
df.head()
df.corr()
df.dtypes
df.shape
# Storing a copy of the data set. Not necessary but a good parctice when changing the structure and elements the dataset
df_copy = df
#Checking for null values
df.isnull().sum()
y = df['stroke']
# Using Dummies variables for tranforming the categorical features.

dummies_1 = pd.get_dummies(df['gender'],drop_first=True)
df = pd.concat([df.drop('gender',axis=1),dummies_1],axis=1)

dummies_2 = pd.get_dummies(df['ever_married'],drop_first=True)
df = pd.concat([df.drop('ever_married',axis=1),dummies_2],axis=1)

dummies_3 = pd.get_dummies(df['work_type'],drop_first=True)
df = pd.concat([df.drop('work_type',axis=1),dummies_3],axis=1)

dummies_4 = pd.get_dummies(df['Residence_type'],drop_first=True)
df = pd.concat([df.drop('Residence_type',axis=1),dummies_4],axis=1)

dummies_5 = pd.get_dummies(df['smoking_status'],drop_first=True)
df = pd.concat([df.drop('smoking_status',axis=1),dummies_5],axis=1)

df.dtypes
df.shape
# Dropping the 'stroke' column as it is in the middle of the dataset. The later lines with transfer it at the end.
# Not necessary but makes it easy for splitting the dataset.
df = df.drop('stroke',axis=1)
df.columns.to_list()
# adds the 'stroke' columns at last position in the dataset
df['stroke'] = y
df.columns.to_list()
X = df.drop('stroke',axis=1).values
y = df['stroke'].values
print(df.shape)
print(X.shape)
print(y.shape)
# Splitting the dataset into train and test set.
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
# Scaling the train set.
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler() 
x_train_scaled = scaler.fit_transform(x_train)
x_test_scaled = scaler.transform(x_test)
# Creating the model 
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout
from tensorflow.keras.callbacks import EarlyStopping

model = Sequential()
model.add(Dense(units=64,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(units=32,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(units=16,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(units=1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])

# Creating an instance for early stopping
early_stop = EarlyStopping(monitor='val_loss',patience=4)
# Fitting the training set to the model and validating it on the test set.
model.fit(x=x_train_scaled,y=y_train,validation_data=(x_test_scaled,y_test),
          epochs=5,callbacks=[early_stop])
results = pd.DataFrame(model.history.history)
# Looking at the graph we can be sure that the model is not overfitting on the dataset
results[['loss','val_loss']].plot()
results[['accuracy','val_accuracy']].plot()
# Evaluating the model on the test set.
test_loss,test_accuracy = model.evaluate(x_test_scaled,y_test,verbose=1)
print('Loss on test set: ',loss)
print('Accuracy on test set: ',accuracy)
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                multiple                  960       
_________________________________________________________________
dropout (Dropout)            multiple                  0         
_________________________________________________________________
dense_1 (Dense)              multiple                  2080      
_________________________________________________________________
dropout_1 (Dropout)          multiple                  0         
_________________________________________________________________
dense_2 (Dense)              multiple                  528       
_________________________________________________________________
dropout_2 (Dropout)          multiple                  0         
________________________________________________________[...]
{'fraction_non_alphanumeric': 0.10756432578835365, 'fraction_numerical': 0.01625072547881602, 'mean_word_length': 5.111111111111111, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '526254', 'license': 'Apache-2.0', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1503, 'n_words': 422, 'path': 'Stroke_Predictor/Stroke_Predictor_Model_.ipynb', 'repo_name': 'kushagras71/ANN_models', 'size': '5169'}