Import the LArray library:
from larray import *
arr = ndtest((3, 3))
arr
One can do all usual arithmetic operations on an array, it will apply the operation to all elements individually
# addition
arr + 10
# multiplication
arr * 2
# 'true' division
arr / 2
# 'floor' division
arr // 2
# % means modulo (aka remainder of division)
arr % 5
# ** means raising to the power
arr ** 3
More interestingly, binary operators as above also works between two arrays:
# load the 'demography_eurostat' dataset
demo_eurostat = load_example_data('demography_eurostat')
# extract the 'pop' array
pop = demo_eurostat.pop
pop
aggregation_matrix = Array([[1, 0, 0], [0, 1, 1]], axes=(Axis('country=Belgium,France+Germany'), pop.country))
aggregation_matrix
# @ means matrix product
aggregation_matrix @ pop['Male']
aggregation_matrix = Array([[1, 0, 0], [0, 0.5, 0.5]], axes=(Axis('country=Belgium,France+Germany/2'), pop.country))
aggregation_matrix
aggregation_matrix @ pop['Male']
# force the resulting matrix to be an integer matrix
(aggregation_matrix @ pop['Male']).astype(int)
You can do operations between arrays having different axes order. The axis order of the result is the same as the left array
# extract the 'births' array
births = demo_eurostat.births
# let's change the order of axes of the 'births' array
births_transposed = births.transpose()
births_transposed
# LArray doesn't care of axes order when performing
# arithmetic operations between arrays
pop + births_transposed
Arithmetic operations between two arrays only works when they have compatible axes (i.e. same labels).
# the 'pop' and 'births' have compatible axes
pop + births
# Now, let's replace the country names by the country codes
births_codes = births.set_labels('country', ['BE', 'FR', 'DE'])
births_codes
# arithmetic operations between arrays
# having incompatible axes raise an error
try:
pop + births_codes
except Exception as e:
print(type(e).__name__, e)
# use the .ignore_labels() method on axis 'country'
# to avoid the incompatible axes error (risky)
pop + births_codes.ignore_labels('country')
The condition that axes must be compatible only applies on common axes. Arithmetic operations between two arrays can be performed even if the second array has extra or missing axes compared to the first one:
# let's define a 'multiplicator' vector with
# one value defined for each gender
multiplicator = Array([-1, 1], axes=pop.gender)
multiplicator
# the multiplication below has been propagated to the
# 'country' and 'time' axes.
# This behavior is called broadcasting
pop * multiplicator
Python comparison operators are:
Operator | Meaning |
---|---|
== |
equal |
!= |
not equal |
> |
greater than |
>= |
greater than or equal |
< |
less than |
<= |
less than or equal |
Applying a comparison operator on an array returns a boolean array:
# test which values are greater than 10 millions
pop > 10e6
Comparison operations can be combined using Python bitwise operators:
Operator | Meaning |
---|---|
& | and |
| | or |
~ | not |
# test which values are greater than 10 millions and less than 40 millions
(pop > 10e6) & (pop < 40e6)
# test which values are less than 10 millions or greater than 40 millions
(pop < 10e6) | (pop > 40e6)
# test which values are not less than 10 millions
~(pop < 10e6)
The returned boolean array can then be used in selections and assignments:
pop_copy = pop.copy()
# set all values greater than 40 millions to 40 millions
pop_copy[pop_copy > 40e6] = 40e6
pop_copy
Boolean operations can be made between arrays:
# test where the two arrays have the same values
pop == pop_copy
To test if all values between are equals, use the equals method:
pop.equals(pop_copy)
The LArray library provides many aggregation functions. The list is given in the Aggregation Functions subsection of the API Reference page.
Aggregation operations can be performed on axes or groups. Axes and groups can be mixed.
The main rules are:
,
Calculate the sum along an axis:
pop.sum('gender')
or several axes (axes are separated by commas ,
):
pop.sum('country', 'gender')
Calculate the sum along all axes except one by appending _by
to the aggregation function:
pop.sum_by('time')
Calculate the sum along groups (the groups belonging to the same axis must grouped inside parentheses ()):
even_years = pop.time[2014::2] >> 'even_years'
odd_years = pop.time[2013::2] >> 'odd_years'
pop.sum((odd_years, even_years))
Mixing axes and groups in aggregations:
pop.sum('gender', (odd_years, even_years))