[GRASS-SVN] r68623 - grass-addons/grass7/vector/v.class.mlR
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Jun 6 08:04:38 PDT 2016
Author: mlennert
Date: 2016-06-06 08:04:38 -0700 (Mon, 06 Jun 2016)
New Revision: 68623
Modified:
grass-addons/grass7/vector/v.class.mlR/v.class.mlR.html
grass-addons/grass7/vector/v.class.mlR/v.class.mlR.py
Log:
v.class.mlR: handle case of only one single classifier
Modified: grass-addons/grass7/vector/v.class.mlR/v.class.mlR.html
===================================================================
--- grass-addons/grass7/vector/v.class.mlR/v.class.mlR.html 2016-06-06 13:56:40 UTC (rev 68622)
+++ grass-addons/grass7/vector/v.class.mlR/v.class.mlR.html 2016-06-06 15:04:38 UTC (rev 68623)
@@ -47,7 +47,9 @@
<p>In the output (as attribute columns or text file) each weighting schemes
result is provided accompanied by a value that can be considered as an
estimation of the probability of the classification after weighted vote,
-based on equation (2) in Moreno et al (2006), page 709.
+based on equation (2) in Moreno et al (2006), page 709. At this stage, this
+estimation does not, however, take into account the probabilities determined
+individually by each classifier.
<p>Optional output of the module include detailed information about the
different classifier models and their cross-validation results
@@ -78,9 +80,10 @@
<li>Add automagic installation of missing R packages.</li>
<li>Add option to manually define grid of tuning parameters</li>
<li>Check for existing file created by R as no overwrite check is
- done in R</li>
+ done in R</li>
+ <li>Use class probabilities determined by individual classifiers
+ to calculate overall class probabilities</li>
</ul>
--
<h2>EXAMPLE</h2>
Modified: grass-addons/grass7/vector/v.class.mlR/v.class.mlR.py
===================================================================
--- grass-addons/grass7/vector/v.class.mlR/v.class.mlR.py 2016-06-06 13:56:40 UTC (rev 68622)
+++ grass-addons/grass7/vector/v.class.mlR/v.class.mlR.py 2016-06-06 15:04:38 UTC (rev 68623)
@@ -328,12 +328,24 @@
r_file.write("models.cv$%s <- %sModel.cv" % (classifier, classifier))
r_file.write("\n")
+ r_file.write("if (length(models.cv)>1) {")
+ r_file.write("\n")
r_file.write("resamps.cv <- resamples(models.cv)")
r_file.write("\n")
r_file.write("accuracy_means <- as.vector(apply(resamps.cv$values[seq(2,length(resamps.cv$values), by=2)], 2, mean))")
r_file.write("\n")
r_file.write("kappa_means <- as.vector(apply(resamps.cv$values[seq(3,length(resamps.cv$values), by=2)], 2, mean))")
r_file.write("\n")
+ r_file.write("} else {")
+ r_file.write("\n")
+ r_file.write("resamps.cv <- models.cv[[1]]$resample")
+ r_file.write("\n")
+ r_file.write("accuracy_means <- mean(resamps.cv$Accuracy)")
+ r_file.write("\n")
+ r_file.write("kappa_means <- mean(resamps.cv$Kappa)")
+ r_file.write("\n")
+ r_file.write("}")
+ r_file.write("\n")
r_file.write("predicted <- data.frame(predict(models.cv, features))")
r_file.write("\n")
if flags['i']:
@@ -360,8 +372,9 @@
r_file.write("\n")
r_file.write("resultsdf$%s_%s <- vote$V1" % (output_classcol, weighting_mode))
r_file.write("\n")
- r_file.write("resultsdf$%s_%s <- vote$V2" % (output_probcol, weighting_mode))
- r_file.write("\n")
+ if len(classifiers) > 1:
+ r_file.write("resultsdf$%s_%s <- vote$V2" % (output_probcol, weighting_mode))
+ r_file.write("\n")
if allmap and not flags['f']:
model_output = '.gscript_tmp_model_output_%d.csv' % os.getpid()
@@ -384,13 +397,11 @@
r_file.write("write.csv(resultsdf, '%s', row.names=FALSE, quote=FALSE)" % classification_results)
r_file.write("\n")
if accuracy_file:
- r_file.write("df_means <- data.frame(method=names(resamps.cv$methods),accuracy=accuracy_means, kappa=kappa_means)")
+ r_file.write("df_means <- data.frame(method=names(model.cv),accuracy=accuracy_means, kappa=kappa_means)")
r_file.write("\n")
r_file.write("write.csv(df_means, '%s', row.names=FALSE, quote=FALSE)" % accuracy_file)
r_file.write("\n")
if model_details:
- r_file.write("conf.mat.cv <- lapply(models.cv, function(x) confusionMatrix(x))")
- r_file.write("\n")
r_file.write("sink('%s')" % model_details)
r_file.write("\n")
r_file.write("cat('BEST TUNING VALUES\n')")
@@ -413,6 +424,8 @@
r_file.write("\n")
r_file.write("cat('******************************\n\n')")
r_file.write("\n")
+ r_file.write("conf.mat.cv <- lapply(models.cv, function(x) confusionMatrix(x))")
+ r_file.write("\n")
r_file.write("print(conf.mat.cv)")
r_file.write("\n")
r_file.write("cat('\nDETAILED CV RESULTS\n')")
@@ -424,7 +437,7 @@
r_file.write("sink()")
r_file.write("\n")
- if bw_plot_file:
+ if bw_plot_file and len(classifiers) > 1:
r_file.write("png('%s.png')" % bw_plot_file)
r_file.write("\n")
r_file.write("print(bwplot(resamps.cv))")
@@ -451,11 +464,15 @@
f = open(model_output_desc, 'w')
header_string = '"Integer"'
if flags['i']:
- for model in classifiers:
+ for classifier in classifiers:
header_string += ',"Integer"'
- for weighting_mode in weighting_modes:
+ if len(classifiers) > 1:
+ for weighting_mode in weighting_modes:
+ header_string += ',"Integer"'
+ header_string += ',"Real"'
+ else:
header_string += ',"Integer"'
- header_string += ',"Real"'
+
f.write(header_string)
f.close()
More information about the grass-commit
mailing list