本文整理汇总了Python中Factory.Factory.get方法的典型用法代码示例。如果您正苦于以下问题:Python Factory.get方法的具体用法?Python Factory.get怎么用?Python Factory.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Factory.Factory
的用法示例。
在下文中一共展示了Factory.get方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DataSet
# 需要导入模块: from Factory import Factory [as 别名]
# 或者: from Factory.Factory import get [as 别名]
class DataSet(object):
def __init__(self, filePath=None):
self.name = None
self.attributes = None
self.examples = ExampleSet()
self.iteration_index = 0
if filePath is not None:
self.initialize(filePath)
def __iter__(self):
""" allow for iteration over the examples """
return self
def next(self):
""" get next item in iteration
@return Example object
"""
try:
self.iteration_index += 1
return self.examples[self.iteration_index-1]
except(IndexError):
self.iteration_index = 0
raise StopIteration
def addAttribute(self, attribute):
""" add attribute to attributes """
self.attributes.add(attribute)
def addExample(self, example):
""" add example object to examples """
self.examples.add(example)
def convert(self, stringData):
""" return Example class object from string input """
return [self.attributes.get(i).getValues(a) for i,a in enumerate(stringData.replace('#', '').split())]
def getName(self):
""" return dataset name """
return self.name
def getAttribute(self, i = None):
""" return ith attribute """
return self.attributes.get(i)
def getAttributes(self):
""" return all attributes """
return self.attributes
def getValueAttributes(self):
""" """
return [self.attributes[i] for i in range(len(self.attributes))[:-1]]
def getLabelAttributes(self):
""" """
return self.attributes[-1]
def getExample(self, i = None):
""" return ith example """
return self.examples.get(i)
def getExamples(self):
return self.examples
def getExamplesByClass(self, i = None):
""" return examples with label i """
return ExampleSet(self.examples.getExamples(i))
def getExamplesByAttribute(self, a, v, c = 1):
""" return examples with specified (a) attribute, (v) value, (c) label """
return [e.getValues() + [e.getLabel()] for e in self.examples if (e.getValue(a) == v) and (e.getLabel() == c)]
def getLabels(self):
""" return class labels """
return self.attributes[-1].getValues()
def getTrainTestSet(self, percent = .6):
""" return tuple of testing and training subsets of data with ratio 'percent' """
if percent > .9: percent = .9
if percent < .1: percent = .1
n = int(len(self.examples) * percent)
trainSet = Factory().build(random.sample(self.examples, n), self.attributes)
testSet = Factory().build([example for example in self.examples if example not in trainSet], self.attributes)
return trainSet, testSet
def setSeed(self, n = 10):
""" set seed number for randomizer """
random.seed(n)
def initialize(self, filePath):
""" load data and initialize this class's data: (1) name, (2) attributes, (3) examples """
fin = open(filePath, 'r')
read = [line for line in fin.read().splitlines() if len(line) > 0]
fin.close()
#.........这里部分代码省略.........
示例2: DataSet
# 需要导入模块: from Factory import Factory [as 别名]
# 或者: from Factory.Factory import get [as 别名]
class DataSet(object):
"""
DataSet
Data structure used to load in .gla files, train, and test data using various classifiers.
"""
def __init__(self, fileIn = None):
self.name = None
self.attributes = None
self.examples = None
self.initialize(fileIn = fileIn)
def convert(self, data):
return [self.attributes.get(i).getValue(d.replace('#', '')) for i,d in enumerate(data.split())]
def initialize(self, fileIn = None):
fin = open(fileIn, 'r')
read = [line for line in fin.read().splitlines() if len(line) > 0]
fin.close()
self.attributes = Factory().construct([line for line in read if len(line) > 0 and line[0] == '@'])
self.examples = Factory().construct([line for line in read if len(line) > 0 and line[0] == '#'], self.attributes)
self.name = read[0]
def getName(self):
return self.name
def setName(self, name):
self.name = name
def getAttributeNames(self):
return [self.attributes.get(a).getName() for a in self.attributes.data]
def getAttributeTypes(self):
return [self.attributes.get(a).getType() for a in self.attributes.data]
def getAttribute(self, attribute = None):
return self.attributes.get(attribute)
def getAttributes(self, unit = None):
if unit == 0: return self.attributes.data.keys()
elif unit == 1: return self.attributes.data.values()
else: return self.attributes
def getClasses(self, unit = 1):
return self.attributes.getClassAttribute().getValues()
def getExample(self, n = None):
return self.examples.getExamples(n)
def getExamples(self):
return self.examples
def getExamplesWithValue(self, a, v, c = 0):
"""
a: indicates the attribute index
v: indicates the attribute value
c: indicates the attribute class/label
"""
if a == -1:
return [e.getValue() + [e.getLabel()] for e in self.examples.getExamples(c) if e.getLabel() == v]
return [e.getValue() + [e.getLabel()] for e in self.examples.getExamples(c) if e.getValue(a) == v]
def getType(self, i):
if type(i) == type(str()):
labels = [self.attributes[k].name for k in self.attributes.keys()]
i = labels.index(i)
return self.attributes.get(i).getType()
def isNumeric(self, i):
if self.getType(i) in ['n', 'num', 'number', 'numeric']:
return True
return False
def getSize(self, of = 'a'):
if of in [0, 'a', 'at', 'attr', 'attribute', 'attributes']: return len(self.getAttributes(0))
if of in [1, 'e', 'ex', 'exam', 'example', 'examples']: return len(self.getExamples())
def getValues(self, attribute = None):
return self.attributes.get(attribute).getValues()
def getTrainTestSet(self, p = .6):
examples = self.getExamples()
n = int(len(examples) * p)
s = sample(examples, n)
train = ExampleSet()
tests = ExampleSet()
for example in examples:
if example in s: train.add(example)
elif example not in train: tests.add(example)
return train, tests
def getTrainValidateTestSet(self, p = .6, v = .5):
examples = self.getExamples()
#.........这里部分代码省略.........