本文整理汇总了Python中mvpa2.misc.attrmap.AttributeMap.keys方法的典型用法代码示例。如果您正苦于以下问题:Python AttributeMap.keys方法的具体用法?Python AttributeMap.keys怎么用?Python AttributeMap.keys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mvpa2.misc.attrmap.AttributeMap
的用法示例。
在下文中一共展示了AttributeMap.keys方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SVM
# 需要导入模块: from mvpa2.misc.attrmap import AttributeMap [as 别名]
# 或者: from mvpa2.misc.attrmap.AttributeMap import keys [as 别名]
#.........这里部分代码省略.........
self.__svm.set_kernel(self.__kernel_test)
# doesn't do any good imho although on unittests helps tiny bit... hm
#self.__svm.init_kernel_optimization()
values_ = self.__svm_apply()
else:
testdata_sg = _tosg(dataset.samples)
self.__svm.set_features(testdata_sg)
values_ = self.__svm_apply()
if __debug__:
debug("SG_", "Classifying testing data")
if values_ is None:
raise RuntimeError, "We got empty list of values from %s" % self
values = values_.get_labels()
if retrainable:
# we must assign it only if it is retrainable
self.ca.repredicted = repredicted = not changed_testdata
if __debug__:
debug("SG__", "Re-assigning learing kernel. Repredicted is %s"
% repredicted)
# return back original kernel
if 'kernel-based' in self.__tags__:
self.__svm.set_kernel(self.__kernel)
if __debug__:
debug("SG__", "Got values %s" % values)
if (self.__is_regression__):
predictions = values
else:
if len(self._attrmap.keys()) == 2:
predictions = np.sign(values)
# since np.sign(0) == 0
predictions[predictions==0] = 1
else:
predictions = values
# remap labels back adjusting their type
# XXX YOH: This is done by topclass now (needs RF)
#predictions = self._attrmap.to_literal(predictions)
if __debug__:
debug("SG__", "Tuned predictions %s" % predictions)
# store conditional attribute
# TODO: extract values properly for multiclass SVMs --
# ie 1 value per label or pairs for all 1-vs-1 classifications
self.ca.estimates = values
## to avoid leaks with not yet properly fixed shogun
if not retrainable:
try:
testdata.free_features()
except:
pass
return predictions
def _untrain(self):
super(SVM, self)._untrain()
# untrain/clean the kernel -- we might not allow to drag SWIG
# instance around BUT XXX -- make it work fine with
示例2: to_lightsvm_format
# 需要导入模块: from mvpa2.misc.attrmap import AttributeMap [as 别名]
# 或者: from mvpa2.misc.attrmap.AttributeMap import keys [as 别名]
def to_lightsvm_format(dataset, out, targets_attr='targets',
domain=None, am=None):
"""Export dataset into LightSVM format
Parameters
----------
dataset : Dataset
out
Anything understanding .write(string), such as `File`
targets_attr : string, optional
Name of the samples attribute to be output
domain : {None, 'regression', 'binary', 'multiclass'}, optional
What domain dataset belongs to. If `None`, it would be deduced
depending on the datatype ('regression' if float, classification
in case of int or string, with 'binary'/'multiclass' depending on
the number of unique targets)
am : `AttributeMap` or None, optional
Which mapping to use for storing the non-conformant targets. If
None was provided, new one would be automagically generated
depending on the given/deduced domain.
Returns
-------
am
LightSVM format is an ASCII representation with a single sample per
each line::
output featureIndex:featureValue ... featureIndex:featureValue
where ``output`` is specific for a given domain:
regression
float number
binary
integer labels from {-1, 1}
multiclass
integer labels from {1..ds.targets_attr.nunique}
"""
targets_a = dataset.sa[targets_attr]
targets = targets_a.value
# XXX this all below
# * might become cleaner
# * might be RF to become more generic to be used may be elsewhere as well
if domain is None:
if targets.dtype.kind in ['S', 'i']:
if len(targets_a.unique) == 2:
domain = 'binary'
else:
domain = 'multiclass'
else:
domain = 'regression'
if domain in ['multiclass', 'binary']:
# check if labels are appropriate and provide mapping if necessary
utargets = targets_a.unique
if domain == 'binary' and set(utargets) != set([-1, 1]):
# need mapping
if len(utargets) != 2:
raise ValueError, \
"We need 2 unique targets in %s of %s. Got targets " \
"from set %s" % (targets_attr, dataset, utargets)
if am is None:
am = AttributeMap(dict(zip(utargets, [-1, 1])))
elif set(am.keys()) != set([-1, 1]):
raise ValueError, \
"Provided %s doesn't map into binary " \
"labels -1,+1" % (am,)
elif domain == 'multiclass' \
and set(utargets) != set(range(1, len(utargets)+1)):
if am is None:
am = AttributeMap(dict(zip(utargets,
range(1, len(utargets) + 1))))
elif set(am.keys()) != set([-1, 1]):
raise ValueError, \
"Provided %s doesn't map into multiclass " \
"range 1..N" % (am, )
if am is not None:
# map the targets
targets = am.to_numeric(targets)
for t, s in zip(targets, dataset.samples):
out.write('%g %s\n'
% (t,
' '.join(
'%i:%.8g' % (i, v)
for i,v in zip(range(1, dataset.nfeatures+1), s))))
out.flush() # push it out
return am