"converting sparse matrix to arff using awk" Code Answer

2

i've no idea what arff is (nor do i need to know to help you transpose your text to a different format) so let's start with this:

$ cat tst.awk
begin{ fs="t" }
nr==1 { printf "@relation '%s'n", filename }
{
    row = $1
    attr = $2

    if (!seenrow[row]++) {
        rows[++numrows] = row
    }

    if (!seenattr[attr]++) {
        printf "@attribute "%s" stringn", attr
        attrs[++numattrs] = attr
    }

    score[row,attr] = $3
}
end {
    print "nn@data"
    for (rownr=1; rownr<=numrows; rownr++) {
        row = rows[rownr]
        for (attrnr=1;attrnr<=numattrs;attrnr++)  {
            attr = attrs[attrnr]
            printf "%d,", score[row,attr]
        }
        print row
    }
}
$
$ cat file
church  place   3
church  institution     6
man     place   86
man     food    63
woman   book    37
$
$ awk -f tst.awk file
@relation 'file'
@attribute "place" string
@attribute "institution" string
@attribute "food" string
@attribute "book" string


@data
3,6,0,0,church
86,0,63,0,man
0,0,0,37,woman

now, tell us what's wrong with that and we can go from there.

By Pranav MS on September 3 2022

Answers related to “converting sparse matrix to arff using awk”

Only authorized users can answer the Search term. Please sign in first, or register a free account.