Drought Identification and Trend Analysis - Admin 2 Level

Author

NCDP (Nitin Magima & Max Mauerman) / QMSS Innovation Lab (Samuel Gartenstein & Jiawen Donovan Sim)

Published

November 1, 2024

The Jupyter Notebook uses the following paper to recreate the analysis for drought identification and trend analysis in Kenya using Google Earth Engine (GEE) for Python, follow these steps:

Analysis Steps

Step 1: Data Collection

Obtain long-term satellite-derived precipitation data using the CHIRPS dataset available in GEE. This data will be used to analyze drought conditions in Kenya.

Step 2: Study Area Definition

Define the study area using Kenya’s geographical boundaries.

Step 3: Data Preprocessing

  • Extract Monthly Precipitation: Extract monthly CHIRPS precipitation data for the study area to calculate Standardized Precipitation Index (SPI) at different time scales (e.g., SPI1, SPI3, SPI6, SPI12).
  • Clip to Region: Clip the precipitation data to Kenya’s boundaries to ensure that the analysis focuses solely on Kenya. This step has already been implemented using GEE.

Step 4: Calculate Standardized Precipitation Index (SPI)

  • SPI Calculation: Calculate SPI at different time scales (1-, 3-, 6-, and 12-month) for drought evaluation. Convert the CHIRPS precipitation data into SPI values by fitting a gamma distribution to each pixel’s monthly time series.

Step 5: Drought Characterization

  • Identify Drought Events: Use run theory to identify drought events based on SPI values.
  • Drought Duration: Identify the number of months with SPI values below thresholds like -1.0 for moderate drought.
  • Drought Severity: Calculate the severity by summing all SPI values during the drought.
  • Drought Intensity: Calculate drought intensity as severity divided by duration.

Step 6: Trend Analysis

  • Mann-Kendall Test: Implement the Mann-Kendall trend test to determine trends in SPI values at annual, seasonal, and monthly time scales.
  • Sen’s Slope Estimator: Apply Sen’s slope estimator to understand the magnitude of the detected trends.

Step 7: Clustering Analysis of Drought Metrics

This step aims to perform a comprehensive clustering analysis on drought metrics across administrative units in Kenya, including data preprocessing, dimensionality reduction, and clustering.

Workflow Steps

  • Data Preprocessing:
    • Encode categorical features, and normalize numerical metrics using MinMaxScaler.
  • Feature Engineering:
    • Construct a feature vector (feature_vector) for clustering, including encoded categorical features and normalized numerical metrics.
  • Dimensionality Reduction Using PCA:
    • Determine the optimal number of components for dimensionality reduction by analyzing cumulative explained variance and applying PCA accordingly.
  • Clustering Analysis:
    • K-Means Clustering:
      • Determine the optimal number of clusters using the Elbow Method and apply K-Means.
      • Visualize the clusters geospatially.
    • Hierarchical Clustering:
      • Create a linkage matrix using Ward’s method and determine the optimal clusters using a dendrogram.
      • Assign cluster labels and visualize geospatially.
    • DBSCAN Clustering:
      • Perform a grid search to determine the best parameters (eps, min_samples) and use the Silhouette Score for optimization.
      • Assign cluster labels and visualize geospatially.

Step 8: EM-DAT Analysis

Compare historical drought years from the Emergency Events Database (EM-DAT) to SPI-derived results.

DISCLAIMER

This is a set of scripts shared for educational purposes only. Anyone who uses this code or its functionality or structure, assumes full liability and credits the author.

Map Disclaimer

The designations employed and the presentation of the material on this map do not imply the expression of any opinion whatsoever on the part of the author concerning the legal status of any country, territory, city or area or of its authorities, or concerning the delimitation of its frontiers or boundaries.

Import libraries

Requirement already satisfied: pymannkendall in /Users/magima/anaconda3/envs/gee/lib/python3.11/site-packages (1.4.3)
Requirement already satisfied: numpy in /Users/magima/anaconda3/envs/gee/lib/python3.11/site-packages (from pymannkendall) (1.26.4)
Requirement already satisfied: scipy in /Users/magima/anaconda3/envs/gee/lib/python3.11/site-packages (from pymannkendall) (1.10.0)
Note: you may need to restart the kernel to use updated packages.

Define Analysis Boundaries

We use the following crop calendar to select the regions and time periods to focus on.

We define boundaries for the Western and Rift Valley, and Eastern and Northern Kenya. We use the FAO GAUL: Global Administrative Unit Layers from Google Earth Engine (GEE) for the analysis.

GEE is a public data archive of petabytes of historical satellite imagery and geospatial datasets. The advantage lies in its remarkable computation speed as processing is outsourced to Google servers. The platform provides a variety of constantly updated datasets; no download of raw imagery is required. While it is free of charge, one still needs to activate access to Google Earth Engine with a valid Google account.

The Global Administrative Unit Layers (GAUL) compiles and disseminates the best available information on administrative units for all the countries in the world, providing a contribution to the standardization of the spatial dataset representing administrative units. The GAUL always maintains global layers with a unified coding system at country, first (e.g. departments), and second administrative levels (e.g. districts). Where data is available, it provides layers on a country by country basis down to third, fourth, and lowers levels.

Load CHIRPS Pentad Dataset

Define Time Period

The periods of interest are the planting, harvest, and rain seasons for different regions of Kenya. We will use these timelines to define the start and end dates for creating the relevant dekadal (10-day period) analysis from the CHIRPS pentad data.

  1. Western and Rift Valley
  • Long Rains: March to June
  • Planting: April to June
  • Long Rains Harvest: July to August & October to November
  1. Eastern and Northern Kenya:
  • Short Rains Harvest: February to March
  • Planting: March to May
  • Long Rains: March to June
  • Lean Season: September to November
  • Short Rains: October to December

Based on this calendar, we’ll use the start and end months for rainfall to define dekads using the CHIRPS Pentad dataset (‘UCSB-CHG/CHIRPS/PENTAD’).

Calculate Standardized Precipitation Index (SPI)

SPI Definitions

  • SPI1 (1-month scale)

Used to capture short-term monthly precipitation fluctuations and early warning of meteorological drought.

  • SPI3 (3-month scale)

Useful for seasonal drought analysis, which helps in understanding the drought conditions over a quarterly period, closely related to agricultural impacts.

  • SPI6 (6-month scale)

Provides insights into medium-term drought conditions, capturing both the end of one season and the start of another, which can influence soil moisture and crop yield over a longer period.

  • SPI12 (12-month scale)

Used to assess long-term drought conditions, representing annual fluctuations and providing insights into the overall hydrological drought scenario, which can affect groundwater recharge and surface water availability.

Selecting SPI Time Scales

Based on the above definitions we select the following time Scales for Long and Short Rains:

  • Long Rains: March to June (4-month period) - We will use 1-month and 3-month SPI for long rains to understand short-term and medium-term droughts.
  • Short Rains: October to December (3-month period) - We will use 1-month and 3-month SPI to capture variability within the shorter rainy season.

Calculate SPI

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 3936 entries, 2000-03-01 to 2023-12-01
Data columns (total 7 columns):
 #   Column         Non-Null Count  Dtype  
---  ------         --------------  -----  
 0   Unnamed: 0     3936 non-null   int64  
 1   year           3936 non-null   int64  
 2   month          3936 non-null   int64  
 3   region         3936 non-null   object 
 4   admin2_name    3936 non-null   object 
 5   precipitation  3936 non-null   float64
 6   season         3936 non-null   object 
dtypes: float64(1), int64(3), object(3)
memory usage: 246.0+ KB
   year  month                      region admin2_name      season  \
0  2000      3  Eastern and Northern Kenya     Garissa  Long Rains   
1  2000      4  Eastern and Northern Kenya     Garissa  Long Rains   
2  2000      5  Eastern and Northern Kenya     Garissa  Long Rains   
3  2000      6  Eastern and Northern Kenya     Garissa  Long Rains   
4  2001      3  Eastern and Northern Kenya     Garissa  Long Rains   

   precipitation       spi spi_scale  
0      14.838282 -0.484617         1  
1      48.755056  0.557650         1  
2      14.919035 -0.480644         1  
3       4.672698 -1.219055         1  
4      25.095673 -0.070452         1  

Identify Drought Events

To characterize droughts, we need to analyze the SPI values calculated previously to identify distinct drought events. This involves analyzing when SPI values fall below a threshold, like -1.0, to determine the duration, severity, and intensity of each drought event.

Definitions:

  • Drought Event: A period during which SPI is below a defined threshold (e.g., SPI < -1.0 for moderate drought).
  • Drought Duration: The number of months during which the SPI value stays below the threshold.
  • Drought Severity: The sum of all SPI values during the drought period.
  • Drought Intensity: Severity divided by duration, indicating how intense the drought is on average.
region admin2_name season spi_scale Drought Event Drought Duration Drought Severity Drought Intensity Drought Event_start Drought Event_end
0 Eastern and Northern Kenya Garissa Long Rains 1 1 9 -1.219055 -0.135451 2000-06 2001-03
1 Eastern and Northern Kenya Garissa Long Rains 1 2 9 -1.307802 -0.145311 2001-06 2002-03
2 Eastern and Northern Kenya Garissa Long Rains 1 3 9 -1.092735 -0.121415 2003-06 2004-03
3 Eastern and Northern Kenya Garissa Long Rains 1 4 9 -1.216412 -0.135157 2005-06 2006-03
4 Eastern and Northern Kenya Garissa Long Rains 1 5 9 -1.021041 -0.113449 2007-06 2008-03
... ... ... ... ... ... ... ... ... ... ...
1097 Western and Rift Valley West Pokot Long Rains 3 5 1 -1.065129 -1.065129 2012-03 2012-04
1098 Western and Rift Valley West Pokot Long Rains 3 6 12 -6.997462 -0.583122 2014-04 2015-04
1099 Western and Rift Valley West Pokot Long Rains 3 7 2 -2.752340 -1.376170 2017-04 2017-06
1100 Western and Rift Valley West Pokot Long Rains 3 8 1 -1.140949 -1.140949 2019-05 2019-06
1101 Western and Rift Valley West Pokot Long Rains 3 9 1 -1.314078 -1.314078 2022-04 2022-05

1102 rows × 10 columns

Note - The issue where drought durations exceed expected limits (e.g., durations exceeding 3 months for a “3-month SPI scale”) arises from the way the drought characterization function is designed. Specifically, the function might be identifying prolonged drought events that span beyond a single season or multi-month drought events that are not being properly split when SPI values rise above the threshold.

Due to continuous negative SPI Values, the function currently identifies a drought event as continuing until the SPI rises above the threshold. If the SPI remains below the threshold for an extended period (even beyond a season), the function will count that entire period as a single drought event.

Geospatial Analysis

Index(['id', 'ADM0_CODE', 'ADM0_NAME', 'ADM1_CODE', 'ADM1_NAME', 'ADM2_CODE',
       'ADM2_NAME', 'DISP_AREA', 'EXP2_YEAR', 'STATUS', 'STR2_YEAR',
       'Shape_Area', 'Shape_Leng', 'geometry', 'region', 'admin2_name',
       'season', 'spi_scale', 'Drought Event', 'Drought Duration',
       'Drought Severity', 'Drought Intensity', 'Drought Event_start',
       'Drought Event_end'],
      dtype='object')
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1102 entries, 0 to 1101
Data columns (total 10 columns):
 #   Column               Non-Null Count  Dtype  
---  ------               --------------  -----  
 0   region               1102 non-null   object 
 1   admin2_name          1102 non-null   object 
 2   season               1102 non-null   object 
 3   spi_scale            1102 non-null   object 
 4   Drought Event        1102 non-null   int64  
 5   Drought Duration     1102 non-null   int64  
 6   Drought Severity     1102 non-null   float64
 7   Drought Intensity    1102 non-null   float64
 8   Drought Event_start  1102 non-null   object 
 9   Drought Event_end    1102 non-null   object 
dtypes: float64(2), int64(2), object(6)
memory usage: 86.2+ KB

Trend Analysis

The Mann-Kendall test is a non-parametric test used to identify trends in time series data. It is particularly useful for climate data, like SPI values, to determine if there’s a significant increasing or decreasing trend in drought conditions over time. We will also use Sen’s slope estimator to determine the magnitude of the trends identified.

Trend Analysis Results (Mann-Kendall Test and Sen's Slope):
                     region admin2_name      season time_scale       trend  \
52  Western and Rift Valley       Buret  Long Rains    monthly  increasing   
53  Western and Rift Valley       Buret  Long Rains   seasonal  increasing   
84  Western and Rift Valley      Vihiga  Long Rains    monthly  increasing   
85  Western and Rift Valley      Vihiga  Long Rains   seasonal  increasing   

     p_value  sen_slope  
52  0.042020   0.002952  
53  0.042020   0.002952  
84  0.034196   0.002750  
85  0.034196   0.002750  

Clustering Analysis

Functions

Clustering Using Precipitation Across Years

Data Preprocessing

Long Rains Dataset:
            Unnamed: 0                      region admin2_name  precipitation  \
date                                                                            
2000-03-01           0     Western and Rift Valley      Vihiga      98.703641   
2000-03-01          32     Western and Rift Valley     Kajiado      60.961387   
2000-03-01          36     Western and Rift Valley       Keiyo      33.846755   
2000-03-01          40     Western and Rift Valley     Kericho      69.627150   
2000-03-01        1956  Eastern and Northern Kenya      Mwingi      22.458084   

                season year_month  
date                               
2000-03-01  Long Rains    2000_03  
2000-03-01  Long Rains    2000_03  
2000-03-01  Long Rains    2000_03  
2000-03-01  Long Rains    2000_03  
2000-03-01  Long Rains    2000_03  

Short Rains Dataset:
            Unnamed: 0                      region admin2_name  precipitation  \
date                                                                            
2000-10-01        3096  Eastern and Northern Kenya      Moyale      33.095842   
2000-10-01        3093  Eastern and Northern Kenya    Marsabit      11.912731   
2000-10-01        3090  Eastern and Northern Kenya     Makueni      23.793805   
2000-10-01        3072  Eastern and Northern Kenya     Tharaka      46.677001   
2000-10-01        3087  Eastern and Northern Kenya    Machakos      33.047264   

                 season year_month  
date                                
2000-10-01  Short Rains    2000_10  
2000-10-01  Short Rains    2000_10  
2000-10-01  Short Rains    2000_10  
2000-10-01  Short Rains    2000_10  
2000-10-01  Short Rains    2000_10  
Pivoted Long Rains Dataset:
year_month      2000_03     2000_04     2000_05     2000_06     2001_03  \
admin2_name                                                               
Baringo       21.158160   26.254335   67.760484   62.071824  107.876293   
Bomet        123.038907  157.774249  122.221624   76.965487  108.035221   
Buret         90.188825  180.259207  183.476531  123.148213  117.618238   
Garissa       14.838282   48.755056   14.919035    4.672698   25.095673   
Ijara         28.224835   80.635717  166.129456   60.321118   37.603699   

year_month      2001_04     2001_05     2001_06     2002_03     2002_04  ...  \
admin2_name                                                              ...   
Baringo      140.770504   60.742941  116.119686   94.835013   89.783375  ...   
Bomet        201.834953  176.518491  119.077552  153.402600  181.134024  ...   
Buret        233.503763  251.474740  171.158641  161.586497  201.256590  ...   
Garissa       75.805055   14.305178    3.972108   20.505164  151.002978  ...   
Ijara         87.470902  207.430744   68.322061   24.393812  127.179843  ...   

year_month      2021_05     2021_06     2022_03     2022_04     2022_05  \
admin2_name                                                               
Baringo      195.075285   61.711254   39.697042   90.767013   99.540372   
Bomet        186.928919   90.685318   95.356798  199.171484  239.053431   
Buret        288.166895  126.581533  120.307225  219.413341  308.806540   
Garissa       12.554367    5.484871   14.681459   40.689669   18.707744   
Ijara         66.593020   60.673507    9.105074   95.195305  151.826904   

year_month      2022_06     2023_03     2023_04     2023_05     2023_06  
admin2_name                                                              
Baringo       56.311873  107.744030  136.442762   89.653120   79.320371  
Bomet         91.623328  224.246994  264.044649  154.388733  141.129695  
Buret        131.548684  234.065587  326.073801  236.538564  186.513851  
Garissa        5.019645   67.714332  207.831759   10.225923    4.390235  
Ijara         55.979699   41.513142  292.518471   60.168029   40.396014  

[5 rows x 96 columns]

Pivoted Short Rains Dataset:
year_month     2000_10     2000_11     2000_12    2001_10     2001_11  \
admin2_name                                                             
Garissa      28.525727   81.978251   25.519197  14.522468  166.835117   
Ijara        70.277407   79.454738   43.061873  17.601509  185.111359   
Isiolo       17.747121   66.150812   27.610037  14.778846   99.592018   
Kitui        26.455039  225.003964  140.971973  17.442512  259.734871   
Machakos     33.047264  204.536912   93.570372  35.267075  281.369655   

year_month     2001_12    2002_10     2002_11     2002_12     2003_10  ...  \
admin2_name                                                            ...   
Garissa      31.331984  65.391797  126.902972   80.302920   67.385378  ...   
Ijara        51.519862  60.874063   88.263089   98.854677  116.582935  ...   
Isiolo       32.128907  86.521986   75.657394  114.233474   32.038581  ...   
Kitui        85.896819  40.282733  180.453359  169.469417   25.664708  ...   
Machakos     29.280513  52.751512  182.826370  198.331093   58.720459  ...   

year_month     2020_12    2021_10     2021_11     2021_12    2022_10  \
admin2_name                                                            
Garissa      34.242412  14.381440   69.325578  157.460035  22.259744   
Ijara        37.380963  16.171811   69.187826  174.963201  29.320231   
Isiolo       34.766376  18.041942   47.671556   95.643322  14.175735   
Kitui        99.918876  21.286992   99.648856  224.379703  16.784340   
Machakos     70.029163  33.658633  151.957980  148.511450  23.977999   

year_month      2022_11    2022_12    2023_10     2023_11     2023_12  
admin2_name                                                            
Garissa      125.872363  29.488529  53.000556  282.346753   50.991061  
Ijara        122.343550  21.044737  45.109783  294.643251  153.084007  
Isiolo        70.933897  42.700765  56.026558  209.128286   26.538066  
Kitui        156.003805  73.307000  51.106812  395.105079  114.212641  
Machakos     209.422459  39.522342  53.958168  378.292180   61.576969  

[5 rows x 72 columns]

Clustering Long Rains

year_month    2000_03   2000_04   2000_05   2000_06   2001_03   2001_04  \
admin2_name                                                               
Baringo      0.096580  0.063537  0.274455  0.430156  0.641807  0.458898   
Bomet        0.711431  0.863275  0.512600  0.534777  0.642916  0.757735   
Buret        0.513180  1.000000  0.780452  0.859188  0.709744  0.912716   
Garissa      0.058439  0.200358  0.043392  0.026955  0.064528  0.140969   
Ijara        0.139227  0.394216  0.704598  0.417858  0.151754  0.198059   

year_month    2001_05   2001_06   2002_03   2002_04  ...   2021_05   2021_06  \
admin2_name                                          ...                       
Baringo      0.228780  0.579690  0.337946  0.103239  ...  0.597923  0.467513   
Bomet        0.696916  0.594607  0.589300  0.354884  ...  0.571236  0.689317   
Buret        1.000000  0.857271  0.624423  0.410316  ...  0.902883  0.964111   
Garissa      0.041010  0.014089  0.018944  0.271881  ...  0.000000  0.037086   
Ijara        0.821909  0.338629  0.035633  0.206255  ...  0.177026  0.459569   

year_month    2022_03   2022_04   2022_05   2022_06   2023_03   2023_04  \
admin2_name                                                               
Baringo      0.181259  0.250868  0.297095  0.425200  0.274930  0.079094   
Bomet        0.511047  0.793933  0.765706  0.694975  0.758545  0.420933   
Buret        0.658880  0.895337  1.000000  1.000000  0.799303  0.587105   
Garissa      0.033040  0.000000  0.025586  0.033333  0.108764  0.270341   
Ijara        0.000000  0.273052  0.472720  0.422662  0.000000  0.497212   

year_month    2023_05   2023_06  
admin2_name                      
Baringo      0.311555  0.296942  
Bomet        0.565482  0.532086  
Buret        0.887717  0.704742  
Garissa      0.000000  0.011882  
Ijara        0.195899  0.148860  

[5 rows x 96 columns]
Feature Vector Construction
year_month    2000_03   2000_04   2000_05   2000_06   2001_03   2001_04  \
admin2_name                                                               
Baringo      0.096580  0.063537  0.274455  0.430156  0.641807  0.458898   
Bomet        0.711431  0.863275  0.512600  0.534777  0.642916  0.757735   
Buret        0.513180  1.000000  0.780452  0.859188  0.709744  0.912716   
Garissa      0.058439  0.200358  0.043392  0.026955  0.064528  0.140969   
Ijara        0.139227  0.394216  0.704598  0.417858  0.151754  0.198059   

year_month    2001_05   2001_06   2002_03   2002_04  ...   2021_05   2021_06  \
admin2_name                                          ...                       
Baringo      0.228780  0.579690  0.337946  0.103239  ...  0.597923  0.467513   
Bomet        0.696916  0.594607  0.589300  0.354884  ...  0.571236  0.689317   
Buret        1.000000  0.857271  0.624423  0.410316  ...  0.902883  0.964111   
Garissa      0.041010  0.014089  0.018944  0.271881  ...  0.000000  0.037086   
Ijara        0.821909  0.338629  0.035633  0.206255  ...  0.177026  0.459569   

year_month    2022_03   2022_04   2022_05   2022_06   2023_03   2023_04  \
admin2_name                                                               
Baringo      0.181259  0.250868  0.297095  0.425200  0.274930  0.079094   
Bomet        0.511047  0.793933  0.765706  0.694975  0.758545  0.420933   
Buret        0.658880  0.895337  1.000000  1.000000  0.799303  0.587105   
Garissa      0.033040  0.000000  0.025586  0.033333  0.108764  0.270341   
Ijara        0.000000  0.273052  0.472720  0.422662  0.000000  0.497212   

year_month    2023_05   2023_06  
admin2_name                      
Baringo      0.311555  0.296942  
Bomet        0.565482  0.532086  
Buret        0.887717  0.704742  
Garissa      0.000000  0.011882  
Ijara        0.195899  0.148860  

[5 rows x 96 columns]

To optimize the number of components (n_components) for Principal Component Analysis (PCA), we need to determine how many components are sufficient to retain the majority of the variance in the data. This can be done by plotting the explained variance ratio and observing when additional components contribute diminishing returns.

Cumulative explained variance: [0.74251072 0.84997348 0.8964526  0.93117238 0.94811902 0.96007422
 0.96770117 0.9734383  0.97849543 0.98279654 0.98568395 0.98800517
 0.99018409 0.99191187 0.99316233 0.99422196 0.99516996 0.99600306
 0.99668762 0.99723921 0.99773607 0.99815746 0.9985256  0.99884514
 0.99911939 0.99934184 0.99953087 0.99968828 0.99981288 0.99991382
 1.         1.        ]
K-Means Clustering

The Elbow Method is used to determine the optimal number of clusters (n_clusters) in K-Means clustering. It involves fitting the K-Means algorithm with different values for n_clusters and plotting the Within-Cluster Sum of Squares (WCSS) to observe the point where the WCSS starts to diminish at a reduced rate (forming an “elbow”).

WCSS for each number of clusters: [255.54274950218405, 105.87612469134133, 71.3442174787841, 59.344858427616686, 49.75165876716597, 41.245550456632344, 33.338144565963304, 26.45530218110892, 23.07632608374005, 19.957846403081795]
year_month    2000_03   2000_04   2000_05   2000_06   2001_03   2001_04  \
admin2_name                                                               
Baringo      0.096580  0.063537  0.274455  0.430156  0.641807  0.458898   
Bomet        0.711431  0.863275  0.512600  0.534777  0.642916  0.757735   
Buret        0.513180  1.000000  0.780452  0.859188  0.709744  0.912716   
Garissa      0.058439  0.200358  0.043392  0.026955  0.064528  0.140969   
Ijara        0.139227  0.394216  0.704598  0.417858  0.151754  0.198059   

year_month    2001_05   2001_06   2002_03   2002_04  ...   2021_06   2022_03  \
admin2_name                                          ...                       
Baringo      0.228780  0.579690  0.337946  0.103239  ...  0.467513  0.181259   
Bomet        0.696916  0.594607  0.589300  0.354884  ...  0.689317  0.511047   
Buret        1.000000  0.857271  0.624423  0.410316  ...  0.964111  0.658880   
Garissa      0.041010  0.014089  0.018944  0.271881  ...  0.037086  0.033040   
Ijara        0.821909  0.338629  0.035633  0.206255  ...  0.459569  0.000000   

year_month    2022_04   2022_05   2022_06   2023_03   2023_04   2023_05  \
admin2_name                                                               
Baringo      0.250868  0.297095  0.425200  0.274930  0.079094  0.311555   
Bomet        0.793933  0.765706  0.694975  0.758545  0.420933  0.565482   
Buret        0.895337  1.000000  1.000000  0.799303  0.587105  0.887717   
Garissa      0.000000  0.025586  0.033333  0.108764  0.270341  0.000000   
Ijara        0.273052  0.472720  0.422662  0.000000  0.497212  0.195899   

year_month    2023_06  cluster  
admin2_name                     
Baringo      0.296942        1  
Bomet        0.532086        2  
Buret        0.704742        2  
Garissa      0.011882        0  
Ijara        0.148860        1  

[5 rows x 97 columns]

Hierarchical Clustering

Ward’s method minimizes variance and helps in identifying clusters of similar rainfall amounts. It provides a dendrogram, which can help determine different levels of clustering.

DBSCAN Clustering
No valid clusters were found with the given parameters.
Best eps: None, Best min_samples: None, Best Silhouette Score: None

Clustering Short Rains

year_month    2000_10   2000_11   2000_12   2001_10   2001_11   2001_12  \
admin2_name                                                               
Garissa      0.284641  0.208173  0.095534  0.000000  0.420543  0.264046   
Ijara        1.000000  0.196827  0.203591  0.148426  0.482794  0.536334   
Isiolo       0.099964  0.137008  0.108413  0.012359  0.191502  0.274794   
Kitui        0.249163  0.851259  0.806684  0.140762  0.736974  1.000000   
Machakos     0.362112  0.759233  0.514706  1.000000  0.810666  0.236376   

year_month    2002_10   2002_11   2002_12   2003_10  ...   2020_12   2021_10  \
admin2_name                                          ...                       
Garissa      0.323823  0.533384  0.299690  0.333793  ...  0.234417  0.008769   
Ijara        0.288926  0.332205  0.406103  0.646804  ...  0.264231  0.067464   
Isiolo       0.487041  0.266573  0.494315  0.108905  ...  0.239394  0.128774   
Kitui        0.129870  0.812196  0.811147  0.068352  ...  0.858297  0.235158   
Machakos     0.226184  0.824551  0.976697  0.278664  ...  0.574365  0.640744   

year_month    2021_11   2021_12   2022_10   2022_11   2022_12   2023_10  \
admin2_name                                                               
Garissa      0.291626  0.610453  0.168659  0.406485  0.239019  0.069132   
Ijara        0.290613  0.686446  0.315963  0.392293  0.132928  0.018668   
Isiolo       0.132492  0.342065  0.000000  0.185537  0.405021  0.088484   
Kitui        0.514469  0.900997  0.054424  0.527665  0.789567  0.057021   
Machakos     0.898885  0.571602  0.204507  0.742501  0.365087  0.075256   

year_month    2023_11   2023_12  
admin2_name                      
Garissa      0.380445  0.295157  
Ijara        0.408808  1.000000  
Isiolo       0.211562  0.126335  
Kitui        0.640529  0.731635  
Machakos     0.601749  0.368241  

[5 rows x 72 columns]
Feature Vector Construction
year_month    2000_10   2000_11   2000_12   2001_10   2001_11   2001_12  \
admin2_name                                                               
Garissa      0.284641  0.208173  0.095534  0.000000  0.420543  0.264046   
Ijara        1.000000  0.196827  0.203591  0.148426  0.482794  0.536334   
Isiolo       0.099964  0.137008  0.108413  0.012359  0.191502  0.274794   
Kitui        0.249163  0.851259  0.806684  0.140762  0.736974  1.000000   
Machakos     0.362112  0.759233  0.514706  1.000000  0.810666  0.236376   

year_month    2002_10   2002_11   2002_12   2003_10  ...   2020_12   2021_10  \
admin2_name                                          ...                       
Garissa      0.323823  0.533384  0.299690  0.333793  ...  0.234417  0.008769   
Ijara        0.288926  0.332205  0.406103  0.646804  ...  0.264231  0.067464   
Isiolo       0.487041  0.266573  0.494315  0.108905  ...  0.239394  0.128774   
Kitui        0.129870  0.812196  0.811147  0.068352  ...  0.858297  0.235158   
Machakos     0.226184  0.824551  0.976697  0.278664  ...  0.574365  0.640744   

year_month    2021_11   2021_12   2022_10   2022_11   2022_12   2023_10  \
admin2_name                                                               
Garissa      0.291626  0.610453  0.168659  0.406485  0.239019  0.069132   
Ijara        0.290613  0.686446  0.315963  0.392293  0.132928  0.018668   
Isiolo       0.132492  0.342065  0.000000  0.185537  0.405021  0.088484   
Kitui        0.514469  0.900997  0.054424  0.527665  0.789567  0.057021   
Machakos     0.898885  0.571602  0.204507  0.742501  0.365087  0.075256   

year_month    2023_11   2023_12  
admin2_name                      
Garissa      0.380445  0.295157  
Ijara        0.408808  1.000000  
Isiolo       0.211562  0.126335  
Kitui        0.640529  0.731635  
Machakos     0.601749  0.368241  

[5 rows x 72 columns]

To optimize the number of components (n_components) for Principal Component Analysis (PCA), we need to determine how many components are sufficient to retain the majority of the variance in the data. This can be done by plotting the explained variance ratio and observing when additional components contribute diminishing returns.

Cumulative explained variance: [0.59522109 0.8211696  0.88716116 0.93186101 0.95252454 0.96972832
 0.98175716 0.9897867  0.99411478 0.99741308 1.         1.        ]
K-Means Clustering

The Elbow Method is used to determine the optimal number of clusters (n_clusters) in K-Means clustering. It involves fitting the K-Means algorithm with different values for n_clusters and plotting the Within-Cluster Sum of Squares (WCSS) to observe the point where the WCSS starts to diminish at a reduced rate (forming an “elbow”).

WCSS for each number of clusters: [85.05162989048733, 39.22883815490832, 28.943588452580556, 19.769044475034054, 13.57946090789774, 8.94049650882713, 6.383179774371918, 4.370977588128288, 2.689216964819734, 1.441553750718366]
year_month    2000_10   2000_11   2000_12   2001_10   2001_11   2001_12  \
admin2_name                                                               
Garissa      0.284641  0.208173  0.095534  0.000000  0.420543  0.264046   
Ijara        1.000000  0.196827  0.203591  0.148426  0.482794  0.536334   
Isiolo       0.099964  0.137008  0.108413  0.012359  0.191502  0.274794   
Kitui        0.249163  0.851259  0.806684  0.140762  0.736974  1.000000   
Machakos     0.362112  0.759233  0.514706  1.000000  0.810666  0.236376   

year_month    2002_10   2002_11   2002_12   2003_10  ...   2021_10   2021_11  \
admin2_name                                          ...                       
Garissa      0.323823  0.533384  0.299690  0.333793  ...  0.008769  0.291626   
Ijara        0.288926  0.332205  0.406103  0.646804  ...  0.067464  0.290613   
Isiolo       0.487041  0.266573  0.494315  0.108905  ...  0.128774  0.132492   
Kitui        0.129870  0.812196  0.811147  0.068352  ...  0.235158  0.514469   
Machakos     0.226184  0.824551  0.976697  0.278664  ...  0.640744  0.898885   

year_month    2021_12   2022_10   2022_11   2022_12   2023_10   2023_11  \
admin2_name                                                               
Garissa      0.610453  0.168659  0.406485  0.239019  0.069132  0.380445   
Ijara        0.686446  0.315963  0.392293  0.132928  0.018668  0.408808   
Isiolo       0.342065  0.000000  0.185537  0.405021  0.088484  0.211562   
Kitui        0.900997  0.054424  0.527665  0.789567  0.057021  0.640529   
Machakos     0.571602  0.204507  0.742501  0.365087  0.075256  0.601749   

year_month    2023_12  cluster  
admin2_name                     
Garissa      0.295157        1  
Ijara        1.000000        1  
Isiolo       0.126335        1  
Kitui        0.731635        2  
Machakos     0.368241        2  

[5 rows x 73 columns]

Hierarchical Clustering

Ward’s method minimizes variance and helps in identifying clusters of similar rainfall amounts. It provides a dendrogram, which can help determine different levels of clustering.

DBSCAN Clustering
Best eps: 3.1
Best min_samples: 2
Best Silhouette Score: 0.42413537202628665
year_month   dbscan_optimized_cluster
admin2_name                          
Garissa                             0
Ijara                               0
Isiolo                              0
Kitui                               1
Machakos                            1
Best eps: 3.1, Best min_samples: 2, Best Silhouette Score: 0.42413537202628665

Clustering Using Drought Identification

  admin2_name      season spi_scale  Drought Duration  Drought Severity  \
0     Garissa  Long Rains         1                 9         -1.219055   
1     Garissa  Long Rains         1                 9         -1.307802   
2     Garissa  Long Rains         1                 9         -1.092735   
3     Garissa  Long Rains         1                 9         -1.216412   
4     Garissa  Long Rains         1                 9         -1.021041   

   Drought Intensity Drought Event_start Drought Event_end  
0          -0.135451             2000-06           2001-03  
1          -0.145311             2001-06           2002-03  
2          -0.121415             2003-06           2004-03  
3          -0.135157             2005-06           2006-03  
4          -0.113449             2007-06           2008-03  
Index 0: Key = 1_long_rains, Dataset Shape = (555, 6)
----------------------------------------
Index 1: Key = 1_short_rains, Dataset Shape = (152, 6)
----------------------------------------
Index 2: Key = 3_long_rains, Dataset Shape = (324, 6)
----------------------------------------
Index 3: Key = 3_short_rains, Dataset Shape = (71, 6)
----------------------------------------

Clustering Long Rains with SPI1

  admin2_name  Drought Duration  Drought Severity  Drought Intensity  \
0     Garissa                 9         -1.219055          -0.135451   
1     Garissa                 9         -1.307802          -0.145311   
2     Garissa                 9         -1.092735          -0.121415   
3     Garissa                 9         -1.216412          -0.135157   
4     Garissa                 9         -1.021041          -0.113449   

  Drought Event_start Drought Event_end  start_year  end_year  
0          2000-06-01        2001-03-01        2000      2001  
1          2001-06-01        2002-03-01        2001      2002  
2          2003-06-01        2004-03-01        2003      2004  
3          2005-06-01        2006-03-01        2005      2006  
4          2007-06-01        2008-03-01        2007      2008  

  admin2_name  Drought Duration  Drought Severity  Drought Intensity  2000  \
0     Baringo               1.0         -1.366024          -1.331415     1   
1       Bomet               9.0         -1.279931          -0.359589     1   
2       Buret               1.0         -1.561892          -0.913720     1   
3     Garissa               9.0         -1.186511          -0.122609     1   
4       Ijara               1.0         -1.629814          -1.587531     1   

   2001  2002  2003  2004  2005  ...  2014  2015  2016  2017  2018  2019  \
0     0     0     0     0     0  ...     1     1     1     1     1     1   
1     1     1     1     1     1  ...     1     1     1     1     0     1   
2     1     1     1     1     1  ...     1     1     1     1     0     1   
3     1     1     1     1     1  ...     1     1     1     1     0     1   
4     0     1     1     1     1  ...     0     0     1     1     0     1   

   2020  2021  2022  2023  
0     0     1     1     0  
1     0     1     1     1  
2     0     0     0     0  
3     1     1     1     1  
4     0     1     1     0  

[5 rows x 28 columns]
/var/folders/bc/dhwpk1r91v955dmdrbsth4t40000gq/T/ipykernel_15841/3465632746.py:37: DeprecationWarning:

DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.

Data Preprocessing

Feature Vector Construction

To optimize the number of components (n_components) for Principal Component Analysis (PCA), we need to determine how many components are sufficient to retain the majority of the variance in the data. This can be done by plotting the explained variance ratio and observing when additional components contribute diminishing returns.

Cumulative explained variance: [0.46037374 0.58480704 0.67379662 0.7430995  0.79651795 0.83945763
 0.87528164 0.90337465 0.92785051 0.94706799 0.95992788 0.97199441
 0.98075397 0.98667251 0.99216845 0.99614478 0.99788941 0.99907973
 0.99981986 0.99993146 1.         1.         1.         1.
 1.         1.         1.        ]
Reduced Feature Set Shape: (32, 17)
Explained Variance by Optimal Components:
0.9978894094997736

K-Means Clustering

The Elbow Method is used to determine the optimal number of clusters (n_clusters) in K-Means clustering. It involves fitting the K-Means algorithm with different values for n_clusters and plotting the Within-Cluster Sum of Squares (WCSS) to observe the point where the WCSS starts to diminish at a reduced rate (forming an “elbow”).

WCSS for each number of clusters: [110.45030291643923, 67.48471443083798, 54.80950759633242, 43.66691067434408, 37.98443246002999, 34.56339788949877, 29.420856403384512, 25.449620400342397, 21.782099866044412, 19.47366697837728]
DataFrame with K-Means cluster assignments:
   Drought Duration  Drought Severity  Drought Intensity  2000  2001  2002  \
0               0.0          0.703587           0.183485   0.0   0.0   0.0   
1               1.0          0.845744           0.839927   0.0   1.0   1.0   
2               0.0          0.380168           0.465626   0.0   1.0   1.0   
3               1.0          1.000000           1.000000   0.0   1.0   1.0   
4               0.0          0.268015           0.010486   0.0   0.0   1.0   

   2003  2004  2005  2006  ...  2015  2016  2017  2018  2019  2020  2021  \
0   0.0   0.0   0.0   0.0  ...   1.0   1.0   1.0   1.0   1.0   0.0   1.0   
1   1.0   1.0   1.0   1.0  ...   1.0   1.0   1.0   0.0   1.0   0.0   1.0   
2   1.0   1.0   1.0   1.0  ...   1.0   1.0   1.0   0.0   1.0   0.0   0.0   
3   1.0   1.0   1.0   1.0  ...   1.0   1.0   1.0   0.0   1.0   1.0   1.0   
4   1.0   1.0   1.0   1.0  ...   0.0   1.0   1.0   0.0   1.0   0.0   1.0   

   2022  2023  cluster  
0   1.0   0.0        2  
1   1.0   1.0        1  
2   0.0   0.0        0  
3   1.0   1.0        1  
4   1.0   0.0        0  

[5 rows x 28 columns]

Hierarchical Clustering

Ward’s method minimizes variance and helps in identifying clusters of similar rainfall amounts. It provides a dendrogram, which can help determine different levels of clustering.

DataFrame with Hierarchical cluster assignments:
  admin2_name  hierarchical_cluster
0     Baringo                     3
1       Bomet                     2
2       Buret                     4
3     Garissa                     2
4       Ijara                     4

DBSCAN Clustering

No valid clusters were found with the given parameters.
Best eps: None, Best min_samples: None, Best Silhouette Score: None

Clustering Short Rains with SPI1

   admin2_name  Drought Duration  Drought Severity  Drought Intensity  \
30     Garissa                 1         -1.301349          -1.301349   
31     Garissa                 1         -1.282836          -1.282836   
32     Garissa                10         -1.698876          -0.169888   
33     Garissa                 1         -1.127246          -1.127246   
34     Garissa                10         -1.634406          -0.163441   

   Drought Event_start Drought Event_end  start_year  end_year  
30          2001-10-01        2001-11-01        2001      2001  
31          2005-10-01        2005-11-01        2005      2005  
32          2005-12-01        2006-10-01        2005      2006  
33          2008-10-01        2008-11-01        2008      2008  
34          2008-12-01        2009-10-01        2008      2009  

  admin2_name  Drought Duration  Drought Severity  Drought Intensity  2000  \
0     Garissa               1.0         -1.301349          -0.949615     0   
1       Ijara               1.0         -1.492268          -0.985315     0   
2      Isiolo               1.0         -1.502271          -0.920722     1   
3       Kitui               1.0         -1.340658          -1.182848     1   
4    Machakos               1.0         -1.252558          -0.821070     1   

   2001  2002  2003  2004  2005  ...  2014  2015  2016  2017  2018  2019  \
0     1     0     0     0     1  ...     1     0     1     1     1     0   
1     1     0     0     0     1  ...     1     0     1     1     1     0   
2     1     0     0     0     1  ...     0     0     1     1     1     0   
3     1     0     1     0     1  ...     1     0     1     1     0     0   
4     1     1     0     0     1  ...     0     0     1     1     1     0   

   2020  2021  2022  2023  
0     0     1     0     0  
1     1     1     1     1  
2     0     1     1     0  
3     1     1     1     0  
4     1     1     1     0  

[5 rows x 28 columns]
/var/folders/bc/dhwpk1r91v955dmdrbsth4t40000gq/T/ipykernel_15841/3465632746.py:37: DeprecationWarning:

DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.

Data Preprocessing

Feature Vector Construction

To optimize the number of components (n_components) for Principal Component Analysis (PCA), we need to determine how many components are sufficient to retain the majority of the variance in the data. This can be done by plotting the explained variance ratio and observing when additional components contribute diminishing returns.

Cumulative explained variance: [0.36002537 0.55221131 0.69508824 0.80006027 0.86769433 0.90868024
 0.93912303 0.96542439 0.98327584 0.99190756 1.         1.        ]
Reduced Feature Set Shape: (12, 11)
Explained Variance by Optimal Components:
1.0000000000000002

K-Means Clustering

The Elbow Method is used to determine the optimal number of clusters (n_clusters) in K-Means clustering. It involves fitting the K-Means algorithm with different values for n_clusters and plotting the Within-Cluster Sum of Squares (WCSS) to observe the point where the WCSS starts to diminish at a reduced rate (forming an “elbow”).

WCSS for each number of clusters: [45.028692613925955, 31.332452681549498, 23.49690516812442, 18.53740352244474, 14.217386716752863, 10.564804926874952, 7.775370098309036, 5.20393568962022, 3.1487585698997265, 2.019667017404588]
DataFrame with K-Means cluster assignments:
   Drought Duration  Drought Severity  Drought Intensity  2000  2001  2002  \
0               0.0          0.877981           0.267783   0.0   1.0   0.0   
1               0.0          0.501335           0.236059   0.0   1.0   0.0   
2               0.0          0.481602           0.293457   1.0   1.0   0.0   
3               0.0          0.800433           0.060530   1.0   1.0   0.0   
4               0.0          0.974235           0.382010   1.0   1.0   1.0   

   2003  2004  2005  2006  ...  2015  2016  2017  2018  2019  2020  2021  \
0   0.0   0.0   0.0   0.0  ...   0.0   1.0   0.0   1.0   0.0   0.0   1.0   
1   0.0   0.0   0.0   0.0  ...   0.0   1.0   0.0   1.0   0.0   1.0   1.0   
2   0.0   0.0   0.0   0.0  ...   0.0   1.0   0.0   1.0   0.0   0.0   1.0   
3   1.0   0.0   0.0   0.0  ...   0.0   1.0   0.0   0.0   0.0   1.0   1.0   
4   0.0   0.0   0.0   0.0  ...   0.0   1.0   0.0   1.0   0.0   1.0   1.0   

   2022  2023  cluster  
0   0.0   0.0        0  
1   1.0   1.0        0  
2   1.0   0.0        3  
3   1.0   0.0        2  
4   1.0   0.0        3  

[5 rows x 28 columns]

Hierarchical Clustering

Ward’s method minimizes variance and helps in identifying clusters of similar rainfall amounts. It provides a dendrogram, which can help determine different levels of clustering.

DataFrame with Hierarchical cluster assignments:
  admin2_name  hierarchical_cluster
0     Garissa                     4
1       Ijara                     4
2      Isiolo                     3
3       Kitui                     2
4    Machakos                     3

DBSCAN Clustering

No valid clusters were found with the given parameters.
Best eps: None, Best min_samples: None, Best Silhouette Score: None

Clustering Long Rains with SPI3

   admin2_name  Drought Duration  Drought Severity  Drought Intensity  \
18     Garissa                 1         -1.343592          -1.343592   
19     Garissa                 1         -1.530327          -1.530327   
20     Garissa                 1         -1.271111          -1.271111   
21     Garissa                 1         -1.762565          -1.762565   
22     Garissa                 1         -1.072521          -1.072521   

   Drought Event_start Drought Event_end  start_year  end_year  
18          2001-03-01        2001-04-01        2001      2001  
19          2002-03-01        2002-04-01        2002      2002  
20          2003-03-01        2003-04-01        2003      2003  
21          2005-03-01        2005-04-01        2005      2005  
22          2006-03-01        2006-04-01        2006      2006  

  admin2_name  Drought Duration  Drought Severity  Drought Intensity  2000  \
0     Baringo              10.5         -4.083265          -0.704244     1   
1       Bomet               1.5         -1.946644          -0.927498     1   
2       Buret               1.0         -1.450826          -1.142180     1   
3     Garissa               1.0         -1.507435          -1.504089     0   
4       Ijara               1.0         -1.314247          -1.458273     0   

   2001  2002  2003  2004  2005  ...  2014  2015  2016  2017  2018  2019  \
0     1     0     0     0     0  ...     1     1     0     1     1     1   
1     1     0     1     0     1  ...     1     1     0     1     0     0   
2     1     0     1     0     1  ...     1     1     0     1     0     0   
3     1     1     1     0     1  ...     0     0     1     1     0     0   
4     1     0     1     1     0  ...     1     0     1     1     0     0   

   2020  2021  2022  2023  
0     0     0     1     0  
1     0     0     0     0  
2     0     0     0     0  
3     0     1     1     0  
4     0     1     1     0  

[5 rows x 28 columns]
/var/folders/bc/dhwpk1r91v955dmdrbsth4t40000gq/T/ipykernel_15841/3465632746.py:37: DeprecationWarning:

DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.

Data Preprocessing

Feature Vector Construction

To optimize the number of components (n_components) for Principal Component Analysis (PCA), we need to determine how many components are sufficient to retain the majority of the variance in the data. This can be done by plotting the explained variance ratio and observing when additional components contribute diminishing returns.

Cumulative explained variance: [0.27676394 0.40308483 0.5109299  0.59323422 0.66778086 0.72839388
 0.77595821 0.81749797 0.85331138 0.88237169 0.90736034 0.92775282
 0.94488809 0.9574453  0.96871365 0.97843505 0.984071   0.98932284
 0.99312299 0.99655941 0.99819703 0.99963746 0.99994281 1.
 1.         1.         1.        ]
Reduced Feature Set Shape: (32, 20)
Explained Variance by Optimal Components:
0.996559409903961

K-Means Clustering

The Elbow Method is used to determine the optimal number of clusters (n_clusters) in K-Means clustering. It involves fitting the K-Means algorithm with different values for n_clusters and plotting the Within-Cluster Sum of Squares (WCSS) to observe the point where the WCSS starts to diminish at a reduced rate (forming an “elbow”).

WCSS for each number of clusters: [122.11183331190155, 94.71651189448778, 83.31833205317044, 74.10340173749428, 66.64402528983659, 60.87334446727806, 55.999324974707434, 48.740947495483795, 45.414771025977586, 40.967379341691455]
DataFrame with K-Means cluster assignments:
   Drought Duration  Drought Severity  Drought Intensity  2000  2001  2002  \
0          1.000000          0.000000           0.886076   1.0   1.0   0.0   
1          0.052632          0.766988           0.670574   1.0   1.0   0.0   
2          0.000000          0.944973           0.463348   1.0   1.0   0.0   
3          0.000000          0.924652           0.114007   0.0   1.0   1.0   
4          0.000000          0.994001           0.158232   0.0   1.0   0.0   

   2003  2004  2005  2006  ...  2015  2016  2017  2018  2019  2020  2021  \
0   0.0   0.0   0.0   0.0  ...   1.0   0.0   1.0   1.0   1.0   0.0   0.0   
1   1.0   0.0   1.0   0.0  ...   1.0   0.0   1.0   0.0   0.0   0.0   0.0   
2   1.0   0.0   1.0   0.0  ...   1.0   0.0   1.0   0.0   0.0   0.0   0.0   
3   1.0   0.0   1.0   1.0  ...   0.0   1.0   1.0   0.0   0.0   0.0   1.0   
4   1.0   1.0   0.0   0.0  ...   0.0   1.0   1.0   0.0   0.0   0.0   1.0   

   2022  2023  cluster  
0   1.0   0.0        4  
1   0.0   0.0        3  
2   0.0   0.0        3  
3   1.0   0.0        0  
4   1.0   0.0        0  

[5 rows x 28 columns]

Hierarchical Clustering

Ward’s method minimizes variance and helps in identifying clusters of similar rainfall amounts. It provides a dendrogram, which can help determine different levels of clustering.

DataFrame with Hierarchical cluster assignments:
  admin2_name  hierarchical_cluster
0     Baringo                     1
1       Bomet                     3
2       Buret                     3
3     Garissa                     4
4       Ijara                     4

DBSCAN Clustering

No valid clusters were found with the given parameters.
Best eps: None, Best min_samples: None, Best Silhouette Score: None

Clustering Short Rains with SPI3

   admin2_name  Drought Duration  Drought Severity  Drought Intensity  \
41     Garissa                11         -2.453734          -0.223067   
42     Garissa                12         -4.159092          -0.346591   
43     Garissa                 1         -1.000477          -1.000477   
44     Garissa                10         -1.411795          -0.141180   
45     Garissa                11         -2.085489          -0.189590   

   Drought Event_start Drought Event_end  start_year  end_year  
41          2000-12-01        2001-11-01        2000      2001  
42          2005-11-01        2006-11-01        2005      2006  
43          2010-10-01        2010-11-01        2010      2010  
44          2010-12-01        2011-10-01        2010      2011  
45          2013-12-01        2014-11-01        2013      2014  

  admin2_name  Drought Duration  Drought Severity  Drought Intensity  2000  \
0     Garissa              11.0         -2.085489          -0.336498     1   
1       Ijara              10.0         -1.366291          -0.200130     0   
2      Isiolo               2.0         -2.506750          -1.040627     0   
3       Kitui              10.0         -1.210205          -0.262954     0   
4    Machakos               6.0         -1.873798          -0.703089     0   

   2001  2002  2003  2004  2005  ...  2014  2015  2016  2017  2018  2019  \
0     1     0     0     0     1  ...     1     0     0     0     1     0   
1     1     0     0     0     1  ...     0     0     1     1     1     0   
2     0     0     0     0     1  ...     0     0     0     0     1     0   
3     0     0     1     1     1  ...     0     0     0     1     1     0   
4     0     0     0     0     1  ...     0     0     0     0     1     0   

   2020  2021  2022  2023  
0     1     1     0     0  
1     1     1     1     1  
2     0     1     0     0  
3     0     0     0     0  
4     0     0     0     0  

[5 rows x 28 columns]
/var/folders/bc/dhwpk1r91v955dmdrbsth4t40000gq/T/ipykernel_15841/3465632746.py:37: DeprecationWarning:

DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.

Data Preprocessing

Feature Vector Construction

To optimize the number of components (n_components) for Principal Component Analysis (PCA), we need to determine how many components are sufficient to retain the majority of the variance in the data. This can be done by plotting the explained variance ratio and observing when additional components contribute diminishing returns.

Cumulative explained variance: [0.39493054 0.58656477 0.74651325 0.81552005 0.86320927 0.90766758
 0.93956934 0.96797682 0.98719303 0.99624797 1.         1.        ]
Reduced Feature Set Shape: (12, 10)
Explained Variance by Optimal Components:
0.9962479712669272

K-Means Clustering

The Elbow Method is used to determine the optimal number of clusters (n_clusters) in K-Means clustering. It involves fitting the K-Means algorithm with different values for n_clusters and plotting the Within-Cluster Sum of Squares (WCSS) to observe the point where the WCSS starts to diminish at a reduced rate (forming an “elbow”).

WCSS for each number of clusters: [44.04810503247088, 28.825112926750784, 20.002802471241473, 13.732123674027878, 10.974623458142668, 8.329476842682892, 5.771042633900718, 4.2769506829483985, 2.5276680462932317, 1.0436820711625514]
DataFrame with K-Means cluster assignments:
   Drought Duration  Drought Severity  Drought Intensity  2000  2001  2002  \
0               1.0          0.632797           0.837754   1.0   1.0   0.0   
1               0.9          0.919376           1.000000   0.0   1.0   0.0   
2               0.1          0.464937           0.000000   0.0   0.0   0.0   
3               0.9          0.981571           0.925254   0.0   0.0   0.0   
4               0.5          0.717149           0.401594   0.0   0.0   0.0   

   2003  2004  2005  2006  ...  2015  2016  2017  2018  2019  2020  2021  \
0   0.0   0.0   0.0   0.0  ...   0.0   0.0   0.0   1.0   0.0   1.0   1.0   
1   0.0   0.0   0.0   0.0  ...   0.0   1.0   1.0   1.0   0.0   1.0   1.0   
2   0.0   0.0   0.0   0.0  ...   0.0   0.0   0.0   1.0   0.0   0.0   1.0   
3   1.0   1.0   0.0   0.0  ...   0.0   0.0   1.0   1.0   0.0   0.0   0.0   
4   0.0   0.0   0.0   0.0  ...   0.0   0.0   0.0   1.0   0.0   0.0   0.0   

   2022  2023  cluster  
0   0.0   0.0        3  
1   1.0   1.0        0  
2   0.0   0.0        1  
3   0.0   0.0        2  
4   0.0   0.0        2  

[5 rows x 28 columns]

Hierarchical Clustering

Ward’s method minimizes variance and helps in identifying clusters of similar rainfall amounts. It provides a dendrogram, which can help determine different levels of clustering.

DataFrame with Hierarchical cluster assignments:
  admin2_name  hierarchical_cluster
0     Garissa                     2
1       Ijara                     4
2      Isiolo                     3
3       Kitui                     1
4    Machakos                     1

DBSCAN Clustering

No valid clusters were found with the given parameters.
Best eps: None, Best min_samples: None, Best Silhouette Score: None

EM-DAT Analysis

Requirement already satisfied: openpyxl in /Users/magima/anaconda3/envs/gee/lib/python3.11/site-packages (3.1.5)
Requirement already satisfied: et-xmlfile in /Users/magima/anaconda3/envs/gee/lib/python3.11/site-packages (from openpyxl) (1.1.0)
Note: you may need to restart the kernel to use updated packages.
DisNo. Historic Origin Associated Types Start Year Start Month End Year End Month Total Deaths No. Affected Total Affected CPI Admin_Unit_adm1_name Admin_Unit_adm2_name Location
0 2004-9288-KEN No Poor rainfall Crop failure|Food shortage 2004.0 7.0 2004.0 NaN 80.0 2300000.0 2300000.0 61.989586 NaN NaN Kitui
1 2005-9719-KEN No Lack of rains Famine 2005.0 12.0 2006.0 NaN 27.0 3500000.0 3500000.0 64.092740 NaN NaN Makueni
2 2019-9145-KEN No Two consecutive poor rainy seasons Food shortage 2019.0 1.0 2019.0 9.0 NaN 2600000.0 2600000.0 83.904170 Eastern Kitui West Pokot
3 2020-9609-KEN No Lack of rains Food shortage 2020.0 12.0 2022.0 12.0 NaN 4500000.0 4500000.0 84.939198 Eastern Marsabit Marsabit
4 2004-9288-KEN No Poor rainfall Crop failure|Food shortage 2004.0 7.0 2004.0 NaN 80.0 2300000.0 2300000.0 61.989586 NaN NaN Mbeere
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
60 2005-9719-KEN No Lack of rains Famine 2005.0 12.0 2006.0 NaN 27.0 3500000.0 3500000.0 64.092740 NaN NaN Kajiado
61 2004-9288-KEN No Poor rainfall Crop failure|Food shortage 2004.0 7.0 2004.0 NaN 80.0 2300000.0 2300000.0 61.989586 NaN NaN Central provinces
62 2005-9719-KEN No Lack of rains Famine 2005.0 12.0 2006.0 NaN 27.0 3500000.0 3500000.0 64.092740 NaN NaN Turkana
63 2005-9719-KEN No Lack of rains Famine 2005.0 12.0 2006.0 NaN 27.0 3500000.0 3500000.0 64.092740 NaN NaN Samburu
64 2005-9719-KEN No Lack of rains Famine 2005.0 12.0 2006.0 NaN 27.0 3500000.0 3500000.0 64.092740 NaN NaN Laikipia districts (Rift Valley province).

65 rows × 15 columns

Start Year End Year
0 2004.0 2004.0
1 2005.0 2006.0
2 2019.0 2019.0
3 2020.0 2022.0