Converting sparse matrix to ARFF using awk

人盡茶涼 提交于 2019-12-01 14:40:27

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\" string\n", attr
        attrs[++numAttrs] = attr
    }

    score[row,attr] = $3
}
END {
    print "\n\n@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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!