Python programming language developed by Dutch computer scientist Guido van Rossum in the late 1980’s, named after the British comedy Monty Pythons Flying Circus

Python is a powerful, general purpose programming language that provides a framework for machine learning. All different machine learning procedures follow the same set of Python code. Once you run one Python machine learning analysis, running others generally follows the same syntax.

Anaconda

Python, modern Version 3.x, can be downloaded from its official source, but there is much more to using Python than just the base language.

Better to download Python and the rest of the complete open source Python environment from Anaconda, Inc. Downloading Python from Anaconda downloads an entire system: Python 3.x, 100’s of packages, conda package manager, and Anaconda Navigator to run Python and more. The package manager manages and updates the needed packages that supplement the base language required for data analysis and machine learning.

Runs identically on Windows, Mac, and Linux.

Install

Download installer.

There are instructions, but basically just do the following:

  1. Select your OS, then the latest version of Python, Version 3.x.
  2. Accept the defaults when prompted by the installer.

Downloading and installing takes some time.

Run

Python can run from a variety of development environments, including standard Python environments such as Jupyter notebooks abd Sypder IDE. For blending with R, can also run Python code within R.

Within Jupyter and Spyder

Jupyter notebooks are available from the app Anaconda Navigator, or directly from the command line by entering jupyter-notebook. The following links provide optional, additional information.

http://docs.anaconda.com/anaconda/user-guide/getting-started/

https://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/

Within R

install.packages(“reticulate”) # one time only
library(“reticulate”) # every session

To get info on your Python configuration and its location, run py_config(). Mac users will need to direct reticulate to find the installed Anaconda version of Python. The Mac comes pre-installed with an older version of Python. Windows users may or may not need to identify the installed Anaconda version.

Identify the directory with the use_python() function. Specify the path name to the Anadonda3 directory. Replace the xxxx with your user name. Note the forward slash for both Windows and Mac.
Windows: use_python("C:/Users/xxxx/Anaconda3") 
Mac:     use_python("~/anaconda3/bin")

Python can not only run in R with reticulate, R and Python can exchange data structures.

  • In R environment: Access a Python object by prefixing name with py$. For example, head(py$d) to access the Python d data frame.

  • In Python environment: Access an R object by prefixing name with r.. For example, r.d.head() to access the R d data frame.

Use repl_python() interactively run Python. To get a plot to actually plot, need plt.show() after the code to create the plot.

Here find an extended example of an R markdown file of running Python within R, with a focus on regression analysis.

Update

Occasionally update the system, such as after some months following the initial installation. Manage the different applications and their relationhips in the Anaconda system with the conda package manager.

Windows: Run the app Anaconda Prompt
Mac: Open the Terminal app (located in Applications –> Utilities )

Enter:
conda update --all