|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
import pandas as pd |
|
import seaborn as sns |
|
from sklearn.metrics import mean_absolute_error |
|
|
|
|
|
sns.set_theme() |
|
|
|
def read_results(filename): |
|
with open(filename, "r") as f: |
|
lines = f.readlines() |
|
|
|
preds_values = [] |
|
actual_values = [] |
|
mae_values = [] |
|
|
|
for line in lines: |
|
if line.startswith("Preds:"): |
|
preds = line.replace("[", "") |
|
preds = preds.replace("]", "") |
|
preds = preds.strip("Preds:") |
|
preds = preds.strip() |
|
preds = preds.split(",") |
|
preds = [p.strip() for p in preds] |
|
preds = np.asarray([float(p) for p in preds]) |
|
preds_values.append(preds) |
|
|
|
if line.startswith("Actual:"): |
|
actual = line.replace("[", "") |
|
actual = actual.replace("]", "") |
|
actual = actual.strip("Actual values:") |
|
actual = actual.strip() |
|
actual = actual.split(",") |
|
actual = [a.strip() for a in actual] |
|
actual = np.asarray([float(a) for a in actual]) |
|
actual_values.append(actual) |
|
|
|
if line.startswith("MAE"): |
|
mae = float(line.split()[-1]) |
|
mae_values.append(mae) |
|
return preds_values, actual_values, mae_values |
|
|
|
|
|
def plot_distribution(preds_values, actual_values, mae_values, model_name, threshold, oversampled): |
|
for i in range(2): |
|
if i == 0: |
|
input_type = "BoW" |
|
else: |
|
input_type = "TF-IDF" |
|
preds = preds_values[i] |
|
actual = actual_values[i] |
|
mae = mae_values[i] |
|
|
|
res = pd.DataFrame() |
|
res["Prediction"] = preds |
|
res["Actual"] = actual |
|
|
|
sns.displot(res, kind="kde") |
|
plt.xlabel("Home standard score") |
|
plt.title(f"Model: {model_name}, Input type: {input_type}, MAE: {mae}, Threshold:{threshold}", |
|
fontsize = 10) |
|
plt.ylim(-0.03, 2.5) |
|
plt.tight_layout() |
|
plt.savefig(f"figs/{model_name}_{input_type}_{threshold[0]}_{threshold[1]}_{oversampled}.png") |
|
plt.close() |
|
|
|
|
|
def print_category_errors(actual, preds): |
|
for i in range(2): |
|
if i == 0: |
|
input_type = "BoW" |
|
else: |
|
input_type = "TF-IDF" |
|
|
|
preds = preds_values[i] |
|
actual = actual_values[i] |
|
mae = mae_values[i] |
|
|
|
print(input_type) |
|
actual1 = list(actual[np.where(actual < 0.98)]) |
|
preds1 = list(preds[np.where(actual < 0.98)]) |
|
print(f"Category 1 MAE: {mean_absolute_error(actual1, preds1):.4f}") |
|
print(f"Category 1 correlation: {np.corrcoef(actual1, preds1)[0][1]:.4f}") |
|
print() |
|
|
|
actual2 = list(actual[np.where((actual >= 0.98) & (actual < 1.5))]) |
|
preds2 = list(preds[np.where((actual >= 0.98) & (actual < 1.5))]) |
|
print(f"Category 2 MAE: {mean_absolute_error(actual2, preds2):.4f}") |
|
print(f"Category 2 correlation: {np.corrcoef(actual2, preds2)[0][1]:.4f}") |
|
print() |
|
|
|
actual3 = list(actual[np.where((actual >= 1.5) & (actual < 2))]) |
|
preds3 = list(preds[np.where((actual >= 1.5) & (actual < 2))]) |
|
print(f"Category 3 MAE: {mean_absolute_error(actual3, preds3):.4f}") |
|
print(f"Category 3 correlation: {np.corrcoef(actual3, preds3)[0][1]:.4f}") |
|
print() |
|
|
|
actual4 = list(actual[np.where(actual >= 2)]) |
|
preds4 = list(preds[np.where(actual >= 2)]) |
|
print(f"Category 4 MAE: {mean_absolute_error(actual4, preds4):.4f}") |
|
print(f"Category 4 correlation: {np.corrcoef(actual4, preds4)[0][1]:.4f}") |
|
print() |
|
print(f"Overall corr: {np.corrcoef(actual, preds)[0][1]:.4f}") |
|
|
|
|
|
if __name__ == "__main__": |
|
filename = "linear_models/lasso_0.01_0.99.txt" |
|
print(filename) |
|
preds_values, actual_values, mae_values = read_results(filename) |
|
|
|
print_category_errors(actual_values, preds_values) |
|
print("============================") |
|
|
|
filename = "linear_models/lin_reg_0.01_0.99.txt" |
|
print(filename) |
|
preds_values, actual_values, mae_values = read_results(filename) |
|
|
|
print_category_errors(actual_values, preds_values) |
|
print("============================") |
|
|
|
filename = "linear_models/sgd_reg_0.01_0.99.txt" |
|
print(filename) |
|
preds_values, actual_values, mae_values = read_results(filename) |
|
|
|
print_category_errors(actual_values, preds_values) |
|
print("============================") |
|
|
|
filename = "oversampled_False_catboost_reg_0.01_0.99.txt" |
|
print(filename) |
|
preds_values, actual_values, mae_values = read_results(filename) |
|
|
|
print_category_errors(actual_values, preds_values) |
|
print("============================") |
|
|
|
filename = "linear_models/lasso_0.2_0.8.txt" |
|
print(filename) |
|
preds_values, actual_values, mae_values = read_results(filename) |
|
|
|
print_category_errors(actual_values, preds_values) |
|
print("============================") |
|
|
|
filename = "linear_models/oversampled_lin_reg_0.01_0.99.txt" |
|
print(filename) |
|
preds_values, actual_values, mae_values = read_results(filename) |
|
|
|
print_category_errors(actual_values, preds_values) |
|
print("============================") |
|
|
|
filename = "linear_models/oversampled_lasso_0.01_0.99.txt" |
|
print(filename) |
|
preds_values, actual_values, mae_values = read_results(filename) |
|
|
|
print_category_errors(actual_values, preds_values) |
|
print("============================") |
|
|
|
filename = "linear_models/oversampled_sgd_reg_0.01_0.99.txt" |
|
print(filename) |
|
preds_values, actual_values, mae_values = read_results(filename) |
|
|
|
print_category_errors(actual_values, preds_values) |
|
print("============================") |
|
|
|
filename = "oversampled_True_catboost_reg_0.01_0.99.txt" |
|
print(filename) |
|
preds_values, actual_values, mae_values = read_results(filename) |
|
|
|
print_category_errors(actual_values, preds_values) |
|
print("============================") |
|
|
|
filename = "linear_models/oversampled_lasso_0.15_0.85.txt" |
|
print(filename) |
|
preds_values, actual_values, mae_values = read_results(filename) |
|
|
|
print_category_errors(actual_values, preds_values) |
|
print("============================") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|