本文整理汇总了Python中Factory.Factory.add方法的典型用法代码示例。如果您正苦于以下问题:Python Factory.add方法的具体用法?Python Factory.add怎么用?Python Factory.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Factory.Factory
的用法示例。
在下文中一共展示了Factory.add方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DataSet
# 需要导入模块: from Factory import Factory [as 别名]
# 或者: from Factory.Factory import add [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()
#.........这里部分代码省略.........