|
3 | 3 | import numpy as np |
4 | 4 | from scipy.optimize import curve_fit |
5 | 5 |
|
| 6 | + |
6 | 7 | def model(x, a, b): |
7 | 8 | return a * (x ** 2) + b * (x ** 2) * np.log(x) |
8 | 9 |
|
9 | 10 |
|
10 | 11 | if __name__ == "__main__": |
11 | 12 | df = pd.read_csv("results.csv") |
12 | 13 |
|
13 | | - x = df["n"] |
14 | | - y = df["time"] |
15 | | - plt.scatter(x, y) |
| 14 | + grouped = df.groupby("n")["time"] |
| 15 | + |
| 16 | + # Média (e desvio‑padrão) do tempo para cada tamanho |
| 17 | + x_mean = grouped.mean().index.values.astype(float) |
| 18 | + y_mean = grouped.mean().values |
| 19 | + y_std = grouped.std().values |
| 20 | + |
| 21 | + params, covariance = curve_fit(model, x_mean, y_mean) |
| 22 | + a_fit, b_fit = params |
| 23 | + |
| 24 | + y_fit = model(x_mean, a_fit, b_fit) |
16 | 25 |
|
17 | | - params, covariance = curve_fit(model, x, y) |
| 26 | + ss_res = np.sum((y_mean - y_fit) ** 2) |
| 27 | + ss_tot = np.sum((y_mean - np.mean(y_mean)) ** 2) |
| 28 | + r_squared = 1 - ss_res / ss_tot |
18 | 29 |
|
19 | | - a_fit = params[0] |
20 | | - b_fit = params[1] |
21 | | - y_fit = model(x.unique(), a_fit, b_fit) |
| 30 | + rmse = np.sqrt(np.mean((y_mean - y_fit) ** 2)) |
22 | 31 |
|
23 | | - plt.plot(x.unique(), y_fit, label="Regressão não linear de quadrados mínimos", color="red") |
| 32 | + plt.scatter(df["n"], df["time"], label="Observações individuais") |
| 33 | + |
| 34 | + plt.errorbar(x_mean, y_mean, yerr=y_std, fmt="o", color="black", |
| 35 | + capsize=4, label="Média ± desvio‑padrão") |
| 36 | + |
| 37 | + x_fit = np.linspace(x_mean.min(), x_mean.max(), 300) |
| 38 | + y_fit = model(x_fit, a_fit, b_fit) |
| 39 | + plt.plot(x_fit, y_fit, color="red", label=f"Ajuste (R²={r_squared:.4f})") |
24 | 40 |
|
25 | 41 | plt.xlabel("Tamanho da matriz") |
26 | 42 | plt.ylabel("Tempo (ms)") |
| 43 | + plt.legend() |
| 44 | + |
27 | 45 | plt.savefig("graphs/n_time.eps") |
0 commit comments