001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.experiment.validation.cross; 031 032import java.util.Iterator; 033import java.util.List; 034 035import org.openimaj.data.dataset.ListBackedDataset; 036import org.openimaj.data.dataset.ListDataset; 037import org.openimaj.experiment.dataset.util.DatasetAdaptors; 038import org.openimaj.experiment.validation.DefaultValidationData; 039import org.openimaj.experiment.validation.ValidationData; 040import org.openimaj.util.list.AcceptingListView; 041import org.openimaj.util.list.SkippingListView; 042 043/** 044 * Leave-One-Out Cross Validation (LOOCV) with a {@link ListDataset}. 045 * The number of iterations performed by the iterator is equal 046 * to the number of data items. 047 * <p> 048 * Upon each iteration, the dataset is split into training 049 * and validation sets. The validation set will have exactly one 050 * instance. All remaining instances are placed in the training 051 * set. As the iterator progresses, every instance will be included 052 * in the validation set one time. 053 * 054 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 055 * 056 * @param <INSTANCE> Type of instances 057 */ 058public class LeaveOneOut<INSTANCE> implements CrossValidator<ListDataset<INSTANCE>> { 059 private class LeaveOneOutIterable implements CrossValidationIterable<ListDataset<INSTANCE>> { 060 private ListDataset<INSTANCE> dataset; 061 private List<INSTANCE> listView; 062 063 /** 064 * Construct {@link LeaveOneOutIterable} on the given dataset. 065 * @param dataset the dataset 066 */ 067 public LeaveOneOutIterable(ListDataset<INSTANCE> dataset) { 068 this.dataset = dataset; 069 this.listView = DatasetAdaptors.asList(dataset); 070 } 071 072 /** 073 * Get the number of iterations that the {@link Iterator} 074 * returned by {@link #iterator()} will perform. 075 * 076 * @return the number of iterations that will be performed 077 */ 078 @Override 079 public int numberIterations() { 080 return dataset.size(); 081 } 082 083 @Override 084 public Iterator<ValidationData<ListDataset<INSTANCE>>> iterator() { 085 return new Iterator<ValidationData<ListDataset<INSTANCE>>>() { 086 int validationIndex = 0; 087 088 @Override 089 public boolean hasNext() { 090 return validationIndex < dataset.size(); 091 } 092 093 @Override 094 public ValidationData<ListDataset<INSTANCE>> next() { 095 ListDataset<INSTANCE> training = new ListBackedDataset<INSTANCE>(new SkippingListView<INSTANCE>(listView, validationIndex)); 096 ListDataset<INSTANCE> validation = new ListBackedDataset<INSTANCE>(new AcceptingListView<INSTANCE>(listView, validationIndex)); 097 098 validationIndex++; 099 100 return new DefaultValidationData<ListDataset<INSTANCE>>(training, validation); 101 } 102 103 @Override 104 public void remove() { 105 throw new UnsupportedOperationException(); 106 } 107 }; 108 } 109 } 110 111 @Override 112 public CrossValidationIterable<ListDataset<INSTANCE>> createIterable(ListDataset<INSTANCE> data) { 113 return new LeaveOneOutIterable(data); 114 } 115 116 @Override 117 public String toString() { 118 return "Leave-One-Out Cross Validation (LOOCV)"; 119 } 120}