File size: 7,266 Bytes
c52228e 780bb93 c52228e 780bb93 c52228e 780bb93 c52228e 780bb93 c52228e 780bb93 c52228e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
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)
#plot_distribution(preds_values, actual_values, mae_values, "Lasso", [0.01, 0.99], False)
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)
#plot_distribution(preds_values, actual_values, mae_values, "Linear regression", [0.01, 0.99], False)
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)
#plot_distribution(preds_values, actual_values, mae_values, "SGD Regressor", [0.01, 0.99], False)
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)
#plot_distribution(preds_values, actual_values, mae_values, "CatBoostRegressor", [0.01, 0.99], False)
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)
#plot_distribution(preds_values, actual_values, mae_values, "Lasso", [0.2, 0.8], False)
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)
#plot_distribution(preds_values, actual_values, mae_values, "Linear regression", [0.01, 0.99], True)
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)
#plot_distribution(preds_values, actual_values, mae_values, "Lasso", [0.01, 0.99], True)
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)
#plot_distribution(preds_values, actual_values, mae_values, "SGD Regressor", [0.01, 0.99], True)
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)
#plot_distribution(preds_values, actual_values, mae_values, "CatBoostRegressor", [0.01, 0.99], True)
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)
#plot_distribution(preds_values, actual_values, mae_values, "Lasso", [0.15, 0.85], True)
print_category_errors(actual_values, preds_values)
print("============================")
|