Orange Forum • View topic - create table with sparse data

create table with sparse data

A place to ask questions about methods in Orange and how they are used and other general support.

create table with sparse data

Postby alexc » Wed Jul 18, 2012 6:57

Hi,

If I already have sparse data loaded in memory as a list-of-list, how can I construct a table using it?
for example, given
d = [['a', 'b'], ['e', 'f', 'g']]
how to create table something like:
data = Orange.data.Table(d)

I know I can create a basket file that contains:
a, b
e, f, g

and create a table using it:
data = Orange.data.Table('mydata.basket')

however, I would like to create the table directly from memory without using a file. how can i do that?
thanks.

-alex

Re: create table with sparse data

Postby Ales » Wed Jul 18, 2012 11:15

There is no simple way to do this. You will have to construct the domain and data table yourself. Use something like
Code: Select all
d = [['a', 'b'], ['e', 'f', 'g']]
vars = {}
def var_construct(name):
    if name not in vars:
        v = Orange.feature.Continuous(name)
        mid = Orange.feature.Descriptor.new_meta_id()
        domain.add_meta(mid, v)
        vars[name] = v
    return vars[name]

domain = Orange.data.Domain([])
table = Orange.data.Table(domain)

for inst in d:
    inst_vars = map(var_construct, inst)
    new_instance = Orange.data.Instance(domain)
    for v in inst_vars:
        new_instance[v] = 1.0
    table.append(new_instance)

Re: create table with sparse data

Postby alexc » Thu Jul 19, 2012 4:55

Thanks for your help. However, there seems to be a problem.
The code you provided will create two instances:
[], {"a":1.000, "b":1.000}
[], {"a":?, "b":?, "e":1.000, "f":1.000, "g":1.000}

the instances created using basket file are different:
[], {"a":1.000, "b":1.000}
[], {"a":1.000, "c":1.000}

Any idea how to remove the "a":? and "b":? elements from the second instance?
thanks.

Re: create table with sparse data

Postby Ales » Thu Jul 19, 2012 12:46

Replace
Code: Select all
domain.add_meta(mid, v)
with
Code: Select all
domain.add_meta(mid, v, 1)

This marks the meta attributes as optional.

Re: create table with sparse data

Postby alexc » Thu Jul 19, 2012 18:31

That fixed the problem. Thanks a lot!


Return to Questions & Support