搜索此博客

2017年3月24日星期五

ROracle on Linux

1.
sudo apt-get install alien
sudo apt-get install libaio1
2.
Download:
 
oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
oracle-instantclient12.1-devel-12.2.0.1.0-1.x86_64.rpm
oracle-instantclient12.1-sqlplus-12.2.0.1.0-1.x86_64.rpm

(http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html)
3.
sudo alien -i oracle-instantclient12.1-basic-12.2.0.1.0-1.x86_64.rpm
sudo alien -i oracle-instantclient12.1-devel-12.2.0.1.0-1.x86_64.rpm
sudo alien -i oracle-instantclient12.1-sqlplus-12.2.0.1.0-1.x86_64.rpm

4.
export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export ORACLE_HOME=/usr/lib/oracle/12.2/client64
export PATH=$PATH:$ORACLE_HOME/bin

echo "/usr/lib/oracle/12.2/client64/lib" | sudo tee /etc/ld.so.conf.d/oracle.conf
sudo ldconfig -v

5.
echo $LD_LIBRARY_PATH
echo $ORACLE_HOME
echo $PATH
sqlplus   # should give you the command prompt

6.
sudo R CMD INSTALL --configure-args='--with-oci-lib=/usr/lib/oracle/12.2/client64/lib --with-oci-inc=/usr/include/oracle/12.2/client64' /home/xuefliang/Downloads/ROracle_1.3-1.tar.gz

2017年3月23日星期四

caret学习

---
title: "caret学习"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r include=FALSE}
library(caret)
data(mdrr)
```
#数据预处理
##常数自变量,或者是方差极小的自变量
```{r}
zerovar <- nearZeroVar(mdrrDescr)
newdata1 <- mdrrDescr[,-zerovar]
```

##删除的是与其它自变量有很强相关性的变量
```{r}
descrCorr <- cor(newdata1)
highCorr <- findCorrelation(descrCorr,0.9)
newdata2 <- newdata1[,-highCorr]
```

##自变量多重共线性
```{r}
comboinfo <- findLinearCombos(newdata1)
newdata2 <- newdata2[,-comboinfo$remove]
```

##标准化并补足缺失值
```{r}
Process <- preProcess(newdata2)
newdata3 <- predict(Process,newdata2)
```

##用createDataPartition将数据进行划分,分成75%的训练样本和25%检验样本
```{r}
inTrain <- createDataPartition(mdrrClass,p=3/4,list = F)
trainx <- newdata3[inTrain,]
testx <- newdata3[-inTrain,]
trainy <- mdrrClass[inTrain]
testy <- mdrrClass[-inTrain]
```

##建模前还可以对样本数据进行图形观察,例如对前两个变量绘制箱线图
```{r}
featurePlot(trainx[,1:2],trainy,plot = 'box')
```

#特征选择
##定义几个整数,程序必须测试这些数目的自变量
```{r}
subsets <- c(20,30,40,50,60,70,80)
```

##定义控制参数,functions是确定用什么样的模型进行自变量排序,本例选择的模型是##随机森林即rfFuncs,可以选择的还有lmFuncs(线性回归),nbFuncs(朴素贝叶斯)##,treebagFuncs(装袋决策树),caretFuncs(自定义的训练模型)
##method是确定用什么样的抽样方法,本例使用cv即交叉检验, 还有提升boot以及留一交叉检验LOOCV
```{r}
ctrl <- rfeControl(functions = rfFuncs,method = "cv",verbose = F,returnResamp = "final")
```

##rfe命令进行特征选择
```{r}
profile <- rfe(newdata3,mdrrClass,sizes = subsets,rfeControl = ctrl)
print(profile)
plot(profile)
profile$optVariables
```

#建模与参数优化
##过特征选择后的样本数据,并划分为训练样本和检验样本
```{r}
newdata4 <- newdata3[,profile$optVariables]
inTrain <- createDataPartition(mdrrClass,p=3/4,list = F)
trainx <- newdata4[inTrain,]
testx <-newdata4[-inTrain,]
trainy <- mdrrClass[inTrain]
testy <- mdrrClass[-inTrain]
```

##定义模型训练参数,method确定多次交叉检验的抽样方法,number确定了划分的重数, repeats确定了反复次数。
```{r}
fitControl <- trainControl(method = 'repeatedcv',number = 10,repeats = 3,returnResamp ='all')
```

##确定参数选择范围,本例建模准备使用gbm算法
```{r}
gbmGrid <- expand.grid(.n.trees = c(50, 100, 150, 200, 250, 300),.interaction.depth = c(1, 3),.shrinkage = 0.1,.n.minobsinnode=c(10))
```

##确定参数选择范围,本例建模准备使用gbm算法
```{r}
gbmFit1 <- train(trainx,trainy,method='gbm',trControl=fitControl,tuneGrid=gbmGrid,verbose=F)
print(gbmFit1)
plot(gbmFit1)
```

#模型预测与检验
```{r}
predict(gbmFit1, newdata = testx)[1:5]
gbmFit2 <- train(trainx,trainy,method='treebag',trControl=fitControl)
models <- list(gbmFit1,gbmFit2)
```

##另一种得到预测结果的方法是使用extractPrediction函数
```{r}
predValues <- extractPrediction(models,testX = testx,testY = testy)
head(predValues)
testValues <- subset(predValues,dataType=='Test')
```

##如果要得到预测概率,则使用extractProb函数
```{r}
probValues <- extractProb(models,testX = testx,testY = testy)
testProbs <- subset(probValues,dataType=='Test')
```

##分类问题的效能检验,最重要的是观察预测结果的混淆矩阵
```{r}
Pred1 <- subset(testValues,model=='gbm')
Pred2 <- subset(testValues,model=='treebag')
confusionMatrix(Pred1$pred,Pred1$obs)
confusionMatrix(Pred2$pred,Pred2$obs)
```

##ROCR包来绘制ROC图
```{r}
prob1 <- subset(testProbs,model=='gbm')
prob2 <- subset(testProbs,model=='treebag')
library(ROCR)
prob1$label <- ifelse(prob1$obs=='Active',yes = 1,0)
pred1 <- prediction(prob1$Active,prob1$label)
perf1 <- performance(pred1,measure = 'tpr',x.measure = 'fpr')
plot(perf1)
```