<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Getting Started
The purpose of the present Getting Started section is to give a quick overview
of the main objects and features of the LArray library.
To get a more detailed presentation of all capabilities of LArray, read the next sections of the tutorial.
The API Reference section of the documentation give you the list of all objects, methods and functions with their individual documentation and examples.
To use the LArray library, the first thing to do is to import it
Step1:  To know the version of the LArray library installed on your machine, type
Step2:  <div class="alert alert-warning">
**Note
Step3:  Create an array
Working with the LArray library mainly consists of manipulating Array data structures.
They represent N-dimensional labelled arrays and are composed of raw data (NumPy ndarray), axes and optionally some metadata.
An Axis object represents a dimension of an array. It contains a list of labels and has a name
Step4:  The labels allow to select subsets and to manipulate the data without working with the positions
of array elements directly.
To create an array from scratch, you need to supply data and axes
Step5:  You can optionally attach some metadata to an array
Step6:  To get a short summary of an array, type
Step7:  Create an array filled with predefined values
Arrays filled with predefined values can be generated through dedicated functions
Step8:  Save/Load an array
The LArray library offers many I/O functions to read and write arrays in various formats
(CSV, Excel, HDF5). For example, to save an array in a CSV file, call the method to_csv
Step9:  The content of the CSV file is then
Step10:  Other input/output functions are described in the Input/Output section of the API documentation.
Selecting a subset
To select an element or a subset of an array, use brackets [ ].
In Python we usually use the term indexing for this operation.
Let us start by selecting a single element
Step11:  Labels can be given in arbitrary order
Step12:  When selecting a larger subset the result is an array
Step13:  When selecting several labels for the same axis, they must be given as a list (enclosed by [ ])
Step14:  You can also select slices, which are all labels between two bounds (we usually call them the start and stop
bounds). Specifying the start and stop bounds of a slice is optional
Step15:  <div class="alert alert-info">
   Note
Step16:  If we try to get the number of Belgians living in the Netherlands for the year 2017, we might try something like
Step17:  Iterating over an axis
To iterate over an axis, use the following syntax
Step18:  Aggregation
The LArray library includes many aggregations methods
Step19:  We can sum along the 'gender' axis using
Step20:  Or sum along both 'age' and 'gender'
Step21:  It is sometimes more convenient to aggregate along all axes except some. In that case, use the aggregation
methods ending with _by. For example
Step22:  Groups
A Group object represents a subset of labels or positions of an axis
Step23:  It is often useful to attach them an explicit name using the &gt;&gt; operator
Step24:  Still using the same population array
Step25:  Groups can be used in selections
Step26:  or aggregations
Step27:  When aggregating several groups, the names we set above using &gt;&gt; determines the label on the aggregated axis.
Since we did not give a name for the children group, the resulting label is generated automatically 
Step28:  Grouping arrays in a Session
Variables (arrays) may be grouped in Session objects.
A session is an ordered dict-like container with special I/O methods
Step29:  One of the main interests of using sessions is to save and load many arrays at once
<ASSISTANT_TASK:>
Python Code:
%xmode Minimal
from larray import *
Explanation: Getting Started
The purpose of the present Getting Started section is to give a quick overview
of the main objects and features of the LArray library.
To get a more detailed presentation of all capabilities of LArray, read the next sections of the tutorial.
The API Reference section of the documentation give you the list of all objects, methods and functions with their individual documentation and examples.
To use the LArray library, the first thing to do is to import it:
End of explanation
from larray import __version__
__version__
Explanation: To know the version of the LArray library installed on your machine, type:
End of explanation
s = 1 + 2
# In the interactive mode, there is no need to use the print() function 
# to display the content of the variable 's'.
# Simply typing 's' is enough
s
# In the interactive mode, there is no need to use the print() function 
# to display the result of an expression
1 + 2
Explanation: <div class="alert alert-warning">
**Note:** 
The tutorial is generated from Jupyter notebooks which work in the "interactive" mode (like in the LArray Editor console). 
In the interactive mode, there is no need to use the print() function to display the content of a variable.
Simply writing its name is enough.
The same remark applies for the returned value of an expression.<br><br>
In a Python script (file with .py extension), you always need to use the print() function to display the content of 
a variable or the value returned by a function or an expression.
</div>
End of explanation
# define some axes to be used later
age = Axis('age=0-9,10-17,18-66,67+')
gender = Axis('gender=female,male')
time = Axis('time=2015..2017')
Explanation: Create an array
Working with the LArray library mainly consists of manipulating Array data structures.
They represent N-dimensional labelled arrays and are composed of raw data (NumPy ndarray), axes and optionally some metadata.
An Axis object represents a dimension of an array. It contains a list of labels and has a name:
End of explanation
# define some data. This is the belgian population (in thousands). Source: eurostat.
data = [[[633, 635, 634],
         [663, 665, 664]],
        [[484, 486, 491],
         [505, 511, 516]],
        [[3572, 3581, 3583],
         [3600, 3618, 3616]],
        [[1023, 1038, 1053],
         [756, 775, 793]]]
# create an Array object
population = Array(data, axes=[age, gender, time])
population
Explanation: The labels allow to select subsets and to manipulate the data without working with the positions
of array elements directly.
To create an array from scratch, you need to supply data and axes:
End of explanation
# attach some metadata to the population array
population.meta.title = 'population by age, gender and year'
population.meta.source = 'Eurostat'
# display metadata
population.meta
Explanation: You can optionally attach some metadata to an array:
End of explanation
# Array summary: metadata + dimensions + description of axes
population.info
Explanation: To get a short summary of an array, type:
End of explanation
zeros([age, gender])
ones([age, gender])
full([age, gender], fill_value=10.0)
sequence(age)
ndtest([age, gender])
Explanation: Create an array filled with predefined values
Arrays filled with predefined values can be generated through dedicated functions:
zeros : creates an array filled with 0
ones : creates an array filled with 1
full : creates an array filled with a given value
sequence : creates an array by sequentially applying modifications to the array along axis.
ndtest : creates a test array with increasing numbers as data
End of explanation
# save our population array to a CSV file
population.to_csv('population_belgium.csv')
Explanation: Save/Load an array
The LArray library offers many I/O functions to read and write arrays in various formats
(CSV, Excel, HDF5). For example, to save an array in a CSV file, call the method to_csv:
End of explanation
population = read_csv('population_belgium.csv')
population
Explanation: The content of the CSV file is then:
age,gender\time,2015,2016,2017
0-9,female,633,635,634
0-9,male,663,665,664
10-17,female,484,486,491
10-17,male,505,511,516
18-66,female,3572,3581,3583
18-66,male,3600,3618,3616
67+,female,1023,1038,1053
67+,male,756,775,793
<div class="alert alert-info">
   Note: In CSV or Excel files, the last dimension is horizontal and the names of the
   last two dimensions are separated by a backslash \.
</div>
To load a saved array, call the function read_csv:
End of explanation
population['67+', 'female', 2017]
Explanation: Other input/output functions are described in the Input/Output section of the API documentation.
Selecting a subset
To select an element or a subset of an array, use brackets [ ].
In Python we usually use the term indexing for this operation.
Let us start by selecting a single element:
End of explanation
population[2017, 'female', '67+']
Explanation: Labels can be given in arbitrary order:
End of explanation
population['female']
Explanation: When selecting a larger subset the result is an array:
End of explanation
population['female', ['0-9', '10-17']]
Explanation: When selecting several labels for the same axis, they must be given as a list (enclosed by [ ])
End of explanation
# in this case '10-17':'67+' is equivalent to ['10-17', '18-66', '67+']
population['female', '10-17':'67+']
# :'18-66' selects all labels between the first one and '18-66'
# 2017: selects all labels between 2017 and the last one
population[:'18-66', 2017:]
Explanation: You can also select slices, which are all labels between two bounds (we usually call them the start and stop
bounds). Specifying the start and stop bounds of a slice is optional: when not given, start is the first label
of the corresponding axis, stop the last one:
End of explanation
country = Axis(['Belgium', 'Netherlands', 'Germany'], 'country')
citizenship = Axis(['Belgium', 'Netherlands', 'Germany'], 'citizenship')
immigration = ndtest((country, citizenship, time))
immigration
Explanation: <div class="alert alert-info">
   Note: Contrary to slices on normal Python lists, the stop bound is included in the selection.
</div>
<div class="alert alert-warning">
    Selecting by labels as above only works as long as there is no ambiguity.
    When several axes have some labels in common and you do not specify explicitly
    on which axis to work, it fails with an error ending with something like
    ValueError: <somelabel> is ambiguous (valid in <axis1>, <axis2>).
</div>
For example, imagine you need to work with an 'immigration' array containing two axes sharing some common labels:
End of explanation
immigration[country['Netherlands'], citizenship['Belgium'], 2017]
Explanation: If we try to get the number of Belgians living in the Netherlands for the year 2017, we might try something like:
python
    immigration['Netherlands', 'Belgium', 2017]
... but we receive back a volley of insults:
[some long error message ending with the line below]
    [...]
    ValueError: Netherlands is ambiguous (valid in country, citizenship)
In that case, we have to specify explicitly which axes the 'Netherlands' and 'Belgium' labels we want to select belong to:
End of explanation
for year in time:
    print(year)
Explanation: Iterating over an axis
To iterate over an axis, use the following syntax:
End of explanation
population
Explanation: Aggregation
The LArray library includes many aggregations methods: sum, mean, min, max, std, var, ...
For example, assuming we still have an array in the population variable:
End of explanation
population.sum(gender)
Explanation: We can sum along the 'gender' axis using:
End of explanation
population.sum(age, gender)
Explanation: Or sum along both 'age' and 'gender':
End of explanation
population.sum_by(time)
Explanation: It is sometimes more convenient to aggregate along all axes except some. In that case, use the aggregation
methods ending with _by. For example:
End of explanation
children = age['0-9', '10-17']
children
Explanation: Groups
A Group object represents a subset of labels or positions of an axis:
End of explanation
working = age['18-66'] >> 'working'
working
nonworking = age['0-9', '10-17', '67+'] >> 'nonworking'
nonworking
Explanation: It is often useful to attach them an explicit name using the &gt;&gt; operator:
End of explanation
population
Explanation: Still using the same population array:
End of explanation
population[working]
population[nonworking]
Explanation: Groups can be used in selections:
End of explanation
population.sum(nonworking)
Explanation: or aggregations:
End of explanation
population.sum((children, working, nonworking))
Explanation: When aggregating several groups, the names we set above using &gt;&gt; determines the label on the aggregated axis.
Since we did not give a name for the children group, the resulting label is generated automatically :
End of explanation
population = zeros([age, gender, time])
births = zeros([age, gender, time])
deaths = zeros([age, gender, time])
# create a session containing the arrays of the model
demography_session = Session(population=population, births=births, deaths=deaths)
# get an array (option 1)
demography_session['population']
# get an array (option 2)
demography_session.births
# modify an array
demography_session.deaths['male'] = 1
# add an array
demography_session.foreigners = zeros([age, gender, time])
# displays names of arrays contained in the session
# (in alphabetical order)
demography_session.names
Explanation: Grouping arrays in a Session
Variables (arrays) may be grouped in Session objects.
A session is an ordered dict-like container with special I/O methods:
End of explanation
# dump all arrays contained in demography_session in one HDF5 file
demography_session.save('demography.h5')
# load all arrays saved in the HDF5 file 'demography.h5' and store them in the 'demography_session' variable
demography_session = Session('demography.h5')
Explanation: One of the main interests of using sessions is to save and load many arrays at once:
End of explanation
<END_TASK>