117 lines
3.5 KiB
Python
117 lines
3.5 KiB
Python
import pandas as pd
|
|
from keras.models import Sequential
|
|
from keras.layers import Dense
|
|
from keras.wrappers.scikit_learn import KerasClassifier
|
|
from sklearn import tree
|
|
from sklearn.preprocessing import MinMaxScaler
|
|
from sklearn.linear_model import SGDClassifier
|
|
from sklearn.pipeline import make_pipeline
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score
|
|
|
|
# Przewidywanie choroby serca na podstawie danych i wyników pacjenta.
|
|
|
|
data = pd.read_csv("./heart_problem.csv")
|
|
|
|
# Usunięcie wierszy z pustymi wartościami
|
|
|
|
data.dropna(inplace=True)
|
|
|
|
# Usuń wartości odstające.
|
|
indexes = data[(data['cholesterol'] == 0) | (data['resting bp s'] == 0)].index
|
|
data.drop(indexes , inplace=True)
|
|
|
|
# Skalowanie danych
|
|
scaler = MinMaxScaler()
|
|
X = data[['age','sex','resting bp s','cholesterol','fasting blood sugar', 'max heart rate']].to_numpy()
|
|
Y = data['target'].to_numpy()
|
|
X = scaler.fit_transform(X)
|
|
|
|
# Podział na zbiór trenujący/testowy
|
|
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2)
|
|
|
|
print(X_train.shape)
|
|
print(X_test.shape)
|
|
|
|
neural_accuracy = None
|
|
neural_fscore = None
|
|
neural_precision = None
|
|
neural_recall = None
|
|
|
|
sgd_accuracy = None
|
|
sgd_fscore = None
|
|
sgd_precision = None
|
|
sgd_recall = None
|
|
|
|
tree_accuracy = None
|
|
tree_fscore = None
|
|
tree_precision = None
|
|
tree_recall = None
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Klasyfikacja ma/nie ma chorobę serca na podstawie modelu sieci neuronowej.
|
|
def classificator_model():
|
|
model = Sequential()
|
|
model.add(Dense(8, input_dim=6, activation='relu'))
|
|
model.add(Dense(2, activation='softmax'))
|
|
model.compile(loss='categorical_crossentropy', optimizer='adam')
|
|
return model
|
|
|
|
# Trenowanie modelu
|
|
estimator = KerasClassifier(build_fn=classificator_model, epochs=5, batch_size=10, verbose=0)
|
|
estimator.fit(X_train, y_train)
|
|
predicts = estimator.predict(X_test)
|
|
neural_accuracy = accuracy_score(y_test, predicts)
|
|
neural_fscore = f1_score(y_test, predicts)
|
|
neural_precision = precision_score(y_test, predicts)
|
|
neural_recall = recall_score(y_test, predicts)
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Klasyfikacja ma/nie ma chorobę serca na podstawie klasyfikatora SGD.
|
|
|
|
model = SGDClassifier(max_iter=1000)
|
|
model.fit(X_train, y_train)
|
|
predicts = model.predict(X_test)
|
|
sgd_accuracy = accuracy_score(y_test, predicts)
|
|
sgd_fscore = f1_score(y_test, predicts)
|
|
sgd_precision = precision_score(y_test, predicts)
|
|
sgd_recall = recall_score(y_test, predicts)
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Klasyfikacja ma/nie ma chorobę serca na podstawie drzew decyzyjnych.
|
|
|
|
model = tree.DecisionTreeClassifier()
|
|
model.fit(X_train, y_train)
|
|
predicts = model.predict(X_test)
|
|
tree_accuracy = accuracy_score(y_test, predicts)
|
|
tree_fscore = f1_score(y_test, predicts)
|
|
tree_precision = precision_score(y_test, predicts)
|
|
tree_recall = recall_score(y_test, predicts)
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
print(f'''
|
|
Sieć neuronowa:
|
|
_____________________________
|
|
Accuracy: {neural_accuracy}
|
|
Precision: {neural_precision}
|
|
Recall: {neural_recall}
|
|
F-score: {neural_fscore}
|
|
|
|
Klasyfikator SGD:
|
|
_____________________________
|
|
Accuracy: {sgd_accuracy}
|
|
Precision: {sgd_precision}
|
|
Recall: {sgd_recall}
|
|
F-score: {sgd_fscore}
|
|
|
|
Drzewa decyzyjne:
|
|
_____________________________
|
|
Accuracy: {tree_accuracy}
|
|
Precision: {tree_precision}
|
|
Recall: {tree_recall}
|
|
F-score: {tree_fscore}
|
|
''')
|
|
|