romspline/cross_validation.py
def Kfold(self, x, y, K=10, parallel=True, random=True):
    """K-fold cross-validation
    
    Input
    -----
      x        -- samples
      y        -- data to be downsampled
      K        -- number of partitions
                  (default 10)
      parallel -- parallelize the computation over each partition?
                  (default True)
      random   -- fill the partitions randomly from the data?
                  (default True)
    
    Attributes
    ----------
      args   -- data array indexes of largest validation errors 
                on each validation set
      errors -- validation errors on each validation set
    
    Comments
    --------
    The default option for `K` is 10 so that a random 10% subset of the 
    data is used to validate the trial reduced-order splines.
    
    If `random` option is False then the partitions are filled with the
    data in sequential order. This is not a good idea for trying to 
    assess interpolation errors because interpolating over large regions
    of data will likely incur large errors. This option may be removed in 
    future romSpline releases.
    """
    
    assert len(x) == len(y), "Expecting input data to have same length."
    
    self._K = int(K)
    self._size = len(x)
    
    # Allocate some memory
    self.args = np.zeros(K, dtype='int')
    self.errors = np.zeros(K, dtype='double')
    
    # Divide into K nearly equal partitions
    if random:
      self._partitions = random_partitions(self._size, self._K)
    else:
      self._partitions = partitions(self._size, self._K)
    
    if (parallel is False) or (state._PARALLEL is False):
      for ii in range(self._K):
        validation, training = partitions_to_sets(ii, self._partitions, sort=True)
        errors, self.errors[ii], self.args[ii] = _cross_validation(x, y, validation, training, tol=self._tol, rel=self._rel, deg=self._deg)
    
    # TODO: Change parallelization to simple Pool processes
    elif state._PARALLEL is True:
      # Determine the number of processes to run on
      if parallel is True:
        try:
          numprocs = cpu_count()
        except NotImplementedError:
          numprocs = 1
      else:
        numprocs = parallel
      
      # Create a parallel process executor
      executor = ProcessPoolExecutor(max_workers=numprocs)
      
      # Compute the spline errors on each validation partition
      output = []
      for ii in range(self._K):
        validation, training = partitions_to_sets(ii, self._partitions, sort=True)
        output.append( executor.submit(_cross_validation, x, y, validation, training, tol=self._tol, rel=self._rel, deg=self._deg) )
      
      # Gather the results
      for ii, ee in enumerate(output):
        errors, self.errors[ii], self.args[ii] = ee.result()
    
    mask = (self.errors >=self._tol)
    self.args_ge_tol = self.args[mask]
    self.errors_ge_tol = self.errors[mask]
    
    self._made = True
K-fold cross-validation
    
    Input
    -----
      x        -- samples
      y        -- data to be downsampled
      K        -- number of partitions
                  (default 10)
      parallel -- parallelize the computation over each partition?
                  (default True)
      random   -- fill the partitions randomly from the data?
                  (default True)
    
    Attributes
    ----------
      args   -- data array indexes of largest validation errors 
                on each validation set
      errors -- validation errors on each validation set
    
    Comments
    --------
    The default option for `K` is 10 so that a random 10% subset of the 
    data is used to validate the trial reduced-order splines.
    
    If `random` option is False then the partitions are filled with the
    data in sequential order. This is not a good idea for trying to 
    assess interpolation errors because interpolating over large regions
    of data will likely incur large errors. This option may be removed in 
    future romSpline releases.
    
K-fold cross-validation
Input



Attributes

args   -- data array indexes of largest validation errors
on each validation set
errors -- validation errors on each validation set

Comments

The default option for `K` is 10 so that a random 10% subset of the
data is used to validate the trial reduced-order splines.

If `random` option is False then the partitions are filled with the
data in sequential order. This is not a good idea for trying to
assess interpolation errors because interpolating over large regions
of data will likely incur large errors. This option may be removed in
future romSpline releases.
K - fold cross - validation Input Attributes args -- data array indexes of largest validation errors on each validation set errors -- validation errors on each validation set Comments The default option for ` K ` is 10 so that a random 10% subset of the data is used to validate the trial reduced - order splines . If ` random ` option is False then the partitions are filled with the data in sequential order . This is not a good idea for trying to assess interpolation errors because interpolating over large regions of data will likely incur large errors . This option may be removed in future romSpline releases .
def Kfold(self, x, y, K=10, parallel=True, random=True):
    assert len(x) == len(y), "Expecting input data to have same length."
    self._K = int(K)
    self._size = len(x)
    self.args = np.zeros(K, dtype='int')
    self.errors = np.zeros(K, dtype='double')
    if random:
      self._partitions = random_partitions(self._size, self._K)
    else:
      self._partitions = partitions(self._size, self._K)
    if (parallel is False) or (state._PARALLEL is False):
      for ii in range(self._K):
        validation, training = partitions_to_sets(ii, self._partitions, sort=True)
        errors, self.errors[ii], self.args[ii] = _cross_validation(x, y, validation, training, tol=self._tol, rel=self._rel, deg=self._deg)
    elif state._PARALLEL is True:
      if parallel is True:
        try:
          numprocs = cpu_count()
        except NotImplementedError:
          numprocs = 1
      else:
        numprocs = parallel
      executor = ProcessPoolExecutor(max_workers=numprocs)
      output = []
      for ii in range(self._K):
        validation, training = partitions_to_sets(ii, self._partitions, sort=True)
        output.append( executor.submit(_cross_validation, x, y, validation, training, tol=self._tol, rel=self._rel, deg=self._deg) )
      for ii, ee in enumerate(output):
        errors, self.errors[ii], self.args[ii] = ee.result()
    mask = (self.errors >=self._tol)
    self.args_ge_tol = self.args[mask]
    self.errors_ge_tol = self.errors[mask]
    self._made = True
def Kfold ( self , x , y , K = 10 , parallel = True , random = True ) : assert len ( x ) == len ( y ) , "Expecting input data to have same length." self . _K = int ( K ) self . _size = len ( x ) self . args = np . zeros ( K , dtype = 'int' ) self . errors = np . zeros ( K , dtype = 'double' ) if random : self . _partitions = random_partitions ( self . _size , self . _K ) else : self . _partitions = partitions ( self . _size , self . _K ) if ( parallel is False ) or ( state . _PARALLEL is False ) : for ii in range ( self . _K ) : validation , training = partitions_to_sets ( ii , self . _partitions , sort = True ) errors , self . errors [ ii ] , self . args [ ii ] = _cross_validation ( x , y , validation , training , tol = self . _tol , rel = self . _rel , deg = self . _deg ) elif state . _PARALLEL is True : if parallel is True : try : numprocs = cpu_count ( ) except NotImplementedError : numprocs = 1 else : numprocs = parallel executor = ProcessPoolExecutor ( max_workers = numprocs ) output = [ ] for ii in range ( self . _K ) : validation , training = partitions_to_sets ( ii , self . _partitions , sort = True ) output . append ( executor . submit ( _cross_validation , x , y , validation , training , tol = self . _tol , rel = self . _rel , deg = self . _deg ) ) for ii , ee in enumerate ( output ) : errors , self . errors [ ii ] , self . args [ ii ] = ee . result ( ) mask = ( self . errors >= self . _tol ) self . args_ge_tol = self . args [ mask ] self . errors_ge_tol = self . errors [ mask ] self . _made = True
K-fold cross-validation
Input
K - fold cross - validation Input
"""K-fold cross-validation
    
    Input
    -----
      x        -- samples
      y        -- data to be downsampled
      K        -- number of partitions
                  (default 10)
      parallel -- parallelize the computation over each partition?
                  (default True)
      random   -- fill the partitions randomly from the data?
                  (default True)
    
    Attributes
    ----------
      args   -- data array indexes of largest validation errors 
                on each validation set
      errors -- validation errors on each validation set
    
    Comments
    --------
    The default option for `K` is 10 so that a random 10% subset of the 
    data is used to validate the trial reduced-order splines.
    
    If `random` option is False then the partitions are filled with the
    data in sequential order. This is not a good idea for trying to 
    assess interpolation errors because interpolating over large regions
    of data will likely incur large errors. This option may be removed in 
    future romSpline releases.
    """ # Allocate some memory # Divide into K nearly equal partitions # TODO: Change parallelization to simple Pool processes # Determine the number of processes to run on # Create a parallel process executor # Compute the spline errors on each validation partition # Gather the results
self
x
y
K
parallel
random