rfgb.utils module

(docstring for utils)

class rfgb.utils.Data(regression=False, advice=False, softm=False, alpha=0.0, beta=0.0)[source]

Bases: object

Object containing the relational data.

getExampleTrueValue(example)[source]

Returns true regression value of an example for regression learning.

getFacts()[source]

returns the facts in the data

getLiterals()[source]

gets all the literals in the facts

getTarget()[source]

Returns the target.

getValue(example)[source]

Returns the regression value for an example.

Example:

from rfgb.utils import Utils
from rfgb.utils import Data

trainingData = Utils.readTrainingData('cancer',
                    path='testDomain/ToyCancer/train/')

x = trainingData.getValue('cancer(earl)')
# x == -0.5, since earl doesn't have cancer.

y = trainingData.getValue('cancer(alice)')
# y == 0.5, since alice does have cancer
setBackground(bk)[source]

Obtains the literals and their type specifications. Types can be either variable or a list of constants.

setExamples(examples, target)[source]

Set examples for regression.

setFacts(facts)[source]

Mutate the facts in the data object.

Parameters:facts (list.) – List of strings representing the facts.
Returns:None
setNeg(neg, target)[source]

Set negative examples based on the contents of a list.

setPos(pos, target)[source]

Set positive examples based on the contents of a list.

setTarget(bk, target)[source]

Sets self.target as a target string. Sets self.variableType

Parameters:
  • bk (list.) – List of strings representing modes.
  • target (str.) – Target relation or attribute.
Returns:

None

Example:

from rfgb.utils import Data

data = Data(regression=False)
background = ['friends(+person,-person)',
              'friends(-person,+person)',
              'smokes(+person)',
              'cancer(-person)']
target = 'cancer'

data.setTarget(background, target)

print(data.target)
# 'cancer(C)'
variance(examples)[source]

Calculates the variance of the regression values from a subset of the data.

class rfgb.utils.Utils[source]

Bases: object

Class of utilities used by rfgb, such as reading files, removing mode symbols, calculating Cartesian Products, etc.

UniqueVariableCollection = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
static addVariableTypes(literal)[source]

As literals are encountered, update Utils.data.variableType with the type of the variables encountered.

Parameters:literal (str.) – A literal of the form smokes(W) or friends(A,B)
static cartesianProduct(itemSets)[source]

Returns the Cartesian Product of all sets contained in the item sets.

data = None
static getleafValue(examples)[source]

returns average of regression values for examples

static load(location)[source]

Loads json version of learnedDecisionTree from location.

Parameters:location (str.) – Name of the file to load.
Returns:None.
static readTestData(target, path='test/', regression=False)[source]

Reads the testing data from files.

Parameters:
  • target (str.) – The target predicate.
  • path (str.) – Path to the training data.
  • regression (bool) – Read from examples.txt instead of pos.txt and neg.txt.
Default path:

‘train/’

Default regression:
 

False

Returns:

A Data object representing the training data.

Return type:

utils.Data

static readTrainingData(target, path='train/', regression=False, advice=False, softm=False, alpha=0.0, beta=0.0)[source]

Reads the training data from files.

Parameters:
  • target (str.) – The target predicate.
  • path (str.) – Path to the training data.
  • regression (bool) – Read from examples.txt instead of pos.txt and neg.txt.
  • advice (bool) – Read advice from an advice file, which should be contained in the same directory as the examples.
Default path:

‘train/’

Default regression:
 

False

Default advice:

False

Returns:

A Data object representing the training data.

Return type:

utils.Data

static removeModeSymbols(inputString)[source]

Returns a string with the mode symbols (+,-,#) removed.

Example:

from rfgb.utils import Utils

removeModeSymbols('#city')
# == 'city'

i = ['+drinks', '-drink', '-city']
o = list(map(removeModeSymbols, i))
# o == ['drinks', 'drink', 'city']
static save(location, saveItem)[source]

Dumps json version of learnedDecisionTree to location.

Parameters:location (str.) – Name of the file to write.
Returns:None.
static sigmoid(x)[source]
Parameters:x (int or float) – Number to apply sigmoid to.
Returns:exp(x)/float(1+exp(x))
Return type:float