Run Python From R
R is mainly known for data analysis, statistical modeling and visualization. While python is popular for deep learning and natural language processing.
Python and R were ranked top 2 tools for data science and machine learning. If you really want to boost your career in data science world, these are the languages you need to focus on.
How To Call Or Run Python From R?
RStudio developed a package called reticulate which provides a medium to run Python packages and functions from R.
# Load reticulate package
if (!require("reticulate")) install.packages("reticulate")
## Loading required package: reticulate
Python Version Configuration
If the version of Python you want to use is located on the system PATH then it will be automatically discovered (via Sys.which) and used.
Alternatively, you can use one of the following functions to specify alternate versions of Python:
Function Description
use_python Specify the path a specific Python binary.
use_virtualenv Specify the directory containing a Python virtualenv.
use_condaenv Specify the name of a Conda environment.
🔥 import a python module within R
os <- import("os")
pd <- import("pandas")
numpy <- import("numpy")
py_module_available("pandas")
## [1] TRUE
os$getcwd()
## [1] "/Users/zero/myrepo/jixingBlogdown/content/post"
Install Python package with conda in terminal
working with numpy
y <- array(1:4, c(2, 2)) # create a matrix with R
x <- numpy$array(y) # edit it with python
numpy$transpose(y) # transpose the above array
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
numpy$linalg$eig(y) # Eigenvalues and eigen vectors
## [[1]]
## [1] -0.3722813 5.3722813
##
## [[2]]
## [,1] [,2]
## [1,] -0.9093767 -0.5657675
## [2,] 0.4159736 -0.8245648
numpy$sqrt(x)
## [,1] [,2]
## [1,] 1.000000 1.732051
## [2,] 1.414214 2.000000
numpy$exp(x)
## [,1] [,2]
## [1,] 2.718282 20.08554
## [2,] 7.389056 54.59815
🔥 Sourcing Python scripts
Call the python fuction in R:
You can source any Python script just as you would source an R script using the source_python() function, then call the fuction in R.
#use Python functions
source_python("../../scripts/add.py")# minus in action
add(1,1)
## [1] 0
Access objects created in python from R:
If your Python file doesn’t contain functions, but also creates objects, use py_run_file instead of source_python
⭐Access objects created in python from R
with repl_python() note: Don’t work with Rmd
repl_python()
#===== python console====
# Load Pandas package
import pandas as pd
# Importing Dataset
travel = pd.read_excel("data/AIR.xlsx")
# Number of rows and columns
travel.shape
# Select random no. of rows
travel.sample(n = 10)
# Group By
travel.groupby("Year").AIR.mean()
# Filter
t = travel.loc[(travel.Month >= 6) & (travel.Year >= 1955),:]
# Return to R
exit
#==== R console====
# Access objects created in python from R
summary(py$t)
# Line chart using ggplot2
library(ggplot2)
ggplot(py$t, aes(AIR, Year)) + geom_line()
⭐Access objects created in R from Python
# Let's create a object in R
#===== R console====
mydata = head(cars, n=15)
#Use the R created object within Python REPL
repl_python()
#===== python console====
import pandas as pd
#Access objects created in R from Python: r.mydata
r.mydata.describe()
pd.isnull(r.mydata.speed)
exit
⭐Building Logistic Regression Model using sklearn package
repl_python()
# Load libraries
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
# load the iris datasets
iris = datasets.load_iris()
# Developing logit model
model = LogisticRegression()
model.fit(iris.data, iris.target)
# Scoring
actual = iris.target
predicted = model.predict(iris.data)
# Performance Metrics
print(metrics.classification_report(actual, predicted))
print(metrics.confusion_matrix(actual, predicted))