Show model performance directly from predict_data

This commit is contained in:
coolneng 2020-11-10 21:13:50 +01:00
parent de21b65ca0
commit bdfc1c385d
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 18 additions and 18 deletions

View File

@ -1,5 +1,5 @@
from numpy import mean from numpy import mean
from sklearn.metrics import classification_report, confusion_matrix from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.model_selection import cross_val_score from sklearn.model_selection import cross_val_score
from sklearn.naive_bayes import GaussianNB from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier from sklearn.neighbors import KNeighborsClassifier
@ -25,31 +25,31 @@ def predict_data(data, target, model):
model = choose_model(model) model = choose_model(model)
if model == "knn": if model == "knn":
data = scale(data) data = scale(data)
predictions = [] accuracy_scores = []
confusion_matrices = []
for train_index, test_index in split_k_sets(data): for train_index, test_index in split_k_sets(data):
model.fit(data.iloc[train_index], target.iloc[train_index]) model.fit(data.iloc[train_index], target.iloc[train_index])
prediction = model.predict(data.iloc[test_index]) prediction = model.predict(data.iloc[test_index])
predictions.append(prediction) accuracy_scores.append(accuracy_score(target.iloc[test_index], prediction))
return model, predictions confusion_matrices.append(confusion_matrix(target.iloc[test_index], prediction))
cv_score = cross_val_score(model, data, target, cv=10)
evaluate_performance(
confusion_matrix=mean(confusion_matrices, axis=0),
accuracy=mean(accuracy_scores),
cv_score=mean(cv_score),
)
def evaluate_performance(predictions, model, data, target): def evaluate_performance(confusion_matrix, accuracy, cv_score):
confusion_matrices = [] print("Accuracy Score: " + str(accuracy))
classification_reports = [] print("Confusion matrix: ")
score = cross_val_score(model, data, target, cv=10) print(str(confusion_matrix))
for prediction in predictions: print("Cross validation score: " + str(cv_score))
confusion_matrices.append(confusion_matrix(target, prediction))
classification_reports.append(classification_report(target, prediction))
print("Model:" + model)
print("Score: " + score)
print("Confusion matrix: " + mean(confusion_matrices))
print("Classification report: " + mean(classification_reports))
def main(): def main():
data, target = parse_data(source="../data/mamografia.csv", action="fill") data, target = parse_data(source="data/mamografia.csv", action="drop")
model, predictions = predict_data(data=data, target=target, model="knn") predict_data(data=data, target=target, model="svc")
evaluate_performance(predictions=predictions, model=model, data=data, target=target)
if __name__ == "__main__": if __name__ == "__main__":