# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
dframe = pd.read_csv(r"C:\Users\thapa\PycharmProjects\PRProject\train_2v.csv")

print(dframe['stroke'].value_counts())



print(dframe.isnull().sum().sort_values(ascending=False).head())

dframe = dframe.dropna(subset=['smoking_status']) 

print(dframe['stroke'].value_counts())

print(dframe.isnull().sum().sort_values(ascending=False).head())

print(dframe['bmi'].mean())
print(dframe['bmi'].median())

# +
dframe['bmi'].fillna(x_initial['bmi'].mean(), inplace=True)

print(dframe.isnull().sum().sort_values(ascending=False).head())

print(dframe.head(5))


# +
def gendervalue(a):
    if a == 'Male':
        return 1
    if a == 'Female':
        return 0
    if a == 'Other':
        return 3
    
dframe['gender'] = dframe['gender'].apply(gendervalue)
# -

print(dframe['work_type'].value_counts().sort_values(ascending=False).head(10))


# +
def ever_married_value(a):
    if a == 'Yes':
        return 1
    if a == 'No':
        return 0

dframe['ever_married'] = dframe['ever_married'].apply(ever_married_value)


# +
def work_type_class(a):
    if a == 'Private':
        return 0
    if a == 'Self-employed':
        return 1
    if a == 'children':
        return 2
    if a == 'Govt_job':
        return 3
    if a == 'Never_worked':
        return 4
    
dframe['work_type'] = dframe['work_type'].apply(work_type_class)


# +
def Residence_type_value(a):
    if a == 'Urban':
        return 1
    if a == 'Rural':
        return 0
    
dframe['Residence_type'] = dframe['Residence_type'].apply(Residence_type_value)

def smoking_status_class(a):
    if a == 'never smoked':
        return 0
    if a == 'formerly smoked':
        return 1
    if a == 'smokes':
        return 2
    
dframe['smoking_status'] = dframe['smoking_status'].apply(smoking_status_class)

print(dframe.head(5))

# +

# Use numpy to convert to arrays
import numpy as np
# Labels are the values we want to predict
labels = np.array(dframe['stroke'])
# Remove the labels from the features
# axis 1 refers to the columns
features= dframe.drop('stroke', axis = 1)
# Saving feature names for later use
feature_list = list(features.columns)
# Convert to numpy array
features = np.array(features)
# -

# Using Skicit-learn to split data into training and testing sets
from sklearn.model_selection import train_test_split
# Split the data into training and testing sets
train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size = 0.25, random_state = 42)

print('Training Features Shape:', train_features.shape)
print('Training Labels Shape:', train_labels.shape)
print('Testing Features Shape:', test_features.shape)
print('Testing Labels Shape:', test_labels.shape)

# +
#Import Random Forest Model
from sklearn.ensemble import RandomForestClassifier

#Create a Gaussian Classifier
clf=RandomForestClassifier(n_estimators=100)

#Train the model using the training sets y_pred=clf.predict(X_test)
clf.fit(train_features,train_labels)

y_pred=clf.predict(test_features)

# from sklearn.svm import SVC
# svm = SVC(kernel='sigmoid')
# svm.fit(train_features, train_labels)
# print('Accuracy of SVM classifier on training set: {:.2f}'
#      .format(svm.score(train_features, train_labels)))
# print('Accuracy of SVM classifier on test set: {:.2f}'
#      .format(svm.score(test_features, test_labels)))


# -

#Import scikit-learn metrics module for accuracy calculation
from sklearn import metrics
# Model Accuracy, how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(test_labels, y_pred))



StarcoderdataJupyterScriptsDedupFiltered