Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
SVMlightの特徴量の重み
#!/usr/bin/env coffee
# 特徴量の重みを見る
# [LIBSVMの特徴量の重みを見る - LIBSVMのモデルの読み方 - 唯物是真 @Scaled_Wurm](http://sucrose.hatenablog.com/entry/2013/07/10/223952)
#
# SVM^{light} の場合
#
fs = require 'fs'
model = process.argv[2]
unless model
console.warn 'coffee ./% <model-file-by-svmlight>'
process.exit 0
fs.readFile model, 'utf8', (err, data) ->
throw err if err
ls = data.trim().split '\n'
len = ls.length
k = 0 # SV line
for i in [0 ... 40] # 高々40行目までにあるはず
if ls[i].indexOf('is a SV') isnt -1
k = i + 1
break
weight = {}
add = (id, x) ->
if weight[id]
weight[id] += x
else
weight[id] = x
for i in [k ... len]
xs = ls[k].split ' '
coef = +xs[0]
m = xs.length
for j in [1 ... m]
continue if xs[j] is '#'
[id, val] = xs[j].split ':' # id:value
add id, coef * (+val)
items = for id of weight
{ id: id | 0, w: Math.abs weight[id] }
items.sort (a, b) -> b.w - a.w
console.dir items
# vim: set ft=coffee: