Search code examples
rrecommendation-engine

Convert from character file in R to .libfm format


I have a sparse matrix in R. Using package libFMexe I can convert the matrix to the correct format for libFM. I get

head(libFMmat)
[1] "1 0:1 5000:1 33736:1 33737:1 33738:1 33739:1 33740:1 33741:1 
33742:1 33743:1 33744:1 33745:1 33746:1 33747:1"
[2] "1 0:1 5001:1 33735:1 33737:1 33738:1 33739:1 33740:1 33741:1 
33742:1 33743:1 33744:1 33745:1 33746:1 33747:1"
[3] "1 0:1 5002:1 33735:1 33736:1 33738:1 33739:1 33740:1 33741:1 
33742:1 33743:1 33744:1 33745:1 33746:1 33747:1"
[4] "1 0:1 5003:1 33735:1 33736:1 33737:1 33739:1 33740:1 33741:1 
33742:1 33743:1 33744:1 33745:1 33746:1 33747:1"
[5] "1 0:1 5004:1 33735:1 33736:1 33737:1 33738:1 33740:1 33741:1 
33742:1 33743:1 33744:1 33745:1 33746:1 33747:1"
[6] "1 0:1 5005:1 33735:1 33736:1 33737:1 33738:1 33739:1 33741:1 
33742:1 33743:1 33744:1 33745:1 33746:1 33747:1"

which is correct according to my data and in the format I want it. Now I want to use libFM on this data.

First I tried actually using

libFM(libFMmat_train, libFMmat_test)

but I get

sh: libFM: command not found
Error in system(libfm_exe, intern = TRUE) : error in running command

so now I am trying to do it in my terminal. I use

save(libFMmat_train, file= "/.../libFMmat_train.libfm")

but when I run libFM on this data I get

Loading train...    
has x = 0
has xt = 1

ERROR: cannot parse line "??=???U??;p;?x"?k??Kb??X??? ?ƭV08?P0r?
@ոS.$%F$JZ(i??) I73?f?D4?qG?lG????[????5?n??<??{??ֵ?u]????ӿ????'>???o???
w????c?ϻO??O??????_~??????W?̡?,???:??}???|???7~??????Q/?C?(???̣ƴ>K?a?
ϔ???)?g?u???a?????ߕ9??}
            ??;?????qL??>???s???????_l???_??\Kܿ???s5?i?[cl??1?_W???5?
>???u???ge__(??ʈQ????c?y?}?u?????f??b֟co?{ZJs???h=?V?{??L?????ܟ?<????j??
q??_??\R,?S????^???ܿ>??????}????^rߟ???Q?:?^_???K\_??{???q??????3??????
R.a??^uaIXS?_X???1?bϽ??????????~o}??^????????N~t}a?:?iؿ?k???Ӿ???s??Y???
{J)v=?0????K?z?m
}???:???H9??,|?s}??????5??<?cZ?Y???????om???=???Ǖί?1???cLC????+k!?9???
ޖJ~[|oW??"?ף?u!?Ԧ????u????h?Xߐs?" at character 

So my question is - how do I save an R character file in the correct format as a .libfm file so that I can use libFM?


Solution

  • I solved it myself, the file was already in the correct text format, and it did not need to be converted to a .libfm format. So I simply converted my character vector libFMmat to libFM format using (found in another stackoverflow thread)

    fileConn <- file("output.txt")
    writeLines(libFMmat, fileConn)
    close(fileConn)
    

    to get a .txt file which could be used with libFM.