-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplots.py
More file actions
190 lines (154 loc) · 6.71 KB
/
Copy pathplots.py
File metadata and controls
190 lines (154 loc) · 6.71 KB
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
190
import os
import pandas as pd
import matplotlib.pyplot as plt
import ast
def visualize_mse_by_target(results_dir, output_file="ridge_results.png"):
"""
Visualize the Mean Squared Error (MSE) for different models across target percentages.
Parameters:
- results_dir (str): Directory containing the result CSV files.
- output_file (str): File name to save the plot.
"""
mse_data = []
for file_name in os.listdir(results_dir):
if not file_name.endswith(".csv") or "alpha" in file_name:
continue
file_path = os.path.join(results_dir, file_name)
data = pd.read_csv(file_path, header=None)
parts = file_name.split("_")
model_name = parts[0]
target_percentage = int(parts[2])
mse = float(data.loc[data[0] == "MSE", 1].values[0].strip("[]"))
mse_data.append((model_name, target_percentage, mse))
mse_df = pd.DataFrame(mse_data, columns=["Model", "Target", "MSE"])
mse_df = mse_df.sort_values(by=["Model", "Target"])
plt.figure(figsize=(10, 6))
for i, model in enumerate(mse_df["Model"].unique()):
model_data = mse_df[mse_df["Model"] == model]
plt.plot(
model_data["Target"],
model_data["MSE"],
marker="o",
label=model,
alpha=0.8,
linewidth=2 - 0.5 * i,
)
plt.title("MSE by Target Percentage for Different Models")
plt.xlabel("Target Percentage (%)")
plt.ylabel("Mean Squared Error (MSE)")
plt.legend()
plt.grid()
plt.savefig(output_file, format="png")
print(f"Plot saved as {output_file}")
plt.show()
def visualize_ridge_results_by_target(results_dir, output_file="ridge_results.png"):
"""
Visualize the MSE of Ridge regression across different alpha values for each target percentage.
Parameters:
- results_dir (str): Directory containing Ridge regression result CSV files.
- output_file (str): File name to save the plot.
"""
results = []
for file_name in os.listdir(results_dir):
if file_name.startswith("Ridge_alpha_") and file_name.endswith(".csv"):
file_path = os.path.join(results_dir, file_name)
data = pd.read_csv(file_path, header=None, index_col=0)
alpha = float(file_name.split("_")[2])
target = int(file_name.split("_")[-2])
mse = float(data.loc["MSE"][1].strip("[]"))
results.append({"alpha": alpha, "target": target, "MSE": mse})
results_df = pd.DataFrame(results).sort_values(["target", "alpha"])
plt.figure(figsize=(10, 5))
for target in results_df["target"].unique():
subset = results_df[results_df["target"] == target]
plt.plot(subset["alpha"], subset["MSE"], marker="o", label=f"Target {target}")
plt.xscale("log")
plt.xlabel("Alpha")
plt.ylabel("Mean Squared Error")
plt.title("Ridge Regression: MSE vs. Alpha by Target")
plt.legend()
plt.grid()
plt.savefig(output_file, format="png")
print(f"Plot saved as {output_file}")
plt.show()
def visualize_mse_by_target_avg(results_dir, output_file="ridge_results.png"):
"""
Visualize the MSE for different models and multiple targets across target percentages.
Parameters:
- results_dir (str): Directory containing result CSV files.
- output_file (str): File name to save the plot.
"""
mse_data = []
for file_name in os.listdir(results_dir):
if not file_name.endswith(".csv") or "alpha" in file_name:
continue
file_path = os.path.join(results_dir, file_name)
data = pd.read_csv(file_path, header=None)
parts = file_name.split("_")
model_name = parts[0]
target_percentage = int(parts[2])
mse_raw = data.loc[data[0] == "MSE", 1].values[0]
mse_list = ast.literal_eval(mse_raw)
for target_idx, mse in enumerate(mse_list):
mse_data.append((model_name, target_percentage, f"Target {target_idx + 1}", mse))
mse_df = pd.DataFrame(mse_data, columns=["Model", "Target Percentage", "Target", "MSE"])
mse_df = mse_df.sort_values(by=["Model", "Target Percentage", "Target"])
plt.figure(figsize=(10, 6))
for model in mse_df["Model"].unique():
model_data = mse_df[mse_df["Model"] == model]
for target in model_data["Target"].unique():
target_data = model_data[model_data["Target"] == target]
plt.plot(
target_data["Target Percentage"],
target_data["MSE"],
marker="o",
label=f"{model} - {target}",
alpha=0.8,
)
plt.title("MSE by Target Percentage for Different Models and Targets")
plt.xlabel("Target Percentage (%)")
plt.ylabel("Mean Squared Error (MSE)")
plt.legend()
plt.grid()
plt.savefig(output_file, format="png")
print(f"Plot saved as {output_file}")
plt.show()
def visualize_ridge_results_by_target_avg(results_dir, output_file="ridge_results.png"):
"""
Visualize the MSE of Ridge regression for multiple targets across target percentages.
Parameters:
- results_dir (str): Directory containing Ridge result CSV files.
- output_file (str): File name to save the plot.
"""
mse_data = []
for file_name in os.listdir(results_dir):
if not file_name.endswith(".csv") or not file_name.startswith("Ridge") or "alpha" in file_name:
continue
file_path = os.path.join(results_dir, file_name)
data = pd.read_csv(file_path, header=None)
parts = file_name.split("_")
target_percentage = int(parts[2])
mse_raw = data.loc[data[0] == "MSE", 1].values[0]
mse_list = ast.literal_eval(mse_raw)
for target_idx, mse in enumerate(mse_list):
mse_data.append((target_percentage, f"Target {target_idx + 1}", mse))
mse_df = pd.DataFrame(mse_data, columns=["Target Percentage", "Target", "MSE"])
mse_df = mse_df.sort_values(by=["Target Percentage", "Target"])
plt.figure(figsize=(10, 6))
for target in mse_df["Target"].unique():
target_data = mse_df[mse_df["Target"] == target]
plt.plot(
target_data["Target Percentage"],
target_data["MSE"],
marker="o",
label=target,
alpha=0.8,
)
plt.title("MSE by Target Percentage for Ridge Model")
plt.xlabel("Target Percentage (%)")
plt.ylabel("Mean Squared Error (MSE)")
plt.legend(title="Targets")
plt.grid()
plt.savefig(output_file, format="png")
print(f"Plot saved as {output_file}")
plt.show()