<jupyter_start><jupyter_text>Stats Summary<jupyter_code>stroke_data.describe()
import seaborn as sns
sns.countplot(stroke_data["age"], label="Count")
plt.show()
# the data is pretty balanced except mandarin 
from pandas.plotting import scatter_matrix 

scatter_matrix(stroke_data.drop('hypertension', axis=1), figsize=(10,5))
plt.show()
feature_names = ["age", 'hypertension', 'heart_disease', 'avg_glucose_level', 'bmi']
X = stroke_data[feature_names]
y = stroke_data['stroke']
X<jupyter_output><empty_output><jupyter_text>Create training and test sets<jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)<jupyter_output><empty_output><jupyter_text>Apply scaling<jupyter_code>from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>Build Models<jupyter_code>## Logistic Regression
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
logreg.fit(X_train_scaled, y_train)

print('Accuracy of logistic regression on training', logreg.score(X_train_scaled, y_train))
print('Accuracy of logistic regression on testing', logreg.score(X_test_scaled, y_test))
## Decision Tree
from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier()
dt.fit(X_train_scaled, y_train)

print('Accuracy of Decision tree on training', dt.score(X_train_scaled, y_train))
print('Accuracy of Decision tree on testing', dt.score(X_test_scaled, y_test))
# Setting max decision tree depth to help avoid overfitting
dt2 = DecisionTreeClassifier(max_depth=3)
dt2.fit(X_train_scaled, y_train)
print('Accuracy of Decision tree on training', dt2.score(X_train_scaled, y_train))
print('Accuracy of Decision tree on testing', dt2.score(X_test_scaled, y_test))
# K-nearest neighbor 
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(X_train_scaled, y_train)
print('Accuracy of Knn on training', knn.score(X_train_scaled, y_train))
print('Accuracy of Knn on testing', knn.score(X_test_scaled, y_test))
# Linear Discriminant Analysis 
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
lda = LinearDiscriminantAnalysis()
lda.fit(X_train_scaled,y_train)
print('Accuracy of Knn on training', lda.score(X_train_scaled, y_train))
print('Accuracy of Knn on testing', lda.score(X_test_scaled, y_test))
# Gaussian Naive Bayes 
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb.fit(X_train_scaled, y_train)
print('Accuracy of GNB on training', gnb.score(X_train_scaled, y_train))
print('Accuracy of GNB on testing', gnb.score(X_test_scaled, y_test))
# Support Vector Machine
from sklearn.svm import SVC
svm = SVC()
svm.fit(X_train_scaled, y_train)
print('Accuracy of SVM on training', svm.score(X_train_scaled, y_train))
print('Accuracy of SVM on testing', svm.score(X_test_scaled, y_test))
import pickle
pickle.dump(gnb, open('stroke_model.pkl','wb'))
#pickle.dump(dt, open('dt.pkl','wb'))
# Loading model to compare the results
model = pickle.load( open('model.pkl','rb'))
print(model.predict([[192,8.4,7.3,0.55]]))
new_data = [[80,1,1,200,45]]
df = pd.DataFrame(new_data,columns= ["age", 'hypertension', 'heart_disease', 'avg_glucose_level', 'bmi'])
original_df = X.copy()
reference = original_df.append(df).reset_index()

new_data_frame = reference[['age', 'hypertension', 'heart_disease', 'avg_glucose_level', 'bmi']]
scaler = MinMaxScaler()
scaled_df = scaler.fit_transform(new_data_frame)
pickle.dump(gnb, open('model.pkl','wb'))
model = pickle.load( open('model.pkl','rb'))
model.predict([list(scaled_df[len(scaled_df)-1])])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12373871481678173, 'fraction_numerical': 0.007966011683483803, 'mean_word_length': 8.809895833333334, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '166353', 'license': 'MIT', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1345, 'n_words': 284, 'path': '.ipynb_checkpoints/Stroke_ML-checkpoint.ipynb', 'repo_name': 'Beer-Map/Stroke-Machine-Learning', 'size': '3766'}