|
25 | 25 | from sklearn.metrics import r2_score, mean_squared_error |
26 | 26 | from scipy.stats import spearmanr |
27 | 27 | import numpy as np |
28 | | -import matplotlib.pyplot as plt |
29 | 28 | from apistemic.benchmarks.datasets.companies import fetch_companies_df |
30 | 29 | from apistemic.benchmarks.datasets.competitors import fetch_competitor_votes |
31 | 30 | from apistemic.benchmarks.models import CompetitvenessRatingAnswer |
|
35 | 34 | CompanyTupleTransformer, |
36 | 35 | EmbeddingDiffTransformer, |
37 | 36 | ) |
| 37 | +from apistemic.benchmarks.plots import ( |
| 38 | + create_box_plot, |
| 39 | + create_r2_plot, |
| 40 | + create_spearman_plot, |
| 41 | +) |
38 | 42 |
|
39 | 43 |
|
40 | 44 | class ModelType(Enum): |
@@ -77,141 +81,6 @@ def evaluate_similarity_predictions(y_true, y_pred): |
77 | 81 | ) |
78 | 82 |
|
79 | 83 |
|
80 | | -def create_box_plot(all_results: dict[str, list[EvaluationMetrics]]) -> None: |
81 | | - """Create box plot of R² scores by model.""" |
82 | | - models = list(all_results.keys()) |
83 | | - r2_scores = [] |
84 | | - |
85 | | - for model in models: |
86 | | - model_r2_scores = [metrics.r2 for metrics in all_results[model]] |
87 | | - r2_scores.append(model_r2_scores) |
88 | | - |
89 | | - plt.style.use("grayscale") |
90 | | - plt.figure(figsize=(10, 6)) |
91 | | - plt.boxplot(r2_scores, tick_labels=models, patch_artist=False) |
92 | | - |
93 | | - plt.title("How Well Embedding Models Understand Companies") |
94 | | - plt.xlabel("Embedding Model (applied to company name)") |
95 | | - plt.ylabel("R² Score (based on embedded company name only)") |
96 | | - plt.grid(True, alpha=0.3) |
97 | | - plt.xticks(rotation=45) |
98 | | - plt.tight_layout() |
99 | | - |
100 | | - # Save the plot |
101 | | - plt.savefig(".data/plots/r2-scores-boxplot.png", dpi=300, bbox_inches="tight") |
102 | | - |
103 | | - # Print summary statistics |
104 | | - print("\n" + "=" * 60) |
105 | | - print("SUMMARY STATISTICS") |
106 | | - print("=" * 60) |
107 | | - for model, model_r2_scores in zip(models, r2_scores): |
108 | | - print(f"\n{model}:") |
109 | | - print(f" Mean R²: {np.mean(model_r2_scores):.4f}") |
110 | | - print(f" Std R²: {np.std(model_r2_scores):.4f}") |
111 | | - print(f" Min R²: {np.min(model_r2_scores):.4f}") |
112 | | - print(f" Max R²: {np.max(model_r2_scores):.4f}") |
113 | | - |
114 | | - |
115 | | -def create_r2_plot(results: dict[str, EvaluationMetrics]) -> None: |
116 | | - """Create bar plot of R² scores by LLM model.""" |
117 | | - # Sort models by R² score in descending order |
118 | | - models = sorted(results.keys(), key=lambda x: results[x].r2, reverse=True) |
119 | | - r2_scores = [results[model].r2 for model in models] |
120 | | - |
121 | | - # Clean up model names for display |
122 | | - display_names = [] |
123 | | - for model in models: |
124 | | - if "__" in model: |
125 | | - provider, model_name = model.split("__", 1) |
126 | | - display_names.append(f"{provider}\n{model_name}") |
127 | | - else: |
128 | | - display_names.append(model) |
129 | | - |
130 | | - plt.style.use("grayscale") |
131 | | - plt.figure(figsize=(12, 6)) |
132 | | - bars = plt.bar(range(len(models)), r2_scores) |
133 | | - |
134 | | - plt.title("LLM Performance on Competitiveness Rating Task") |
135 | | - plt.xlabel("LLM Model") |
136 | | - plt.ylabel("R² Score") |
137 | | - plt.xticks(range(len(models)), display_names, rotation=45, ha="right") |
138 | | - plt.grid(True, alpha=0.3, axis="y") |
139 | | - plt.ylim(-1.0, 1.0) |
140 | | - |
141 | | - # Add value labels on bars |
142 | | - for bar, score in zip(bars, r2_scores): |
143 | | - y_pos = bar.get_height() + 0.01 if score >= 0 else bar.get_height() - 0.01 |
144 | | - va = "bottom" if score >= 0 else "top" |
145 | | - plt.text( |
146 | | - bar.get_x() + bar.get_width() / 2, |
147 | | - y_pos, |
148 | | - f"{score:.3f}", |
149 | | - ha="center", |
150 | | - va=va, |
151 | | - ) |
152 | | - |
153 | | - plt.tight_layout() |
154 | | - |
155 | | - # Save the plot |
156 | | - plt.savefig(".data/plots/r2-scores-barplot.png", dpi=300, bbox_inches="tight") |
157 | | - |
158 | | - |
159 | | -def create_spearman_plot(results: dict[str, EvaluationMetrics]) -> None: |
160 | | - """Create bar plot of Spearman correlations by LLM model.""" |
161 | | - # Sort models by Spearman correlation in descending order |
162 | | - models = sorted( |
163 | | - results.keys(), key=lambda x: results[x].spearman_corr, reverse=True |
164 | | - ) |
165 | | - spearman_corrs = [results[model].spearman_corr for model in models] |
166 | | - |
167 | | - # Clean up model names for display |
168 | | - display_names = [] |
169 | | - for model in models: |
170 | | - if "__" in model: |
171 | | - provider, model_name = model.split("__", 1) |
172 | | - display_names.append(f"{provider}\n{model_name}") |
173 | | - else: |
174 | | - display_names.append(model) |
175 | | - |
176 | | - plt.style.use("grayscale") |
177 | | - plt.figure(figsize=(12, 6)) |
178 | | - bars = plt.bar(range(len(models)), spearman_corrs) |
179 | | - |
180 | | - plt.title("LLM Performance on Competitiveness Rating Task") |
181 | | - plt.xlabel("LLM Model") |
182 | | - plt.ylabel("Spearman Correlation") |
183 | | - plt.xticks(range(len(models)), display_names, rotation=45, ha="right") |
184 | | - plt.grid(True, alpha=0.3, axis="y") |
185 | | - plt.ylim(0, 1.0) |
186 | | - |
187 | | - # Add value labels on bars |
188 | | - for bar, corr in zip(bars, spearman_corrs): |
189 | | - plt.text( |
190 | | - bar.get_x() + bar.get_width() / 2, |
191 | | - bar.get_height() + 0.01, |
192 | | - f"{corr:.3f}", |
193 | | - ha="center", |
194 | | - va="bottom", |
195 | | - ) |
196 | | - |
197 | | - plt.tight_layout() |
198 | | - |
199 | | - # Save the plot |
200 | | - plt.savefig( |
201 | | - ".data/plots/spearman-correlations-barplot.png", dpi=300, bbox_inches="tight" |
202 | | - ) |
203 | | - |
204 | | - # Print summary statistics |
205 | | - print("\n" + "=" * 60) |
206 | | - print("LLM SPEARMAN CORRELATION RESULTS") |
207 | | - print("=" * 60) |
208 | | - for model, metrics in results.items(): |
209 | | - print(f"\n{model}:") |
210 | | - print(f" Spearman ρ: {metrics.spearman_corr:.4f} (p={metrics.spearman_p:.4f})") |
211 | | - print(f" R²: {metrics.r2:.4f}") |
212 | | - print(f" RMSE: {metrics.rmse:.4f}") |
213 | | - |
214 | | - |
215 | 84 | def run_embedding_classification(run_count: int = 5): |
216 | 85 | df = fetch_competitor_votes() |
217 | 86 | df_agg = df.groupby(["from_organization_id", "to_organization_id"]).mean( |
@@ -378,7 +247,7 @@ def run_scoring(): |
378 | 247 | store = FilesystemStore(".cache") |
379 | 248 |
|
380 | 249 | results = {} |
381 | | - for llm_identifier, llm in sorted(llms.items(), key=lambda x: random.random()): |
| 250 | + for llm_identifier, llm in sorted(llms.items(), key=lambda _: random.random()): |
382 | 251 | print("#" * 50) |
383 | 252 | print(f"Running {llm}") |
384 | 253 | print("#" * 50) |
|
0 commit comments