【发布时间】:2018-01-23 09:37:24
【问题描述】:
我可以像原生一样在 python 控制台中运行 R 脚本吗?
下面我给出了线性回归的简单例子。在执行回归之前,我首先使用Boruta 库执行特征选择,然后在测试和训练中拆分样本,然后使用lm 函数执行回归。
我应该做什么 tp 复制这个脚本并粘贴到 python 控制台中,然后运行它并得到结果。如何正确将此代码插入python?没有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
【问题讨论】: