Predict wildfire risk - Challenge 1: Using the Buildup Index
Task
Perform linear regression between the Buildup Index (BUI) and the Fire Weather Index (FWI) in the Algerian Forest Dataset.
The figure shows the dataset and the corresponding regression line.

Figure 1: BUI-FWI dataset points in blue, and corresponding regression line in red.
We can see from the value of the regression line slope
and from the graph in Figure 1 that the correlation between BUI and FWI is positive. This is unsurprising: the Buildup Index measures the amount of fuel available in the environment, so higher BUI means that fires spread more easily once they start, corresponding to higher fire risk.
We want to model the relation between an independent variable
and a dependent variable
with a line of the form:
where
is the slope of the line and
is its y-intercept.
To find the best-fitting line, we use the method of least squares, which minimizes the sum of squared residuals, i.e. the difference between observed and predicted values:
Setting the derivative of
with respect to
and
to zero, the following formulas for
and
are found:
We just need to use the same code that we used during the lecture, extracting the BUI from the dataset with x =
my_dataset["BUI"].values. The final Python code for the solution is the following:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
def linear_regression(x, y):
sum_x = np.sum(x)
sum_y = np.sum(y)
sum_xy = np.sum(x * y)
sum_x2 = np.sum(x ** 2)
n = len(x)
numerator = n * sum_xy - sum_x * sum_y
denominator = n * sum_x2 - sum_x ** 2
m = numerator / denominator
q = (sum_y - m * sum_x) / n
return m, q
my_dataset = pd.read_csv('Algerian_forest_dataset.csv')
x = my_dataset['BUI'].values
y = my_dataset['FWI'].values
m, q = linear_regression(x, y)
print('Linear model results:')
print(f'Slope (m): {m}')
print(f'Y-intercept (q): {q}')
plt.figure(figsize=(16, 10))
plt.scatter(x, y, color='blue', alpha=0.5, label='Data points')
plt.plot(x, m * x + q, color='red', label='Regression line')
plt.xlabel('Temperature')
plt.ylabel('FWI')
plt.title('Linear Regression: FWI vs BUI')
plt.grid(True)
plt.legend()
plt.show()
We just need to use the same code that we used during the lecture, extracting the BUI from the dataset with x =
my_dataset.BUI. The final Matlab code for the solution is the following:
my_dataset = readtable('Algerian_forest_dataset.csv');
x = my_dataset.BUI;
y = my_dataset.FWI;
[m, q] = my_linear_regression(x, y);
% Print results
disp('Linear model results:');
fprintf('Slope (m): %f\n', m);
fprintf('Y-intercept (q): %f\n', q);
figure;
scatter(x, y, 'blue', 'filled', 'MarkerFaceAlpha', 0.5);
hold on;
plot(x, m * x + q, 'red', 'LineWidth', 2);
xlabel('Temperature');
ylabel('FWI');
title('Linear Regression: FWI vs BUI');
grid on;
legend('Data points', 'Regression line');



