<jupyter_start><jupyter_text>__Target Variable: Stroke__<jupyter_code>df['stroke'].value_counts()
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>__Gender, ever_married, work_type, Residence_type, smoking_status, hypertension, heart_disease : Categorical Variables__  __age , avg_glucose_level, bmi : Numerical Variables__<jupyter_code>df['gender'].value_counts()
df['hypertension'].value_counts()
df['heart_disease'].value_counts()
df['ever_married'].value_counts()
df['work_type'].value_counts()
df['smoking_status'].value_counts()
df['Residence_type'].value_counts()<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>df = df[df['gender']!='Other'].copy()
df.head()
df.columns
df = pd.get_dummies(columns=['gender','ever_married',
       'work_type', 'Residence_type','smoking_status'], data = df, prefix_sep= '_', drop_first= True)
df.head()
df.info()
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>bmi has ~200 missing values, which can be imputed.<jupyter_code>from sklearn.impute import KNNImputer
knn_impute = KNNImputer(n_neighbors= 10, weights= 'distance')
imputed_bmi = knn_impute.fit_transform(df[['bmi']])
df = df.drop(columns=['bmi'], axis= 1)
df.head()
df['bmi_imputed'] = imputed_bmi
df.head()<jupyter_output><empty_output><jupyter_text>Modeling<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
df.columns
X = df.drop(columns=['id','stroke'])
y = df['stroke']
X.shape, y.shape
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=420)
X_train.shape , X_test.shape
model_rf = RandomForestClassifier(n_estimators= 100 , n_jobs= -1, max_depth=2, criterion= 'entropy')
model_svm = SVC()
model_dt = DecisionTreeClassifier(criterion='gini', splitter= 'best')
model_lr = LogisticRegression()
model_rf.fit(X_train, y_train)
model_svm.fit(X_train, y_train)
model_dt.fit(X_train, y_train)
#model_lr.fit(X_train,y_train)
y_pred_rf = model_rf.predict(X_test)
y_pred_svm = model_svm.predict(X_test)
y_pred_dt = model_dt.predict(X_test)
y_pred_lr = model_lr.predict(X_test)
print('Accuracy of Random Forest', np.round(accuracy_score(y_pred= y_pred_rf, y_true = y_test),3))
print('Accuracy of SVM', np.round(accuracy_score(y_pred= y_pred_svm, y_true = y_test),3))
print('Accuracy of Decision Tree', np.round(accuracy_score(y_pred= y_pred_dt, y_true = y_test),3))
print('Accuracy of Logistic Regression', np.round(accuracy_score(y_pred= y_pred_lr, y_true = y_test),3))<jupyter_output>Accuracy of Random Forest 0.951
Accuracy of SVM 0.951
Accuracy of Decision Tree 0.913
Accuracy of Logistic Regression 0.951
StarcoderdataJupyterStructuredCleanDedup