Patterns

class rfbp.Patterns.Pattern(X=None, y=None)[source]

Bases: object

Pattern object for C++ compatibility. The Pattern object is just a simple wrap of a data (matrix) + labels (vector). This object type provide a compatibility with the rFBP functions in C++ and it provides also a series of checks for the input validity.

Parameters:
  • X (None or 2D array-like or string) – Input matrix of variables as (Nsample, Nfeatures) or filename with the input stored in the same way
  • y (None or 1D array-like) – Input labels. The label can be given or read from the input filename as first row in the file.

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import Pattern
>>>
>>> n_sample, n_feature = (20, 101) # n_feature must be odd
>>> data = np.random.choice(a=(-1, 1), p=(.5, .5), size=(n_sample, n_feature))
>>> labels = np.random.choice(a=(-1, 1), p=(.5, .5), size=(n_sample, ))
>>>
>>> pt = Pattern(X=data, y=labels)
>>> # dimensions
>>> assert pt.shape == (n_sample, n_feature)
>>> # data
>>> np.testing.assert_allclose(pt.data, data)
>>> # labels
>>> np.testing.assert_allclose(pt.labels, labels)
data

Return the data matrix

Returns:data – The data matrix as (n_sample, n_features) casted to integers.
Return type:array-like
labels

Return the label array

Returns:labels – The labels vector as (n_sample, ) casted to integers.
Return type:array-like
load(filename, binary=False, delimiter='\t')[source]

Load pattern from file. This is the main utility of the Pattern object. You can use this function to load data from csv-like files OR from a binary file.

Parameters:
  • filename (str) – Filename/Path to the Pattern file
  • binary (bool) – True if the filename is in binary fmt; False for ASCII fmt
  • delimiter (str) – Separator of input file (valid if binary is False)

Example

>>> from ReplicatedFocusingBeliefPropagation import Pattern
>>>
>>> data = Pattern().load(filename='path/to/datafile.csv', delimiter=',', binary=False)
>>> data
  Pattern[shapes=(10, 20)]
pattern

Return the pattern Cython object

Returns:pattern – The cython object wrapped by the Pattern class
Return type:Cython object

Notes

Warning

We discourage the use of this property if you do not know exactly what you are doing!

random(shape)[source]

Generate Random pattern. The pattern is generated using a Bernoulli distribution and thus it creates a data (matrix) + labels (vector) of binary values. The values are converted into the range (-1, 1) for the compatibility with the rFBP algorithm.

Parameters:shapes (tuple) – a 2-D tuple with (M, N) where M is the number of samples and N the number of probes

Example

>>> from ReplicatedFocusingBeliefPropagation import Pattern
>>>
>>> n_sample = 10
>>> n_feature = 20
>>> data = Pattern().random(shape=(n_sample, n_feature))
>>> assert data.shape == (n_sample, n_feature)
>>> data
  Pattern[shapes=(10, 20)]
shape

Return the shape of the data matrix

Returns:shape – The tuple related to the data dimensions (n_sample, n_features)
Return type:tuple