Permalink
Browse files

Added split method to dataset, and special method __len__

  • Loading branch information...
gasagna committed Nov 26, 2011
1 parent 9cdf62d commit c0af486c489524358029793c9a90493572eb8f44
Showing with 42 additions and 0 deletions.
  1. +42 −0 nn.py
View
42 nn.py
@@ -78,6 +78,48 @@ def __init__ ( self, inputs, targets ):
# length of the dataset, number of samples
self.n_samples = self.inputs.shape[0]
+ def split( self, fractions=[0.5, 0.5]):
+ """Split randomly the dataset into smaller dataset.
+
+ Parameters
+ ----------
+ fraction: list of floats, default = [0.5, 0.5]
+ the dataset is split into ``len(fraction)`` smaller
+ dataset, and the ``i``-th dataset has a size
+ which is ``fraction[i]`` of the original dataset.
+ Note that ``sum(fraction)`` can also be smaller than one
+ but not greater.
+
+ Returns
+ -------
+ subsets: list of :py:class:`nn.Dataset`
+ a list of the subsets of the original datasets
+ """
+
+
+
+ if sum(fractions) > 1.0 or sum(fractions) <= 0:
+ raise ValueError( "the sum of fractions argument should be between 0 and 1" )
+
+ # random indices
+ idx = np.arange(self.n_samples)
+ np.random.shuffle(idx)
+
+ # insert zero
+ fractions.insert(0, 0)
+
+ # gte limits of the subsets
+ limits = (np.cumsum(fractions)*self.n_samples ).astype(np.int32)
+
+ subsets = []
+ # create output dataset
+ for i in range(len(fractions)-1):
+ subsets.append( Dataset(self.inputs[idx[limits[i]:limits[i+1]]], self.targets[idx[limits[i]:limits[i+1]]]) )
+
+ return subsets
+
+ def __len__(self):
+ return len( self.inputs )
class MultiLayerPerceptron( ):
"""A Multi Layer Perceptron feed-forward neural network.

0 comments on commit c0af486

Please sign in to comment.