0

Can i run R script in python console , like native. Below i gave simple example with linear regression. Before perform regression i first perform feature selection with Boruta library, then i split sample on test and train and then perform regression with lm function. What i should do tp copy this script and paste in python console, then run it and get the result. How to correctly insert this code into the python? Is it possible without rpy?

 mydat=read.csv("C:/Users/Admin/Downloads/test.csv", sep=";",dec=",")
View(mydat)
str(mydat)


mydat$symboling.<-NULL
mydat$make.<-NULL
mydat$num.of.cylinders.<-NULL
mydat$fuel.type.<-NULL
mydat$aspiration.<-NULL
mydat$num.of.cylinders.<-NULL



 #Feature Selection
    library("Boruta")
    FS=Boruta(normalized.losses.~.,data=mydat)
    getSelectedAttributes(FS, withTentative = F)
    plot(FS, cex.axis=0.5)

#get scatterplot
scatter.smooth(x=mydat$length.,y=mydat$normalized.losses.,main="normalized losse~length")


#split sample on train and sample
index <- sample(1:nrow(mydat),round(0.70*nrow(mydat)))
train <- mydat[index,]
test <- mydat[-index,]


#build the model
mymodel=lm(normalized.losses.~.,data=train)
summary(mymodel)
AIC(mymodel)

#check accuracy for train
pred.tr=predict(mymodel,train)
actual_pred.tr=data.frame(cbind(actual=train$normalized.losses.,predicteds=pred.tr))
actual_pred.tr
psysky
  • 3,037
  • 5
  • 28
  • 64

1 Answers1

2

Yes you can run R code in python console using rpy2.

SuperFADX
  • 31
  • 3
  • can you please give an example of how the code will look if i work with the rpy2 from beginning to end . In a clear example with my code, it will be easier to understand for me – psysky Jan 23 '18 at 12:26