<jupyter_start><jupyter_text>Generating the 'command' for changin directory of the data set<jupyter_code>import os
print("Enter the directory/path of the dataset: ")
str1 = input()
directory=str1.replace('\\', '\\\\')
print("\nos.chdir('"+(directory)+"')")<jupyter_output>Enter the directory/path of the dataset: 


os.chdir('')<jupyter_text>Copy and paste the 'generated command' down below<jupyter_code>os.chdir('E:\\aBAC\\LTU sem 1\\python')
os.getcwd()<jupyter_output><empty_output><jupyter_text>Start<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math

import seaborn as sns

%matplotlib inline
strk=pd.read_csv("healthcare-dataset-stroke-data.csv")<jupyter_output><empty_output><jupyter_text>Checking the Data set which I loaded<jupyter_code>strk.head()<jupyter_output><empty_output><jupyter_text>Checking the null values inside the data set<jupyter_code>strk.isnull().sum()<jupyter_output><empty_output><jupyter_text>bmi = Body Mass Index Dropping "bmi" column as it has most of the NA/NULL values.<jupyter_code>strk.drop('bmi', axis=1, inplace=True)
strk.isnull().sum()
strk.info()
sns.countplot(x='stroke', data=strk)
sns.countplot(x='Residence_type', data=strk)
sns.countplot(x='ever_married', data=strk)
sns.countplot(x='work_type', data=strk)
sns.countplot(x='smoking_status', data=strk)
sns.countplot(x='ever_married', hue='stroke', data=strk)
sns.countplot(x='gender', hue='stroke', data=strk)
sns.countplot(x='ever_married', hue='stroke', data=strk)
sns.countplot(x='hypertension', hue='stroke', data=strk)
sns.countplot(x='smoking_status', hue='stroke', data=strk)<jupyter_output><empty_output><jupyter_text>Removing string values one by one columnwise, starting from 'gender' by using panda's 'get_dummies' function. Removing string from 'gender'<jupyter_code>pd.get_dummies(strk['gender']).head()<jupyter_output><empty_output><jupyter_text>Dropping the 'Other' column as we can predict the 'Other' value with the help of other two values. New column name: sex<jupyter_code>sex = pd.get_dummies(strk['gender'])
sex.drop('Other', axis=1, inplace=True)
sex.head()<jupyter_output><empty_output><jupyter_text>Converting 'age' into int from float. New column name: int_age<jupyter_code>int_age = strk["age"].astype(int)
int_age.head()
strk.info()
strk.rename(columns = {'age':'f_age'}, inplace=True)<jupyter_output><empty_output><jupyter_text>Removing string from 'ever_married' column and then removing the first column 'No' New column name: em<jupyter_code>em=pd.get_dummies(strk['ever_married'],drop_first=True)
em.head()<jupyter_output><empty_output><jupyter_text>Removing string from 'Residence_type' and also the first column 'Rural' New column name: rt<jupyter_code>rt=pd.get_dummies(strk['Residence_type'],drop_first=True)
rt.head()<jupyter_output><empty_output><jupyter_text>Removing 'avg_glucose_level' column & smoking status column<jupyter_code>strk.drop(['avg_glucose_level','smoking_status','work_type'], axis=1, inplace=True)
strk.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 5110 entries, 0 to 5109
Data columns (total 8 columns):
 #   Column          Non-Null Count  Dtype  
---  ------          --------------  -----  
 0   id              5110 non-null   int64  
 1   gender          5110 non-null   object 
 2   f_age           5110 non-null   float64
 3   hypertension    5110 non-null   int64  
 4   heart_disease   5110 non-null   int64  
 5   ever_married    5110 non-null   object 
 6   Residence_type  5110 non-null   object 
 7   stroke          5110 non-null   int64  
dtypes: float64(1), int64(4), object(3)
memory usage: 319.5+ KB<jupyter_text>Merging all the new columns<jupyter_code>strk=pd.concat([strk,sex,int_age,em,rt], axis=1)
strk.head()<jupyter_output><empty_output><jupyter_text>And now dropping the old columns associated with the new columns<jupyter_code>strk.drop(['id','gender','f_age','ever_married','Residence_type'], axis=1, inplace=True)<jupyter_output><empty_output><jupyter_text>New Data set<jupyter_code>strk.head()<jupyter_output><empty_output><jupyter_text>Renaming the column 'Yes' to 'Ever married'<jupyter_code>strk.rename(columns = {'Yes':'Ever married'}, inplace=True)
strk.head()
strk.rename(columns = {'hypertension':'Hypertension'}, inplace=True)
strk.rename(columns = {'heart_disease':'Heart disease'}, inplace=True)
strk.rename(columns = {'stroke':'Stroke'}, inplace=True)
strk.rename(columns = {'age':'Age'}, inplace=True)
strk.head(10)<jupyter_output><empty_output><jupyter_text>Catagorize age into two section, age > 50 or not.<jupyter_code>def change_age(cols):
    Age= cols[0]
    
    if Age>=50:
        return 1
    else:
        return 0<jupyter_output><empty_output><jupyter_text>Final data set after 'Data wrangling'<jupyter_code>strk['Age']= strk[['Age']].apply(change_age, axis=1)
strk.head()<jupyter_output><empty_output><jupyter_text>Identifying dependent and independent variables * dv= Dependent variable => Stroke* iv= Independent variable => Rest of the data<jupyter_code>dv = strk['Stroke']
iv = strk.drop('Stroke', axis=1)
dv.head()
iv.head()
plt.subplots(figsize=(13,8))
sns.heatmap(strk.corr(), annot=True, cmap='RdYlGn')<jupyter_output><empty_output><jupyter_text>--------------------------------------------------------------------------------------------------------    Dividing the set into two different sets: test & train<jupyter_code>from sklearn.model_selection import train_test_split
iv_train, iv_test, dv_train, dv_test = train_test_split(iv, dv, test_size=0.3, random_state=1)
iv_train.shape
iv_test.shape
iv.shape
dv.shape<jupyter_output><empty_output><jupyter_text>--------------------------------------------------------------------------------------------------------   1.Creating model by using a specific algorithm ( RF )  Dividing data into attributes and lables<jupyter_code>X = strk.iloc[:, 0:7].values
y = strk.iloc[:, 2].values<jupyter_output><empty_output><jupyter_text>Splitting data into two sets; for trainning and testing<jupyter_code>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=0)
X_train.shape
X_test.shape
y_train.shape
y_test.shape<jupyter_output><empty_output><jupyter_text>Feature scaling<jupyter_code>from sklearn.preprocessing import StandardScaler

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)<jupyter_output><empty_output><jupyter_text>Trainning algorithm<jupyter_code>from sklearn.ensemble import RandomForestRegressor

regressor = RandomForestRegressor(n_estimators=20000, random_state=0)
model=regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)<jupyter_output><empty_output><jupyter_text>Checking the accuracy (RF)<jupyter_code>from sklearn.metrics import accuracy_score
acuRF=accuracy_score(y_test, y_pred)
print(acuRF)
print ("\nAccuracy: " + "{:.2%}".format(acuRF))
from sklearn.metrics import confusion_matrix
confusion_matrix = confusion_matrix(y_test,y_pred)
class_names = [0,1]
fig,ax = plt.subplots()
tick_marks = np.arange(len(class_names))
plt.xticks(tick_marks,class_names)
plt.yticks(tick_marks,class_names)
sns.heatmap(pd.DataFrame(confusion_matrix), annot = True, cmap = 'Greens', fmt = 'g')
ax.xaxis.set_label_position('top')
plt.tight_layout()
plt.title('Confusion matrix for Random Forest')
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
plt.show()
from sklearn.metrics import roc_auc_score,roc_curve

y_probabilities = model.predict(X_test)
false_positive_rate_knn, true_positive_rate_knn, threshold_knn = roc_curve(y_test,y_probabilities)
plt.figure(figsize=(10,6))
plt.title('ROC for Random Forest regression')
plt.plot(false_positive_rate_knn, true_positive_rate_knn, linewidth=5, color='green')
plt.plot([0,1],ls='--',linewidth=5)
plt.plot([0,0],[1,0],c='.5')
plt.plot([1,1],c='.5')
plt.text(0.2,0.6,'AUC: {:.2f}'.format(roc_auc_score(y_test,y_probabilities)),size= 16)
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1607231302625062, 'fraction_numerical': 0.01659237246161466, 'mean_word_length': 6.017376194613379, 'pattern_counts': {'":': 0, '<': 100, '<?xml version=': 0, '>': 104, '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': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '16767', 'license': 'MIT', 'n_tokens_mistral': 3056, 'n_tokens_neox': 2905, 'n_words': 534, 'path': 'Stroke.ipynb', 'repo_name': 'GobShafin/Data-Science-healthcare-prediction-model-Python', 'size': '8076'}