Meteor コマンドあれこれ。
Meteorで遊んでいます。
http://www.meteor.com/
コマンドをいくつか覚えたので、メモしておきます。
アップデート
% meteor update
Bootstrapをデフォルトのスタイルにする
% meteor add bootstrap
Ubuntu 11.10から12.04へ
アップグレード後、/etc/resolve.confが空になってしまうので次のように修正。
% sudo cp /etc/resolvconf/resolv.conf.d/original /etc/resolveconf/resolv.conf.d/base % sudo resolvconf -u
名前が引けるかどうか確認して、終了。
Java7のインストール on Ubuntu
http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html#javasejdk からダウンロードして、
/opt/local/share 以下に展開した後にやったことの記録。
update-alternativeで使用するJavaのバージョンを切り替えられるようにした。
% cd /opt/local/share/jdk1.7.0_03/bin % foreach i (`ls *`) sudo update-alternatives --install /usr/bin/$i $i /opt/local/share/jdk1.7.0_03/bin/$i 20 end
これで切り替えられるようになった。
% sudo update-alternatives --config java There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
今日のRuby
コサイン尺度を求めるメソッドをナイーブに実装。
def cosin(array1, array2) p = pair(array1, array2) z = zero(array1.size) return p.map{|i| i[0] * i[1]}.reduce{|i,j| i+j}.to_f / euclidean(array1, z).to_f / euclidean(array2, z).to_f end private def zero(size) return Array.new(size, 0) end
zeroはゼロベクトルを返します。
今日のRubyその2
距離関数を作ったので、近傍を求めるクラスを作ってみた。ナイーブである。
class Neighborhood def initialize(items, matric) raise unless matric.is_a?(Proc) || matric.is_a?(Method) require 'matrix' unless defined?(Matrix) items = items.to_a if items.is_a?(Matrix) @items = items @matric = matric end def nearest(item) return knn(item, 1) end def knn(item, k) raise unless k > 0 && k < item.size d = to_distance(item) result = [] k.times{ i = d.index(d.min) result << i d.delete_at(i) } return result end private def to_distance(item) raise unless @items.all?{|i| i.size == @items.size} matric = @default_matric unless matric return @items.map{|i| @matric.call(item, i) } end end
探索対象群と距離関数をあたえて初期化、その後近傍を求める。
テストケースはこういう感じ。図らずも上記のMatric.city_blockが正しいことがわかった:)
require 'test/unit' require File.join(File.dirname(__FILE__), "matric") class TestNeighborhood < Test::Unit::TestCase include Matric def setup @items = [ [0, 0, 0, 0], [0, 1, 1, 1], [1, 1, 1, 0], [0, 1, 0, 1] ] @item = [1, 0, 1, 0] @distance = [2.0, 3.0, 1.0, 4.0] @nearest = 2 end def test_neighborhood n = Neighborhood.new(@items, Matric.method(:city_block)) assert(n.nearest(@item).shift == @nearest) two = n.knn(@item, 2) assert(two[1] == 0) end end