本文整理汇总了Python中Orange.data.DiscreteVariable类的典型用法代码示例。如果您正苦于以下问题:Python DiscreteVariable类的具体用法?Python DiscreteVariable怎么用?Python DiscreteVariable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DiscreteVariable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_colors
def test_colors(self):
var = DiscreteVariable.make("a", values=["F", "M"])
self.assertIsNone(var._colors)
self.assertEqual(var.colors.shape, (2, 3))
self.assertIs(var._colors, var.colors)
self.assertEqual(var.colors.shape, (2, 3))
self.assertFalse(var.colors.flags.writeable)
var.colors = np.arange(6).reshape((2, 3))
np.testing.assert_almost_equal(var.colors, [[0, 1, 2], [3, 4, 5]])
self.assertFalse(var.colors.flags.writeable)
with self.assertRaises(ValueError):
var.colors[0] = [42, 41, 40]
var.set_color(0, [42, 41, 40])
np.testing.assert_almost_equal(var.colors, [[42, 41, 40], [3, 4, 5]])
var = DiscreteVariable.make("x", values=["A", "B"])
var.attributes["colors"] = ['#0a0b0c', '#0d0e0f']
np.testing.assert_almost_equal(var.colors, [[10, 11, 12], [13, 14, 15]])
# Test ncolors adapts to nvalues
var = DiscreteVariable.make('foo', values=['d', 'r'])
self.assertEqual(len(var.colors), 2)
var.add_value('e')
self.assertEqual(len(var.colors), 3)
user_defined = (0, 0, 0)
var.set_color(2, user_defined)
var.add_value('k')
self.assertEqual(len(var.colors), 4)
np.testing.assert_array_equal(var.colors[2], user_defined)
示例2: test_make_proxy_disc
def test_make_proxy_disc(self):
abc = DiscreteVariable("abc", values="abc", ordered=True)
abc1 = abc.make_proxy()
abc2 = abc1.make_proxy()
self.assertIs(abc.master, abc)
self.assertIs(abc1.master, abc)
self.assertIs(abc2.master, abc)
self.assertEqual(abc, abc1)
self.assertEqual(abc, abc2)
self.assertEqual(abc1, abc2)
self.assertEqual(hash(abc), hash(abc1))
self.assertEqual(hash(abc1), hash(abc2))
abcx = DiscreteVariable("abc", values="abc", ordered=True)
self.assertNotEqual(abc, abcx)
abc1p = pickle.loads(pickle.dumps(abc1))
self.assertIs(abc1p.master, abc)
self.assertEqual(abc1p, abc)
abcp, abc1p, abc2p = pickle.loads(pickle.dumps((abc, abc1, abc2)))
self.assertIs(abcp.master, abcp.master)
self.assertIs(abc1p.master, abcp.master)
self.assertIs(abc2p.master, abcp.master)
self.assertEqual(abcp, abc1p)
self.assertEqual(abcp, abc2p)
self.assertEqual(abc1p, abc2p)
示例3: test_repr
def test_repr(self):
var = DiscreteVariable.make("a", values=["F", "M"])
self.assertEqual(
repr(var),
"DiscreteVariable(name='a', values=['F', 'M'])")
var.ordered = True
self.assertEqual(
repr(var),
"DiscreteVariable(name='a', values=['F', 'M'], ordered=True)")
var = DiscreteVariable.make("a", values="1234567")
self.assertEqual(
repr(var),
"DiscreteVariable(name='a', values=['1', '2', '3', '4', '5', '6', '7'])")
示例4: test_copy_descriptor_discrete
def test_copy_descriptor_discrete(self):
var = DiscreteVariable("foo", values=list("abc"), ordered=True)
var.attributes = {"bar": 42, "baz": 13}
copied = copy_descriptor(var)
self.assertIsInstance(copied, DiscreteVariable)
self.assertEqual(copied.name, "foo")
self.assertEqual(list(copied.values), list("abc"))
self.assertTrue(copied.ordered)
self.assertEqual(copied.attributes, var.attributes)
self.assertIsNot(copied.attributes, var.attributes)
var = DiscreteVariable("foo", values=list("abc"), ordered=False)
copied = copy_descriptor(var, "cux")
self.assertEqual(copied.name, "cux")
self.assertFalse(copied.ordered)
示例5: test_unpickle
def test_unpickle(self):
d1 = DiscreteVariable("A", values=["two", "one"])
s = pickle.dumps(d1)
d2 = DiscreteVariable.make("A", values=["one", "two", "three"])
d2_values = tuple(d2.values)
d1c = pickle.loads(s)
# See: gh-3238
# The unpickle reconstruction picks an existing variable (d2), on which
# __setstate__ or __dict__.update is called
self.assertSequenceEqual(d2.values, d2_values)
self.assertSequenceEqual(d1c.values, d1.values)
s = pickle.dumps(d2)
DiscreteVariable._clear_all_caches() # [comment redacted]
d1 = DiscreteVariable("A", values=["one", "two"])
d2 = pickle.loads(s)
self.assertSequenceEqual(d2.values, ["two", "one", "three"])
示例6: _create_corpus
def _create_corpus(self):
corpus = None
names = ["name", "path", "content"]
data = []
category_data = []
text_categories = list(set(t.category for t in self._text_data))
values = list(set(text_categories))
category_var = DiscreteVariable.make("category", values=values)
for textdata in self._text_data:
data.append(
[textdata.name,
textdata.path,
textdata.content]
)
category_data.append(category_var.to_val(textdata.category))
if len(text_categories) > 1:
category_data = np.array(category_data)
else:
category_var = []
category_data = np.empty((len(data), 0))
domain = Domain(
[], category_var, [StringVariable.make(name) for name in names]
)
domain["name"].attributes["title"] = True
data = np.array(data, dtype=object)
if len(data):
corpus = Corpus(domain,
Y=category_data,
metas=data,
text_features=[domain.metas[2]])
return corpus
示例7: create_discretized_var
def create_discretized_var(cls, var, points):
lpoints = list(points)
if lpoints:
values = [
cls._fmt_interval(low, high, var.number_of_decimals)
for low, high in zip([-np.inf] + lpoints, lpoints + [np.inf])]
to_sql = BinSql(var, lpoints)
else:
values = ["single_value"]
to_sql = SingleValueSql(values[0])
dvar = DiscreteVariable(name=var.name, values=values,
compute_value=cls(var, points))
dvar.source_variable = var
dvar.to_sql = to_sql
return dvar
示例8: extend_attributes
def extend_attributes(self, X, feature_names, feature_values=None,
compute_values=None, var_attrs=None, sparse=False):
"""
Append features to corpus. If `feature_values` argument is present,
features will be Discrete else Continuous.
Args:
X (numpy.ndarray or scipy.sparse.csr_matrix): Features values to append
feature_names (list): List of string containing feature names
feature_values (list): A list of possible values for Discrete features.
compute_values (list): Compute values for corresponding features.
var_attrs (dict): Additional attributes appended to variable.attributes.
sparse (bool): Whether the features should be marked as sparse.
"""
if self.X.size == 0:
self.X = X
elif sp.issparse(self.X) or sp.issparse(X):
self.X = sp.hstack((self.X, X)).tocsr()
else:
self.X = np.hstack((self.X, X))
if compute_values is None:
compute_values = [None] * X.shape[1]
if feature_values is None:
feature_values = [None] * X.shape[1]
new_attr = self.domain.attributes
for f, values, cv in zip(feature_names, feature_values, compute_values):
if values is not None:
var = DiscreteVariable(f, values=values, compute_value=cv)
else:
var = ContinuousVariable(f, compute_value=cv)
var.sparse = sparse # don't pass this to constructor so this works with Orange < 3.8.0
if cv is not None: # set original variable for cv
cv.variable = var
if isinstance(var_attrs, dict):
var.attributes.update(var_attrs)
new_attr += (var, )
new_domain = Domain(
attributes=new_attr,
class_vars=self.domain.class_vars,
metas=self.domain.metas
)
self.domain = new_domain
示例9: test_colors
def test_colors(self):
var = DiscreteVariable.make("a", values=["F", "M"])
self.assertIsNone(var._colors)
self.assertEqual(var.colors.shape, (2, 3))
self.assertIs(var._colors, var.colors)
self.assertEqual(var.colors.shape, (2, 3))
self.assertFalse(var.colors.flags.writeable)
var.colors = np.arange(6).reshape((2, 3))
np.testing.assert_almost_equal(var.colors, [[0, 1, 2], [3, 4, 5]])
self.assertFalse(var.colors.flags.writeable)
with self.assertRaises(ValueError):
var.colors[0] = [42, 41, 40]
var.set_color(0, [42, 41, 40])
np.testing.assert_almost_equal(var.colors, [[42, 41, 40], [3, 4, 5]])
var = DiscreteVariable.make("x", values=["A", "B"])
var.attributes["colors"] = ['#0a0b0c', '#0d0e0f']
np.testing.assert_almost_equal(var.colors, [[10, 11, 12], [13, 14, 15]])
示例10: test_repr
def test_repr(self):
var = DiscreteVariable.make("a", values=["F", "M"])
self.assertEqual(
repr(var),
"DiscreteVariable('a', values=['F', 'M'])")
var.base_value = 1
self.assertEqual(
repr(var),
"DiscreteVariable('a', values=['F', 'M'], base_value=1)")
var.ordered = True
self.assertEqual(
repr(var),
"DiscreteVariable('a', values=['F', 'M'], "
"ordered=True, base_value=1)")
var = DiscreteVariable.make("a", values="1234567")
self.assertEqual(
repr(var),
"DiscreteVariable('a', values=['1', '2', '3', '4', '5', ...])")
示例11: create_discretized_var
def create_discretized_var(cls, var, points):
lpoints = list(points)
if points:
values = [
cls._fmt_interval(low, high, var.number_of_decimals)
for low, high in zip([-np.inf] + lpoints, lpoints + [np.inf])]
def discretized_attribute():
return 'bin(%s, ARRAY%s)' % (var.to_sql(), str(lpoints))
else:
values = ["single_value"]
def discretized_attribute():
return "'%s'" % values[0]
dvar = DiscreteVariable(name="D_" + var.name, values=values)
dvar.compute_value = cls(var, points)
dvar.source_variable = var
dvar.to_sql = discretized_attribute
return dvar
示例12: _guess_variable
def _guess_variable(self, field_name, field_metadata, inspect_table):
type_code = field_metadata[0]
FLOATISH_TYPES = (700, 701, 1700) # real, float8, numeric
INT_TYPES = (20, 21, 23) # bigint, int, smallint
CHAR_TYPES = (25, 1042, 1043,) # text, char, varchar
BOOLEAN_TYPES = (16,) # bool
DATE_TYPES = (1082, 1114, 1184, ) # date, timestamp, timestamptz
# time, timestamp, timestamptz, timetz
TIME_TYPES = (1083, 1114, 1184, 1266,)
if type_code in FLOATISH_TYPES:
return ContinuousVariable.make(field_name)
if type_code in TIME_TYPES + DATE_TYPES:
tv = TimeVariable.make(field_name)
tv.have_date |= type_code in DATE_TYPES
tv.have_time |= type_code in TIME_TYPES
return tv
if type_code in INT_TYPES: # bigint, int, smallint
if inspect_table:
values = self.get_distinct_values(field_name, inspect_table)
if values:
return DiscreteVariable.make(field_name, values)
return ContinuousVariable.make(field_name)
if type_code in BOOLEAN_TYPES:
return DiscreteVariable.make(field_name, ['false', 'true'])
if type_code in CHAR_TYPES:
if inspect_table:
values = self.get_distinct_values(field_name, inspect_table)
# remove trailing spaces
values = [v.rstrip() for v in values]
if values:
return DiscreteVariable.make(field_name, values)
return StringVariable.make(field_name)
示例13: test_to_val
def test_to_val(self):
values = ["F", "M"]
var = DiscreteVariable(name="Feature 0", values=values)
self.assertEqual(var.to_val(0), 0)
self.assertEqual(var.to_val("F"), 0)
self.assertEqual(var.to_val(0.), 0)
self.assertTrue(math.isnan(var.to_val("?")))
# TODO: with self.assertRaises(ValueError): var.to_val(2)
with self.assertRaises(ValueError):
var.to_val("G")
示例14: test_find_compatible_ordered
def test_find_compatible_ordered(self):
abc = DiscreteVariable("abc", values="abc", ordered=True)
find_comp = DiscreteVariable._find_compatible
self.assertIsNone(find_comp("abc"))
self.assertIsNone(find_comp("abc", list("abc")))
self.assertIs(find_comp("abc", ordered=True), abc)
self.assertIs(find_comp("abc", ["a"], ordered=True), abc)
self.assertIs(find_comp("abc", ["a", "b"], ordered=True), abc)
self.assertIs(find_comp("abc", ["a", "b", "c"], ordered=True), abc)
self.assertIs(find_comp("abc", ["a", "b", "c", "d"], ordered=True), abc)
abd = DiscreteVariable.make(
"abc", values=["a", "d", "b"], ordered=True)
self.assertIsNot(abc, abd)
abc_un = DiscreteVariable.make("abc", values=["a", "b", "c"])
self.assertIsNot(abc_un, abc)
self.assertIs(
find_comp("abc", values=["a", "d", "b"], ordered=True), abd)
self.assertIs(find_comp("abc", values=["a", "b", "c"]), abc_un)
示例15: test_val_from_str
def test_val_from_str(self):
var = DiscreteVariable.make("a", values=["F", "M"])
self.assertTrue(math.isnan(var.to_val(None)))
self.assertEqual(var.to_val(1), 1)