image_output_1.png
from imblearn.under_sampling import RandomUnderSampler

import pandas as pd

df = pd.read_csv('../input/full-filled-brain-stroke-dataset/full_data.csv')

df[['hypertension', 'heart_disease', 'stroke']] = df[['hypertension', 'heart_disease', 'stroke']].astype('object')

df.isnull().sum()

data = df[(df['avg_glucose_level'] <= 160) & (df['bmi'] <= 45)]
data = data.reset_index(drop=True)

features = data.drop('stroke', axis=1).columns
features

cat_features = []
nmr_features = []
for i in range(len(features)):
    if df.iloc[:, i].dtype == 'object':
        cat_features.append(df.columns[i])
    else:
        nmr_features.append(df.columns[i])

data = pd.get_dummies(data, columns=cat_features, drop_first=True)

x = data.drop('stroke', axis=1)
y = data['stroke'].astype('int')

from imblearn.under_sampling import RandomUnderSampler
undersample = RandomUnderSampler(sampling_strategy=0.5)
x_under, y_under = undersample.fit_resample(x, y)

pd.DataFrame(y_under).value_counts()