Python on Anaconda
Anaconda automatically selects the version of a module corresponding to the version of Python. It is easy to install Anaconda. In the following, how to install Anaconda is illustrated for Windows 10 and Ubuntu/CentOS
Simple examples of Stan scripts are presented in this website.
For PyMC3 examples, they are shown in this website.
Visit the website of Anaconda
https://www.anaconda.com/products/individual
Then select the anaconda for Windows, and install it.
The two checks on the check boxes (Figure 1) are recommended to be off.
Figure 1
After the installation, select gAnaconda Powershell Prompth when you uses the terminal, or select gJupyter Notebookh when you use Notebook (Figure 2).
Figure 2
When you use a module, such as pystan, install it by conda command in the virtual environment. Each module has to be installed on each Python virtual environment.
When you select gAnaconda Powershell Promth in Figure 2, you start in the virtual environment being active. When you are in a virtual environment, the name of the virtual environment enclosed by a pair of ( and ) , e.g.(base), are displayed in the head of the command line. To make the virtual environment not active, run the command
conda
deactivate
To make the virtual environment active again, run the command
conda
activate
To install modules, run the conda command in the state of the virtual environment on which the module is to be installed being active, for example,
conda install
numpy matplotlib
By this, you can use the modules numpy and matplotlib in the virtual environment on which the modules are installed.
At present (2023.01), PyMC3 can be installed by the following command
conda install
pymc3
But, when you use pymc3, you must run the script on Jupyter Notebook.
At now (2023.01.04), in Windows, successful combination of PyStan and Python is limited. I use PyStan 2.17.1.0 in Python 3.7 virtual environment of Anaconda in Windows 10.
PyStan can be installed in Python 3.7 virtual environment by the code
conda install
pystan==2.17.1.0
You should be cautious that in sampling by
Stan, the parameter n_jobs
of the sampling method should be 1 in the case of
Windows. That is,
n_jobs = 1
A Python 3.7 virtual environment can be created by the code
conda create –n
py37 python=3.7
To activate the created environment, run the code
conda activate
py37
To escape from the activated py37 environment, run the code
conda deactivate
When a virtual environment is active, you can use a software for Windows, e.g. Notepad, as usual. If you have installed Python from https://www.python.org/, you can use the editor of IDLE of Python.
Tips on installing Python in Windows are presented in this website.
Now suppose that you have developed the script file as shown in Figure 3.
Figure 3
Run Anaconda Powershell Prompt (Figure 2), then the terminal window starts on your home directory (Figure 4). Change the directory to that, where the script file is saved. In the case of Figure 3, change the current directory to .\samples.
Figure 4
Call python command as follows
python
sample1.py
In general, to run a script file, call python command as follows
python
script-filename
When you choose gJupyter Notebookh in Figure 2, Jupyter Notebook starts. The top folder which you can open in this case is the home folder in drive C. When you access other folders than the home one, select gAnaconda Powershell Promth. In the terminal of the Powershell, move to the desired folder using command cd, make the virtual environment active if necessary, then execute jupyter command like this
jupyter
notebook
When Jupyter Notebook starts, the Web Browser starts. When Jupyter Notebook ends and the browser is closed, the terminal window shows the program is still running. To stop the program, press down gCtrl + Ch twice, i.e., press C key twice with Ctrl key being pressed. Then the program quits.
When you use PyMC3, it should be run in Jupyter Notebook in the case of Windows 10.
Anaconda can be installed by down loading the file for Linux from the Website
https://www.anaconda.com/products/individual
The down loaded file (name of the file depends on the time) can be installed by running the following code in the folder in which the file is downloaded.
sh
./Anaconda3-2018.12-Linux-x86_64.sh
When you are asked in installing like this
Do you wish
the installer to initialize Anaconda3
in your
/home/user1/.bashrc ? [yes|no]
choice of gyesh is recommended.
After the installation is finished, restart the system.
Now, when you start the terminal, the command line begins with the string g(base)h, which means that the terminal is for the virtual environment gbaseh, although the terminal can be used as usual.
But, if you want to escape from the virtual environment, run the code
conda deactivate
To make the virtual environment active again, execute the following command
conda
activate
To start Jupyter Notebook, execute the following command in the state of the virtual environment being active
jupyter
notebook
To install modules, execute conda command in the active virtual environment as follows
conda install
numpy
Then you can use the module numpy.
Modules have to be installed on each environment on which they are used.
Script files can be written by a text editor, e.g. gedit, as usual when the virtual environment is active.
To execute a script file, move to the folder in which the file is saved, and execute python command in the active virtual environment as this
python
file-name
To start Jupyter notebook, run the following command in the active virtual environment,
jupyter
notebook
When Jupyter Notebook starts, the folders which can be accessed are those under the folder in which the above command is executed. When you access other folders, move to the folder which you access or one under which the folder is, then execute the above command.
When Jupyter Notebook starts, a web browser starts. When Jupyter Notebook ends and the browser is closed, the terminal window shows the program is still running. To stop the program, press down gCtrl + Ch twice, i.e., press C key twice with Ctrl key being pressed. Then the program quits.