sw_diversity_index.sw_diversity_index

sw_diversity_index.sw_diversity_index(arr)

Calculate the Shannon Wiener diversity index (H’) for a community.

The Shannon Wiener index quantifies species diversity by accounting for both species richness and evenness. It is defined as:

H' = -SUM(p_i * ln(p_i))

where p_i is the proportion of individuals belonging to species i.

Parameters

Name Type Description Default
arr array - like A one-dimensional array of non-negative values representing species counts. required

Returns

Name Type Description
float The Shannon Wiener diversity index. Returns 0.0 if only one species is present or if the input is empty.

Notes

  • Zero counts are ignored in the calculation.
  • The natural log (ln) is used.
  • Higher values indicate greater diversity.

Examples

>>> sw_diversity_index([50, 30, 20])
1.0297
>>> sw_diversity_index([1])
0.0