Environment Setup for ML7331

Author

Jessica McPhaul

Create Environment

In Bash (Use mamba or conda depending on your system setup)

mamba create -n ml7331 python=3.9 r-base=4.4.1
mamba activate ml7331
mamba install ipykernel
python -m ipykernel install --user --name ml7331 --display-name "Python (ml7331)"
# (restart vscode or source ~/.bashrc)
mamba activate ml7331
pip install rpy2
mamba install notebook
mamba install tzlocal
mamba install plotly 
pip install matplotlib
pip install scipy
pip install scikit-learn
pip install pillow
pip install tensorflow
pip check

In R

.libPaths(c("C:/Users/jessi/mambaforge/envs/ml7331/Lib/R/library", .libPaths())) 
install.packages("reticulate")
install.packages('arules')
install.packages('arulesViz')
install.packages('mlbench')

Verify Installs:

library(arules)
library(arulesViz)
library(mlbench)
library(reticulate)

Save the Environment:

mamba env export -n ml7331 > ml7331_environment.yml
conda clean --all

In R Studio:

library(reticulate)
use_condaenv("ml7331", required= TRUE)

Run a Simple Python Command:

py_run_string("x = 10")
py_run_string("y = x + 5")
py$x  # Access the Python variable in R
py$y  # Access another Python variable in R

Install an R Package Using Mamba:

system("mamba install -c conda-forge r-ggplot2")

Install numpy Using Mamba:

system("mamba install -c conda-forge numpy")

Alternatively Use PIP:

# Install numpy using pip
py_install("numpy", method = "pip")

Verify Installs:

Check TensorFlow:

tensorflow <- import("tensorflow")
print(tensorflow$__version__)

Check PyTorch:

torch <- import("torch")
print(torch$__version__)

Check Keras (if needed):

keras <- import("keras")
print(keras$__version__)

Check Packages:

system("mamba list")
system("pip list")

In R Markdown, Notebook, or Quarto:

x = 10
y = x + 5
print(y)
15

Notes:

  • rpy2 is used to run R within Python. (install with pip)
  • reticulate is used to run Python within R (install within R). ```

NOTES FOR JESS AUG 20 2024

  • CHANGED R STUIO DIRECT PYTHON PATH TO THIS ENV NOTE FOR ISSUES LATER:
    • library(reticulate) use_python(“C:/Users/jessi/mambaforge/envs/ml7331/python.exe”, required = TRUE)
    • cmd: set QUARTO_PYTHON=“C:/Users/jessi/mambaforge/envs/ml7331/python.exe”
    • bash: export QUARTO_PYTHON=“C:/Users/jessi/mambaforge/envs/ml7331/python.exe”
    • r: file.edit(“~/.Renviron”)
      • in open file (in R) paste: QUARTO_PYTHON=“C:/Users/jessi/mambaforge/envs/ml7331/python.exe”
      • also paste in bash: “QUARTO_PYTHON=”C:/Users/jessi/mambaforge/envs/ml7331/python.exe”
    • bash: QUARTO_PYTHON=“C:/Users/jessi/mambaforge/envs/ml7331/python.exe”
  • check PATH
  • HUGE but complete tinytex: tinytex::install_tinytex(bundle = ‘TinyTeX-2’)