我有一个稀疏矩阵.txt文件,包含许多数字,但大多数是0' s。以下是此.txt文件中的示例:
0 0 0.271178 0 0 0 0.538776 0 -0.631228 0 0 -0.501485 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.438643 0 0 0 0 0 0.287754 0 0 0 0 0 0 0 0 0 0 0 0 -0.498918 0 0 0 0 0 0.475561 0 0 0 0 0 0 0 0 0 0 0 0.370479 0 -0.300765 0 -1.10987 0 0 0.163637 0 0 0 0 0 0 0.304006 0 0.181697 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.261758 0 0 0 0.153415 0.17412 0 -0.129725 0 0.17598 0 0 0 0 0 0 0 0.56053 0 0 -0.211302 0 0 0 0 0 0 0 0.213277 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.376654 0 0 -0.604655 0 0 0 0 0 0.301025 0 0 -0.431324 0 0 -0.139445 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.404718 0.610013 -0.286108 0 0.46045 0 0 0 0 0 0 -0.193543 0 0 0 0 0 0 0 0.315063 0 0 -0.285598 0 0 0.206607 0
我需要做的是能够浏览此文件并将数据转换为WEKA's 可识别的文件格式,.arff,所以我可以在数据集上运行数据挖掘算法。有谁知道如何做到这一点?我在StackOverflow上发现了一些与我的问题类似的问题,但是,他们使用的是AWK语言中的脚本,之前从未使用过.awk程序,所以我无法让这些程序正常运行。 以下是这些帖子的链接: 1 2
答案 0 :(得分:1)
查看the ARFF file format以及您关联的问题。
你可以很容易地在shell脚本中嵌入awk程序,所以你可以这样做:
#!/bin/sh
file=$1
awk '
BEGIN {print "@RELATION something\n"}
{
data = sep = ""
for (i=1; i<=NF; i++) {
if ($i != 0) {
printf "@ATTRIBUTE a_%d NUMERIC\n", i
data = data sep $i
sep = ","
}
print ""
print "@DATA"
print data
}
' "$file"
使用以下代码调用脚本:bash script.sh data_file
并查看
@RELATION something
@ATTRIBUTE a_3 NUMERIC
@ATTRIBUTE a_7 NUMERIC
@ATTRIBUTE a_9 NUMERIC
@ATTRIBUTE a_12 NUMERIC
@ATTRIBUTE a_33 NUMERIC
@ATTRIBUTE a_39 NUMERIC
@ATTRIBUTE a_52 NUMERIC
@ATTRIBUTE a_58 NUMERIC
@ATTRIBUTE a_70 NUMERIC
@ATTRIBUTE a_72 NUMERIC
@ATTRIBUTE a_74 NUMERIC
@ATTRIBUTE a_77 NUMERIC
@ATTRIBUTE a_84 NUMERIC
@ATTRIBUTE a_86 NUMERIC
@ATTRIBUTE a_124 NUMERIC
@ATTRIBUTE a_128 NUMERIC
@ATTRIBUTE a_129 NUMERIC
@ATTRIBUTE a_131 NUMERIC
@ATTRIBUTE a_133 NUMERIC
@ATTRIBUTE a_141 NUMERIC
@ATTRIBUTE a_144 NUMERIC
@ATTRIBUTE a_152 NUMERIC
@ATTRIBUTE a_185 NUMERIC
@ATTRIBUTE a_189 NUMERIC
@ATTRIBUTE a_207 NUMERIC
@ATTRIBUTE a_210 NUMERIC
@ATTRIBUTE a_216 NUMERIC
@ATTRIBUTE a_219 NUMERIC
@ATTRIBUTE a_222 NUMERIC
@ATTRIBUTE a_238 NUMERIC
@ATTRIBUTE a_239 NUMERIC
@ATTRIBUTE a_240 NUMERIC
@ATTRIBUTE a_242 NUMERIC
@ATTRIBUTE a_249 NUMERIC
@ATTRIBUTE a_257 NUMERIC
@ATTRIBUTE a_260 NUMERIC
@ATTRIBUTE a_263 NUMERIC
@DATA
0.271178,0.538776,-0.631228,-0.501485,0.438643,0.287754,-0.498918,0.475561,0.370479,-0.300765,-1.10987,0.163637,0.304006,0.181697,-0.261758,0.153415,0.17412,-0.129725,0.17598,0.56053,-0.211302,0.213277,0.0698458,0.661972,0.376654,-0.604655,0.301025,-0.431324,-0.139445,0.404718,0.610013,-0.286108,0.46045,-0.193543,0.315063,-0.285598,0.206607
但如果您想要输出,那么您必须告诉我们。
答案 1 :(得分:0)
很棒的脚本Glenn,但它缺少索引并将其放在属性名称中。类似的东西:
@attribute a_0 numeric
@attribute a_1 numeric
@attribute a_2 numeric
@attribute a_3 numeric
...
@data
{3 0.271178,...}
这是避免遗漏属性位置信息所需的稀疏ARFF格式。我的建议是使用密集格式(只是CSV)并使用WEKA过滤器NonSparseToSparse
。