Skip to content

Instantly share code, notes, and snippets.

@cympfh
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cympfh/de22da300bdb9a1293ed to your computer and use it in GitHub Desktop.
Save cympfh/de22da300bdb9a1293ed to your computer and use it in GitHub Desktop.
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:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment