Package parsimony :: Package utils
[hide private]
[frames] | no frames]

Package utils

source code

Created on Thu Feb 8 09:22:00 2013

Copyright (c) 2013-2014, CEA/DSV/I2BM/Neurospin. All rights reserved.


Author: Tommy Löfstedt and Edouard Duchesnay

License: BSD 3-clause.

Submodules [hide private]

Classes [hide private]
  AnonymousClass
Used to create anonymous classes.
Functions [hide private]
floating point number
time_cpu()
Return the CPU time or real time since the start of the process or since the first call to clock().
floating point number
time_wall()
Return the current time in seconds since the Epoch.
floating point number
time()
Return the CPU time or real time since the start of the process or since the first call to clock().
 
deprecated(*replaced_by)
This decorator can be used to mark functions as deprecated.
source code
 
optimal_shrinkage(X, T=None) source code
 
check_arrays(*arrays)
Checks that: - Lists are converted to numpy arrays.
source code
 
plot_map2d(map2d, plot=None, title=None, limits=None, center_cmap=True) source code
 
class_weight_to_sample_weight(class_weight, y)
Estimate class weights for unbalanced datasets.
source code
 
check_labels(y)
ensure binary classification with 0, 1 labels
source code
Variables [hide private]
  __package__ = 'parsimony.utils'
Function Details [hide private]

time_cpu()

 

Return the CPU time or real time since the start of the process or since the first call to clock(). This has as much precision as the system records.

Returns: floating point number

time_wall()

 

Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them.

Returns: floating point number

time()

 

Return the CPU time or real time since the start of the process or since the first call to clock(). This has as much precision as the system records.

Returns: floating point number

deprecated(*replaced_by)

source code 
This decorator can be used to mark functions as deprecated.

Useful when phasing out old API functions.

Parameters
----------
replaced_by : String. The name of the function that should be used instead.

check_arrays(*arrays)

source code 
Checks that:
    - Lists are converted to numpy arrays.
    - All arrays are cast to float.
    - All arrays have consistent first dimensions.
    - Arrays are at least 2D arrays, if not they are reshaped.

Parameters
----------
*arrays: Sequence of arrays or scipy.sparse matrices with same shape[0]
        Python lists or tuples occurring in arrays are converted to 2D
        numpy arrays.

Examples
--------
>>> import numpy as np
>>> check_arrays([1, 2], np.array([3, 4]), np.array([[1., 2.], [3., 4.]]))
[array([[ 1.],
       [ 2.]]), array([[ 3.],
       [ 4.]]), array([[ 1.,  2.],
       [ 3.,  4.]])]

class_weight_to_sample_weight(class_weight, y)

source code 
Estimate class weights for unbalanced datasets.

Parameters
----------
class_weight : dict, 'auto' or None
    If 'auto', class weights will be given inverse proportional
    to the frequency of the class in the data. sample_weight will sum
    to n_sample.
    If a dictionary is given, keys are classes and values
    are corresponding class weights. With two classes in {1, 0},
    class_weight = {0:0.5, 1:0.5} is equivalent to class_weight == "auto"
    If None is given, the class weights will be uniform sample_weight==1.

y : array-like, shape (n_samples,)
    Array of original class labels per sample;

Returns
-------
weight_vect : ndarray, shape (n_samples,)
    Array with weight_vect[i] the weight for i-th sample

Example
-------
>>> y = [1, 1, 1, 0, 0, 2]
>>> w = class_weight_to_sample_weight("auto", y)
>>> print w.sum() == len(y)
True
>>> print ["%i:%.2f" % (l, np.sum(w[y==l])) for l in np.unique(y)]
['0:2.00', '1:2.00', '2:2.00']
>>> y = [1, 1, 1, 0, 0, 2]
>>> w2 = class_weight_to_sample_weight({0:1./3, 1:1./3, 2:1./3}, y)
>>> np.all(w2 == w)
True