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.
-
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.
-
setFacts
(facts)[source]¶ Mutate the facts in the data object.
Parameters: facts (list.) – List of strings representing the facts. Returns: None
-
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)'
-
-
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
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 ofpos.txt
andneg.txt
.
Default path: ‘train/’
Default regression: False
Returns: A Data object representing the training data.
Return type:
-
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 ofpos.txt
andneg.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:
-
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']
-