From 8445320d2cfb81dcbf4bcc09597db9f65f9bada7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C5=82gorzata=20R=C3=B3g?= Date: Tue, 17 Oct 2023 18:31:25 +0200 Subject: [PATCH] Move code to .py files, add evaluation and comments --- model_training.py | 56 +++++++++++++++++++++++++++++++++++++++++++++ price_evaluation.py | 14 ++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 model_training.py create mode 100644 price_evaluation.py diff --git a/model_training.py b/model_training.py new file mode 100644 index 0000000..fe0c788 --- /dev/null +++ b/model_training.py @@ -0,0 +1,56 @@ +# Importowanie potrzebnych bibliotek +import pickle +import csv +import pandas as pd +import numpy as np +from sklearn.linear_model import LinearRegression +from sklearn.metrics import mean_squared_error + +# Nazwa pliku z modelem +filename = "flat_model.pkl" + +#Ładowanie danych do trenowania +data = pd.read_table("mieszkania/train.tsv", delimiter='\t', header=None) +data.rename(columns={0: 'cena', 1: 'stan', 2: 'czynsz', 3: 'x3', 4: 'cenazam', 5: 'link', 6: 'pietro', 7: 'x7', 8: 'metraz', 9: 'rynek', 10: 'liczba pokoi', 11: 'budynek', 12: 'x12', 13: 'x13', 14: 'x14', 15: 'x15', 16: 'x16', 17: 'x17', 18: 'x18', 19: 'x19', 20: 'x20', 21: 'x21', 22: 'x22', 23: 'x23', 24: 'x24', 25: 'x25'}, inplace=True) + +#Ładowanie danych do ewaluacji +data_eval = pd.read_table("mieszkania/test.tsv", delimiter='\t', header=None) +data_eval_expected = pd.read_table("mieszkania/test_expected.tsv", delimiter='\t', header=None) +data_eval.rename(columns={0: 'stan', 1: 'czynsz', 2: 'x3', 3: 'cenazam', 4: 'link', 5: 'pietro', 6: 'x7', 7: 'metraz', 8: 'rynek', 9: 'liczba pokoi', 10: 'budynek', 11: 'x12', 12: 'x13', 13: 'x14', 14: 'x15', 15: 'x16', 16: 'x17', 17: 'x18', 18: 'x19', 19: 'x20', 20: 'x21', 21: 'x22', 22: 'x23', 23: 'x24', 24: 'x25'}, inplace=True) +data_eval_expected.rename(columns={0:'cena'}, inplace=True) +data_eval = pd.concat([data_eval, data_eval_expected], axis=1) + +#Czyszczenie danych +data.drop(['x3','cenazam','link','pietro','budynek','x7','x12','x13','x14','x15','x16','x17','x18','x19','x20','x21','x22','x23','x24','x25'], inplace=True, axis=1) +data['czynsz'] = data['czynsz'].str.extract('(\d+)') +data['stan'] = data['stan'].map({'do zamieszkania': 2, 'do remontu': 1, 'do wykończenia': 2}) +data['rynek'] = data['rynek'].map({'wtórny': 0, 'pierwotny': 1}) +data.dropna(inplace=True) + +data_eval.drop(['x3','cenazam','link','pietro','budynek','x7','x12','x13','x14','x15','x16','x17','x18','x19','x20','x21','x22','x23','x24','x25'], inplace=True, axis=1) +data_eval['czynsz'] = data_eval['czynsz'].str.extract('(\d+)') +data_eval['stan'] = data_eval['stan'].map({'do zamieszkania': 2, 'do remontu': 1, 'do wykończenia': 2}) +data_eval['rynek'] = data_eval['rynek'].map({'wtórny': 0, 'pierwotny': 1}) +data_eval.dropna(inplace=True) + +#Wybranie danych do treningu +cena = data['cena'] +parametry = data[['stan', 'czynsz', 'liczba pokoi', 'metraz', 'rynek']] + +#Wybranie danych do ewaluacji +cena_eval = data_eval['cena'].tolist() +parametry_eval = data_eval[['stan', 'czynsz', 'liczba pokoi', 'metraz', 'rynek']] + +#Proces trenowania +model = LinearRegression() +model.fit(X=parametry, y=cena) + +#Ewaluacja +cena_predicted_eval = model.predict(parametry_eval) +mse = mean_squared_error(cena_eval, cena_predicted_eval) + +#Zwrócenie metryki mean squared error na zbiorze do ewaluacji +print(f"Metryka mean squared error {mse}") + +#Zapisanie modelu +pickle.dump(model, open(filename, "wb")) diff --git a/price_evaluation.py b/price_evaluation.py new file mode 100644 index 0000000..6322f9d --- /dev/null +++ b/price_evaluation.py @@ -0,0 +1,14 @@ +# Importowanie potrzebnych bibliotek +import pickle +import pandas as pd + +#Nazwa pliku z modelem +filename = "flat_model.pkl" + +#Ładowanie modelu +model = pickle.load(open(filename, "rb")) + +#Definicja funkcji predykcyjnej +def predict(stan, czynsz, liczba_pokoi, metraz, rynek): + params = pd.DataFrame.from_dict({'stan': [stan], 'czynsz': [czynsz], 'liczba pokoi': [liczba_pokoi], 'metraz': [metraz], 'rynek': [rynek]}) + return model.predict(params).item()