Table of Contents
In order to run the code examples in this repository, you need to set up a Python environment with all the necessary libraries installed. Below are the requirements and installation instructions for setting up the environment.
- Python 3.9 or later
- Jupyter Notebook - All lecture notes and code examples are provided as Jupyter Notebooks.
- Common Python libraries for data analysis (e.g.,
notebook
,pandas
,numpy
,matplotlib
,seaborn
,scikit-learn
, etc.)
The easiest way to set up a Python environment is to install the Anaconda Distribution, which comes with all common libraries and tools for data science and machine learning and the Python executable. This should be sufficient for most of the code examples in this repository. Please make sure to choose the correct version for your operating system.
If you prefer to set up a Python environment from scratch, you can install Python and the necessary libraries using the pip
package manager. Below are the steps.
-
Install Python 3.9 or later from the official Python website.
- You can also use
pyenv
to manage multiple Python versions on your machine https://github.com/pyenv/pyenv
- You can also use
-
To install the current version of the required libraries, run the following command in your terminal:
pip install -r https://raw.githubusercontent.com/xiangshiyin/machine-learning-for-actuarial-science/refs/heads/main/requirements.txt
-
To start the Jupyter Notebook server, run the following command in your terminal:
jupyter notebook
-
Open the Jupyter Notebook in your browser and navigate to the desired notebook file to run the code examples.
-
When you are done, deactivate the virtual environment:
exit
If you prefer to set up a Python environment from scratch, you can install Python and the necessary libraries using the Poetry
package manager. Below are the steps to set up the environment using Poetry
.
-
Install Python 3.9 or later from the official Python website.
- You can also use
pyenv
to manage multiple Python versions on your machine https://github.com/pyenv/pyenv
- You can also use
-
Install
Poetry
by following the instructions on the official Poetry website. -
Clone this repository to your local machine.
-
Navigate to the root directory of the repository and run the following command to install the required dependencies:
# run the following command if you want to create a virtual environment in the project directory poetry config virtualenvs.in-project true # run the following command to install the dependencies poetry install
-
Activate the virtual environment created by
Poetry
:poetry shell
-
Start the Jupyter Notebook server:
jupyter notebook
-
Open the Jupyter Notebook in your browser and navigate to the desired notebook file to run the code examples.
-
When you are done, deactivate the virtual environment:
exit
-
You can also run individual notebooks without activating the virtual environment by running the following command:
poetry run jupyter notebook
The first time you run the Docker container, it will download the image from the Docker Hub, which may take some time depending on your internet connection speed. Subsequent runs will be faster as the image will be cached on your machine.
- Install Docker Desktop from the official Docker website.
- Clone this repository to your local machine.
- Navigate to the root directory of the repository and run the following command
docker run -it --rm -p 8888:8888 -v "${PWD}":/home/jovyan/work jupyter/scipy-notebook
- Open the Jupyter Notebook in your browser by following the instructions in the terminal.
- When you are done, stop the Docker container by pressing
Ctrl+C
in the terminal. - You can also run the Docker container in the background by running the following command:
docker run -d --rm -p 8888:8888 -v "${PWD}":/home/jovyan/work jupyter/scipy-notebook
- To stop the Docker container running in the background, run the following command:
docker ps docker stop <CONTAINER_ID>
- Replace
<CONTAINER_ID>
with the actual container ID from the output of thedocker ps
command.
- Replace
In addition to Jupyter Notebook, you may also want to set up an Integrated Development Environment (IDE) for writing and running Python code, such as Visual Studio Code. In fact, you could use Jupyter Notebook within Visual Studio Code by installing the Jupyter extension (check here for more details).
You can also open and run the Jupyter Notebooks in this repository directly in your browser using Google Colab by clicking the button at the top of each notebook.
- For general information on Google Colab, check https://colab.research.google.com/