File size: 4,222 Bytes
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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

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()


if __name__ == "__main__":
    preds_values, actual_values, mae_values = read_results("linear_models/lasso_0.01_0.99.txt")
    plot_distribution(preds_values, actual_values, mae_values, "Lasso", [0.01, 0.99], False)

    preds_values, actual_values, mae_values = read_results("linear_models/lin_reg_0.01_0.99.txt")
    plot_distribution(preds_values, actual_values, mae_values, "Linear regression", [0.01, 0.99], False)

    preds_values, actual_values, mae_values = read_results("linear_models/sgd_reg_0.01_0.99.txt")
    plot_distribution(preds_values, actual_values, mae_values, "SGD Regressor", [0.01, 0.99], False)

    preds_values, actual_values, mae_values = read_results("oversampled_False_catboost_reg_0.01_0.99.txt")
    plot_distribution(preds_values, actual_values, mae_values, "CatBoostRegressor", [0.01, 0.99], False)

    preds_values, actual_values, mae_values = read_results("linear_models/lasso_0.2_0.8.txt")
    plot_distribution(preds_values, actual_values, mae_values, "Lasso", [0.2, 0.8], False)

    preds_values, actual_values, mae_values = read_results("linear_models/oversampled_lin_reg_0.01_0.99.txt")
    plot_distribution(preds_values, actual_values, mae_values, "Linear regression", [0.01, 0.99], True)

    preds_values, actual_values, mae_values = read_results("linear_models/oversampled_lasso_0.01_0.99.txt")
    plot_distribution(preds_values, actual_values, mae_values, "Lasso", [0.01, 0.99], True)

    preds_values, actual_values, mae_values = read_results("linear_models/oversampled_sgd_reg_0.01_0.99.txt")
    plot_distribution(preds_values, actual_values, mae_values, "SGD Regressor", [0.01, 0.99], True)

    preds_values, actual_values, mae_values = read_results("oversampled_True_catboost_reg_0.01_0.99.txt")
    plot_distribution(preds_values, actual_values, mae_values, "CatBoostRegressor", [0.01, 0.99], True)

    preds_values, actual_values, mae_values = read_results("linear_models/oversampled_lasso_0.15_0.85.txt")
    plot_distribution(preds_values, actual_values, mae_values, "Lasso", [0.15, 0.85], False)