forked from seung365/Stroke-self-diagnosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predictionByNumerical.py
93 lines (73 loc) · 2.71 KB
/
predictionByNumerical.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
df = pd.read_csv("strokeData.csv")
# id column 없애기 + 기혼여부 수치로 바꾸기.
df = df.drop("number",axis=1)
df = df.drop("id",axis=1)
df['ever_married'] = df['ever_married'].map({'Yes':1, 'No':0})
# gender의 other 없애기
df = df[df['gender'] != 'Other']
# 범주형 데이터 인코딩
categorical_cols = ['gender', 'work_type', 'residence_type', 'smoking_status']
for col in categorical_cols:
le = LabelEncoder()
df[col] = le.fit_transform(df[col])
x = df.drop("stroke",axis=1)
y = df.stroke
from imblearn.over_sampling import SMOTE
smote = SMOTE(sampling_strategy="minority")
x_smote, y_smote = smote.fit_resample(x, y)
y_smote.value_counts()
x_train, x_test, y_train, y_test = train_test_split(x_smote, y_smote, test_size=0.2, random_state=42, stratify=y_smote)
scaler = StandardScaler()
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)
# 모델 학습 및 평가
from lazypredict.Supervised import LazyClassifier
clf = LazyClassifier(verbose=0, ignore_warnings=True, custom_metric=None)
models, predictions = clf.fit(x_train, x_test, y_train, y_test)
print(models)
from sklearn.ensemble import RandomForestClassifier
'''
rf = RandomForestClassifier()
rf.fit(x_train, y_train)
# 성능 평가
from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix, ConfusionMatrixDisplay
y_pred = rf.predict(x_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
# 혼동 행렬 시각화
cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm)
disp.plot()
plt.show()
'''
from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay
# RandomForest 모델을 학습시킵니다.
rf = RandomForestClassifier()
rf.fit(x_train, y_train)
# 예측 확률을 계산합니다.
y_pred_proba = rf.predict_proba(x_test)[:, 1] # 뇌졸중 클래스('1')에 대한 확률 추출
print("Predicted probabilities for having a stroke:", y_pred_proba)
# 임계값 설정을 기반으로 예측 결과를 계산합니다 (예: 확률 > 0.5면 뇌졸중으로 예측)
threshold = 0.5
y_pred = (y_pred_proba >= threshold).astype(int)
# 정확도 계산
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
'''
# 혼동 행렬 시각화
cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm)
disp.plot()
plt.show()
'''
# 모델 저장
from joblib import dump, load
dump(rf, 'stroke_model.pkl')