Skip to content

Commit 96013b8

Browse files
authored
Merge pull request #30 from ClarkCGA/yayao
Update (model_evaluation.py): Enhancements to Plotting Style and Regression Line
2 parents 565d439 + b9cb4c3 commit 96013b8

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

model_evaluation.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,26 @@ def create_plot(self, grid_area, clipped_gdf, title, out_fn,xmax=None, ymax=None
512512
plt.title(title, color='firebrick', fontweight='bold', fontsize=20, pad=20)
513513

514514
# Plot the trend line
515-
plt.plot(X, trend_line, color='mediumseagreen', linestyle='-', label='Best Fit Line')
515+
plt.plot(X, trend_line, color='mediumseagreen', linestyle='-', label='OLS Line')
516516

517517
# Plot a 1-to-1 line
518518
plt.plot([0, max(clipped_gdf['ActualDef'])], [0, max(clipped_gdf['ActualDef'])], color='crimson', linestyle='--',
519519
label='1:1 Line')
520520

521+
## Theil-Sen Regressor
522+
# Fit Theil-Sen Regressor
523+
# Compute Theil-Sen estimator
524+
ts_slope, ts_intercept, _, _ = stats.theilslopes(Y, X)
525+
526+
# Generate predictions
527+
y_pred = ts_slope * X + ts_intercept
528+
529+
# Equation of the line
530+
ts_equation = f'Y = {ts_slope:.4f} * X + {ts_intercept:.2f}'
531+
532+
# Plot Theil-Sen Line
533+
plt.plot(X, y_pred, color='orange', linestyle='-', label='Theil-Sen Line')
534+
521535
# Add a legend in the bottom right position
522536
plt.legend(loc='lower right')
523537

@@ -543,10 +557,12 @@ def create_plot(self, grid_area, clipped_gdf, title, out_fn,xmax=None, ymax=None
543557
text_y_gap = ymax * 0.05
544558

545559
# Adjust plt texts with the new calculated positions
546-
plt.text(text_x_pos, text_y_start_pos, equation, fontsize=11, color='black')
547-
plt.text(text_x_pos, text_y_start_pos - text_y_gap, f'Samples = {len(X)}', fontsize=11, color='black')
548-
plt.text(text_x_pos, text_y_start_pos - 2 * text_y_gap, f'R^2 = {r_squared:.4f}', fontsize=11, color='black')
549-
plt.text(text_x_pos, text_y_start_pos - 3 * text_y_gap, f'MedAE = {MedAE:.2f} ({MedAE_percent:.2f}%)',
560+
plt.text(text_x_pos, text_y_start_pos, f'Theil-Sen : {ts_equation}', fontsize=11,
561+
color='black')
562+
plt.text(text_x_pos, text_y_start_pos - text_y_gap, f'OLS : {equation}', fontsize=11, color='black')
563+
plt.text(text_x_pos, text_y_start_pos - 2 * text_y_gap, f'Samples = {len(X)}', fontsize=11, color='black')
564+
plt.text(text_x_pos, text_y_start_pos - 3 * text_y_gap, f'R^2 = {r_squared:.4f}', fontsize=11, color='black')
565+
plt.text(text_x_pos, text_y_start_pos - 4 * text_y_gap, f'MedAE = {MedAE:.2f} ({MedAE_percent:.2f}%)',
550566
fontsize=11, color='black')
551567

552568
# x, yticks

0 commit comments

Comments
 (0)