-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistics_by_features.rb
More file actions
29 lines (24 loc) · 903 Bytes
/
statistics_by_features.rb
File metadata and controls
29 lines (24 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require './libsvmdata_reader.rb'
require './average_calculator.rb'
ARGV.each do |filename|
sum = Hash.new{ |hash, key| hash[key] = 0.0 }
squared_sum = Hash.new{ |hash, key| hash[key] = 0.0 }
num_instances = 0
read_libsvm_file(filename) do |label, instance_hash|
instance_hash.each do |key, val|
sum[key] += val
squared_sum[key] += val * val
end
num_instances += 1
STDERR.puts "#{filename}: Read #{num_instances} instances." if num_instances % 10000 == 0
end
STDERR.puts "#{filename}: Read #{num_instances} instances." if num_instances % 10000 != 0
open("#{filename}.info", "w") do |infofile|
sum.keys.sort.each do |key|
average = sum[key] / num_instances
infofile.puts "#{key} #{average} #{Math.sqrt(squared_sum[key] / num_instances - average**2)}"
end
end
end