Пример #1
0
        protected override void BuildModel()
        {
            StringBuilder rCmd = new StringBuilder(@"
trainRaw=read.csv(""" + RawTrainPath.Replace("\\", "/") + @""", header = TRUE, sep = ',')" + @"
trainNorm=trainRaw
cols=NCOL(trainRaw)
mxmn <- array(0, dim=c(2,cols-1))
options(scipen=999)
for(i in 2:cols) {
  cmax=max(trainRaw[,i])
  cmin=min(trainRaw[,i])
  mxmn[1,i-1]=cmax
  mxmn[2,i-1]=cmin
  trainNorm[,i]=(trainRaw[,i]-((cmax+cmin)/2))/((cmax-cmin)/2)
}
trainNorm[is.na(trainNorm)]=0
write.table(data.frame(mxmn), file=""" + ColumnMaxMinPath.Replace("\\", "/") + @""", row.names=FALSE, col.names=FALSE, sep=',')" + @"
library(randomForest)
rf=randomForest(Class ~., data=trainNorm, ntree=" + _numTrees + ", importance=TRUE, seed=99)" + @"
save(rf, file=""" + RandomForestModelPath.Replace("\\", "/") + @""")" + @"
");
            string        output, error;

            R.Execute(rCmd.ToString(), false, out output, out error);

            try
            {
                if (!File.Exists(RandomForestModelPath))
                {
                    throw new Exception("RandomForest model was not created at \"" + RandomForestModelPath + "\".");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("ERROR:  RandomForest failed to build model. Output and error messages follow:" + Environment.NewLine +
                                    "\tException message:  " + ex.Message + Environment.NewLine +
                                    "\tR output:  " + output + Environment.NewLine +
                                    "\tR orror:  " + error);
            }
            finally
            {
                try { File.Delete(RawTrainPath); }
                catch { }
            }
        }
        protected override void BuildModel()
        {
            StringBuilder rCmd = new StringBuilder(@"
trainRaw=read.csv(""" + RawTrainPath.Replace(@"\", "/") + @""", header = TRUE, sep = ',')" + @"
trainNorm=trainRaw
cols=NCOL(trainRaw)
mxmn <- array(0, dim=c(2,cols-1))
options(scipen=999)
for(i in 2:cols) {
  cmax=max(trainRaw[,i])
  cmin=min(trainRaw[,i])
  mxmn[1,i-1]=cmax
  mxmn[2,i-1]=cmin
  trainNorm[,i]=(trainRaw[,i]-((cmax+cmin)/2))/((cmax-cmin)/2)
}
trainNorm[is.na(trainNorm)]=0
write.table(data.frame(mxmn), file=""" + ColumnMaxMinPath.Replace(@"\", "/") + @""", row.names=FALSE, col.names=FALSE, sep=',')" + @"
library(ada)
set.seed(99)
cls <- sort(unique(trainNorm$Class))
cList <- vector('list', length(cls))
save(cls, file=""" + ClassPath.Replace(@"\", "/") + @""")" + @"
binForm = ""Class ~.,data=trainNorm,iter=" + _iterations + ",loss='logistic',nu=1,type='discrete'" + @"""
multForm = ""Class ~.,data=cList[[i]],iter=" + _iterations + ",loss='logistic',nu=1,type='discrete'" + @"""
if(length(cls)==2) {
  adb <- eval(parse(text=paste('ada(', binForm, ')', sep='')))
  save(adb, file=""" + AdaModelPath.Replace(@"\", "/") + @""")" + @"
} else {
  for(i in 1:length(cls)) {
    cList[[i]] <- trainNorm
    for(j in 1:length(cls)) {
      if(i!=j) {
        levels(cList[[i]]$Class)[levels(cList[[i]]$Class)==cls[j]] <- 'REST'
      }
    }
    adb <- eval(parse(text=paste('ada(', multForm, ')', sep='')))
    save(adb, file=paste('" + Path.Combine(Model.ModelDirectory, @"ada', i, '.RData', sep='')").Replace("\\", "/") + @")" + @"
  }
}
");

            R.Execute(rCmd.ToString(), false);

            File.Delete(RawTrainPath);
        }