Proyecto Final Model
Proyecto Final Model
Collecting cartopy
Downloading Cartopy-0.23.0-cp310-cp310-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.6/11.6 MB 39.0 MB/s eta
0:00:00
ent already satisfied: numpy>=1.21 in /usr/local/lib/python3.10/dist-
packages (from cartopy) (1.25.2)
Requirement already satisfied: matplotlib>=3.5 in
/usr/local/lib/python3.10/dist-packages (from cartopy) (3.7.1)
Requirement already satisfied: shapely>=1.7 in
/usr/local/lib/python3.10/dist-packages (from cartopy) (2.0.4)
Requirement already satisfied: packaging>=20 in
/usr/local/lib/python3.10/dist-packages (from cartopy) (24.0)
Requirement already satisfied: pyshp>=2.3 in
/usr/local/lib/python3.10/dist-packages (from cartopy) (2.3.1)
Requirement already satisfied: pyproj>=3.3.1 in
/usr/local/lib/python3.10/dist-packages (from cartopy) (3.6.1)
Requirement already satisfied: contourpy>=1.0.1 in
/usr/local/lib/python3.10/dist-packages (from matplotlib>=3.5-
>cartopy) (1.2.1)
Requirement already satisfied: cycler>=0.10 in
/usr/local/lib/python3.10/dist-packages (from matplotlib>=3.5-
>cartopy) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in
/usr/local/lib/python3.10/dist-packages (from matplotlib>=3.5-
>cartopy) (4.51.0)
Requirement already satisfied: kiwisolver>=1.0.1 in
/usr/local/lib/python3.10/dist-packages (from matplotlib>=3.5-
>cartopy) (1.4.5)
Requirement already satisfied: pillow>=6.2.0 in
/usr/local/lib/python3.10/dist-packages (from matplotlib>=3.5-
>cartopy) (9.4.0)
Requirement already satisfied: pyparsing>=2.3.1 in
/usr/local/lib/python3.10/dist-packages (from matplotlib>=3.5-
>cartopy) (3.1.2)
Requirement already satisfied: python-dateutil>=2.7 in
/usr/local/lib/python3.10/dist-packages (from matplotlib>=3.5-
>cartopy) (2.8.2)
Requirement already satisfied: certifi in
/usr/local/lib/python3.10/dist-packages (from pyproj>=3.3.1->cartopy)
(2024.2.2)
Requirement already satisfied: six>=1.5 in
/usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7-
>matplotlib>=3.5->cartopy) (1.16.0)
Installing collected packages: cartopy
Successfully installed cartopy-0.23.0
Clean data
# Seleccion de variables y nombre de las columnas
def change_column_names(df):
columns = {
"latitude": "latitude", "longitude": "longitude",
"population_density": "population_density", "General class":
"land_cover_type",
"class": "land_cover_subtype", "Sub-class":
"vegetation_percent", "date": "date", "ws": "wind_speed", "vpd":
"vapor_pressure_deficit",
"vap": "vapor_pressure", "tmin": "minimum_temperature",
"tmax": "maximum_temperature", "swe": "snow_water_equivalent",
"srad": "surface_shortwave_radiation", "soil":
"soil_moisture", "q": "runoff", "ppt": "precipitation_accumulation",
"pet": "Reference_evapotranspiration", "def":
"climate_water_deficit", "aet": "actual_Evapotranspiration",
"PDSI": "palmer_drought_severity_index", "brightness":
"brightness_temperature", "scan": "scan_fire_size", "track":
"track_fire_size",
"confidence": "confidence", "frp": "fire_radiative_power",
"daynight": "daynight", "type": "fire_type", "n_pixels":
"n_pixels_ndvi",
"vim": "ndvi", "vim_avg": "ndvi_long_term_average", "viq":
"ndvi_anomaly_percent", "year": "year"
}
return df.dropna().rename(columns=columns)[columns.values()]
x = int(value)
if 0 <= x < 30:
return "l"
elif 30 <= x < 80:
return "n"
return "h"
models
def get_models_path():
models_path = "./models"
if not os.path.exists(models_path):
os.makedirs(models_path)
return models_path
def get_root_directory():
root_directory_path = "./datasets"
if not os.path.exists(root_directory_path):
os.makedirs(root_directory_path)
return root_directory_path
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
random_forest_model = RandomForestClassifier(n_estimators=120,
max_depth=10, class_weight="balanced", random_state=42)
random_forest_info = get_avg_training(X, y, random_forest_model)
bayesian_model = GaussianNB()
bayesian_info = get_avg_training(X, y, bayesian_model)
Describing data
models_path = get_models_path()
root_directory_path = get_root_directory()
df_final = pd.read_pickle(f'{root_directory_path}/final_dataset.pkl')
target = "confidence"
df_final[target] = df_final[target].apply(asign_class)
df_final = change_column_names(df_final)
df_final.head()
{"type":"dataframe","variable_name":"df_final"}
df_final.to_csv(f'{root_directory_path}/final_dataset.csv',
index=False)
df_final_cleanned, dict_values =
get_general_dataframe(df_final.copy(), target)
df_final_cleanned_codes = df_final_cleanned.copy()
df_final_cleanned_codes[target] =
df_final_cleanned_codes[target].astype('category').cat.codes
df_final_cleanned[target].value_counts()
confidence
n 1521844
h 208636
l 100449
Name: count, dtype: int64
df_final_cleanned.columns
corr = df_final_cleanned_codes.corr()
plt.figure(figsize=(16, 14))
sns.set(font_scale=0.7)
print("Random Forest")
print(f"rf_accuracy={rf_accuracy}")
print(f"rf_precision={rf_precision}")
print(f"rf_recall={rf_recall}")
print(f"rf_f1={rf_f1}")
print(f"rf_cm:\n{rf_cm}")
Random Forest
rf_accuracy=0.7525005076318212
rf_precision=0.7525005076318212
rf_recall=0.7525005076318212
rf_f1=0.7525005076318212
rf_cm:
l n h
l 67544.0 21275.0 11630.0
n 355747.0 1105059.0 61038.0
h 1175.0 2289.0 205172.0
print("Bayesian Model")
print(f"bm_accuracy={bm_accuracy}")
print(f"bm_precision={bm_precision}")
print(f"bm_recall={bm_recall}")
print(f"bm_f1={bm_f1}")
print(f"bm_cm:\n{bm_cm}")
Bayesian Model
bm_accuracy=0.6458054910823028
bm_precision=0.6458054910823028
bm_recall=0.6458054910823028
bm_f1=0.6458054910823028
bm_cm:
l n h
l 17212.0 54666.0 28571.0
n 136139.0 1006275.0 379430.0
h 12520.0 37179.0 158937.0
brightness_temperature = 0.36536143226513207
fire_radiative_power = 0.26298131436302397
scan_fire_size = 0.19816786282273172
daynight = 0.05344582322736113
year = 0.02746812323112576
latitude = 0.008749474713167015
month = 0.007927916793298018
ndvi = 0.007855331489897404
population_density = 0.006623341421557317
vapor_pressure_deficit = 0.006342677338069368
longitude = 0.00460010894885883
palmer_drought_severity_index = 0.004476762419985014
climate_water_deficit = 0.004437413280390877
vapor_pressure = 0.00417743441088386
Reference_evapotranspiration = 0.0037091304899637884
fire_type = 0.003679213372044027
wind_speed = 0.003615540209061185
soil_moisture = 0.0035809291293696765
land_cover_type = 0.0031771276115307783
ndvi_anomaly_percent = 0.002763994002717311
precipitation_accumulation = 0.0024544810602002078
land_cover_subtype = 0.002428343721722926
runoff = 0.0024117304912774583
minimum_temperature = 0.0023194547493360323
n_pixels_ndvi = 0.0021459017251619786
surface_shortwave_radiation = 0.0013781765069856905
day = 0.0013768713136899481
seasons = 0.001300280176602127
vegetation_percent = 0.0010427064968135003
snow_water_equivalent = 1.1022180412770868e-06
most_influyent_index = np.argmax(np.abs(rf_probs))
most_influyent_name =
reduced_df.drop(columns=[target]).columns[most_influyent_index]
most_influyent_value = rf_probs[most_influyent_index]
print(f"{most_influyent_name} = {most_influyent_value}")
brightness_temperature = 0.36536143226513207
# confidences (l, n, h)
confidences = df_final_cleanned[target].unique()
colors = ["yellow", "orange", "red"]
year = 2022
# rainy season
coords = df_final_cleanned[
(df_final_cleanned[target] == conf) &
(df_final_cleanned["seasons"] == 1) &
(df_final_cleanned["year"] == year)][['longitude',
'latitude']].values
lon_values, lat_values = coords[:, 0], coords[:, 1]
ax2.scatter(lon_values, lat_values, color=color, s=50,
transform=ccrs.PlateCarree(), zorder=5)
# Mostrar el mapa
plt.suptitle('Mapa de incendios')
plt.show()
/usr/local/lib/python3.10/dist-packages/cartopy/io/__init__.py:241:
DownloadWarning: Downloading:
https://naturalearth.s3.amazonaws.com/10m_physical/ne_10m_land.zip
warnings.warn(f'Downloading: {url}', DownloadWarning)
/usr/local/lib/python3.10/dist-packages/cartopy/io/__init__.py:241:
DownloadWarning: Downloading:
https://naturalearth.s3.amazonaws.com/10m_cultural/ne_10m_admin_0_boun
dary_lines_land.zip
warnings.warn(f'Downloading: {url}', DownloadWarning)
/usr/local/lib/python3.10/dist-packages/cartopy/io/__init__.py:241:
DownloadWarning: Downloading:
https://naturalearth.s3.amazonaws.com/10m_physical/ne_10m_coastline.zi
p
warnings.warn(f'Downloading: {url}', DownloadWarning)
/usr/local/lib/python3.10/dist-packages/cartopy/io/__init__.py:241:
DownloadWarning: Downloading:
https://naturalearth.s3.amazonaws.com/10m_physical/ne_10m_lakes.zip
warnings.warn(f'Downloading: {url}', DownloadWarning)
/usr/local/lib/python3.10/dist-packages/cartopy/io/__init__.py:241:
DownloadWarning: Downloading:
https://naturalearth.s3.amazonaws.com/10m_physical/ne_10m_rivers_lake_
centerlines.zip
warnings.warn(f'Downloading: {url}', DownloadWarning)
save_models(dict_values, random_forest_model, bayesian_model,
models_path)
dict_values
# Medellin
ff_value = {
'latitude': 6.25184,
'longitude': -75.56359,
'population_density': 19134.373047,
'land_cover_type': 'Terra Firma',
'land_cover_subtype': 'Tree cover',
'vegetation_percent': '35% short vegetation cover',
'wind_speed': 1.3,
'vapor_pressure_deficit': 0.82,
'vapor_pressure': 1.913,
'minimum_temperature': 289.45,
'snow_water_equivalent': 0.0,
'surface_shortwave_radiation': 207.8,
'soil_moisture': 36,
'runoff': 108.4,
'precipitation_accumulation': 221.7,
'Reference_evapotranspiration': 113.3,
'climate_water_deficit': 0.0,
'palmer_drought_severity_index': -3.4,
'brightness_temperature': 321.6,
'scan_fire_size': 1.3,
'fire_radiative_power': 9.0,
'daynight': 'D',
'fire_type': 'presumed vegetation fire',
'n_pixels_ndvi': 15.0,
'ndvi': 0.7695,
'ndvi_anomaly_percent': 100.7927,
'year': 2023,
'seasons': 'dry season',
'month': 4,
'day': 2
}
value_predicted = random_forest_model.predict(mp_values)[0]
if value_predicted == "l":
print("0 - 30% de probabilidad de que ocurra un incendio")
elif value_predicted == "n":
print("30 - 80% de probabilidad de que ocurra un incendio")
elif value_predicted == "h":
print("80 - 100% de probabilidad de que ocurra un incendio")