From abfc877c7d1a96d53d89d3d0c407883566cee7a7 Mon Sep 17 00:00:00 2001 From: coolneng Date: Fri, 1 Jan 2021 21:06:09 +0100 Subject: [PATCH] Add processing module --- src/processing.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/processing.py diff --git a/src/processing.py b/src/processing.py new file mode 100644 index 0000000..4ec8cd4 --- /dev/null +++ b/src/processing.py @@ -0,0 +1,51 @@ +from numpy import mean +from pandas import DataFrame +from sklearn.metrics import accuracy_score +from sklearn.model_selection import cross_val_score +from sklearn.ensemble import GradientBoostingClassifier + +from preprocessing import parse_data, split_k_sets + + +def predict_data(train_data, train_target, test_data, test_ids): + model = GradientBoostingClassifier(random_state=42) + accuracy_scores = [] + for train_index, test_index in split_k_sets(train_data): + model.fit(train_data.iloc[train_index], train_target.iloc[train_index]) + prediction = model.predict(train_data.iloc[test_index]) + accuracy_scores.append( + accuracy_score(train_target.iloc[test_index], prediction) + ) + cv_score = cross_val_score(model, train_data, train_target) + evaluate_performance( + accuracy=mean(accuracy_scores), + cv_score=mean(cv_score), + ) + predictions = model.predict(test_data) + export_results(ids=test_ids, prediction=predictions) + + +def evaluate_performance(accuracy, cv_score): + print("Accuracy Score: " + str(accuracy)) + print("Cross validation score: " + str(cv_score)) + + +def export_results(ids, prediction): + result_df = DataFrame({"id": ids, "Precio_cat": prediction}) + result_df.to_csv(path_or_buf="data/results.csv", index=False) + + +def main(): + train_data, train_target, test_data, test_ids = parse_data( + train="data/train.csv", test="data/test.csv" + ) + predict_data( + train_data=train_data, + train_target=train_target, + test_data=test_data, + test_ids=test_ids, + ) + + +if __name__ == "__main__": + main()