如何使用python加载libsvm文件(填充缺少的值)

2024-09-28 03:20:42 发布

您现在位置:Python中文网/ 问答频道 /正文

设v=[10:1,15:2,20:3];(i=[0,1,2]列表中值的索引)

  • n = 10 - (i + 1) = 10 - (0 + 1) = 9 ==> newV = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  • n = 15 - (9 + i + 1) = 15 - (9 + 1 + 1) = 4 ==> newV =[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2]
  • n = 20 - ( 9 + 4 + 2 + 1 ) = 20 - 16 = 4 ==> newV =[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3]

finalV = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3]`

^{pr2}$

谢谢你的铅笔和纸。在


Tags: 列表铅笔pr2newvfinalv
1条回答
网友
1楼 · 发布于 2024-09-28 03:20:42

你的问题有些不清楚的地方,但这里有一个可能的解决办法

for line in open("file.txt", 'r').readlines():
    line = line.strip().split(" ")
    lineName = line.pop(0)
    arr = [0.0] * int(line[-1].split(":")[0])  # fills the array with zeros

    for element in line:
        index,value = element.split(":")
        arr[int(index)-1] = float(value)

    print lineName + " " + " ".join(map(str, arr))  # /questions/6507431

相关问题 更多 >

    热门问题