Set up the Python environment--create a virtual environment in Conda

Create a virtual environment in Conda to build a Python environment.

Find the Anaconda folder in the menu bar, click on "Anaconda Prompt", and the following commands are all operated under Anaconda Prompt if there is no description.

conda create -n TransCoxEnvi

Code explanation: This content is a command-line instruction for creating a virtual environment named "TransCoxEnvi". This instruction uses conda as the package manager for creating and managing Python environments. The purpose of creating a virtual environment is to isolate the dependencies of different projects to ensure that dependencies between projects do not conflict with each other. After you create a virtual environment, you can install and manage specific versions of packages and libraries required by your project within that environment.

Next install the required Python packages:

conda install tensorflow-probability==0.8
conda install tensorflow==2.1.0

Enter the above code in Anaconda Prompt, the result is shown in the figure below:

Install the reticulate package in R:

install.packages("reticulate")

Please setup your python environment in R and make sure R recognizes the correct python package you just installed.

library(reticulate)
## modify this to your directory
use_python("/Users/zli16/opt/anaconda3/envs/tf/bin/python") 
use_condaenv("TransCoxEnvi")

Let's test that we can load python packages from R:

tf <- import("tensorflow")
py_run_string("print(tf.__version__)")
py_run_string("xi = tf.Variable(np.repeat([0.], repeats = 100), dtype = 'float64')")

Another method that works for me if your R can't find the python environment is to create a " .Renviron" file in your home directory and put the location of your conda-environment python in this file:

RETICULATE_PYTHON="/Users/zli16/opt/anaconda3/envs/TransCoxEnvi/bin/python"

After doing this, restart R to let R find the correct python to use. Then retest that your R can find the python package. Setting up a python environment can be frustrating.

source:

GitHub - ziyili20/TransCox: Provide transfer learning-based Cox Proportional Hazards model

Guess you like

Origin blog.csdn.net/u011375991/article/details/131796942
Recommended