Skip to content

Instantly share code, notes, and snippets.

@Haroperi
Last active August 29, 2015 14:06
Show Gist options
  • Save Haroperi/48dbd30b557c585ad287 to your computer and use it in GitHub Desktop.
Save Haroperi/48dbd30b557c585ad287 to your computer and use it in GitHub Desktop.
Diary
SB棟の低層区の市民はエレベータの下のボタンを押しつつも、エレベータが高層区の乗降でしばらく下に来ないと分かるやいなや、階段で一階まで降りる。その結果高層区からエレベータで降りてくる市民は、乗客いない階で足止めを食らう。高層区の市民は自分より下の階にエレベータは不要であると思っているし、また、全ての市民は自分より上の階は崩れればいいと思っている。このような憎しみ合いの結果、だるま落としの上手い巨人でも現れない限り、SB棟は最上階から順に徐々に崩れ、いずれはSB棟を平屋建てにするだろう。

MeCabのユーザー辞書を作るときに引っかかったこと

MeCabのユーザー辞書を作っていて、以下のように言われた。

feature_index.cpp(158) [mmap_.open(modelfile.c_str())] mmap.h(152) [(fd = ::open(filename, flag | O_BINARY)) >= 0] open failed

原因は、ユーザー辞書を作るためのCSVファイル中のカンマが多いことであった。 その他CSVファイルのミスでも、おそらく同じことが起こる。

MeCabのユーザー辞書をUbuntuで作るときに引っかかったこと

Macではできたのに、UbuntuではMeCabでユーザー辞書が作れずに困った。

結論、mecab-dict-index -d で渡すのは、ubuntuでは/usr/share/mecab/dic/ipadicであった。

$ sudo apt-get install mecab mecab-ipadic-utf8
$ mecab -D
filename:	/var/lib/mecab/dic/debian/sys.dic
version:	102
charset:	UTF-8
type:	0
size:	392126
left size:	1316
right size:	1316
$ mecab-config --dicdir
/usr/lib/mecab/dic
$ `mecab-config --libexecdir`/mecab-dict-index -d $(shell mecab-config --dicdir)/ipadic -u dict.dic -f utf-8 -t utf-8 dict.csv
dictionary_compiler.cpp(82) [param.load(DCONF(DICRC))] no such file or directory: /usr/lib/mecab/dic/ipadic/dicrc

そういえばユーザー辞書を作るときは、コンパイルしてインストールされたシステム辞書ではなくて、その元となるファイルが必要であった。 しかたがないので調べたら、「/usr/share/mecab/dic/ipadic」というのが出てきた。 よって以下のようにすれば良いらしい。

mecab-dict-index -d /usr/share/mecab/dic/ipadic ...

Predicting Y for new X using EBEN

EBEN (Empirical Bayesian Elastic Net algorithms) is a great library. However it does not provide function to predict Y for new observation in version 1.1.1. In this diary, we implement and run it.

program

require(EBEN)
data(BASIS)  # 1000 x 481 matrix of explanatory variables
data(y)  # 1000 dimentional vector of dependent variable on BASIS

trainY <- y[1:900]
trainX <- BASIS[1:900,]
newY <- y[901:1000]
newX <- BASIS[901:1000,]

# get model
m <- EBelasticNet.Gaussian(trainX, trainY, lambda=0.5, alpha=0.5)

# define predict function
predict.eben <- function(m, newX) {
  rv <- rep(0, nrow(newX))
  # For each row in m$weight, calc w_j * newX[,j]
  apply(m$weight, MARGIN=1,
        function(row) {
          idx1 <- row[1]
          idx2 <- row[2]
          coef <- row[3]
          if (idx1 == idx2)
            rv <<- rv + coef * newX[,idx1]
          else
            rv <<- rv + coef * (newX[,idx1] * newX[,idx2])  # updated on 10 Sep. 2014
          NULL
        })
  rv <- rv + m$Intercept
  return(rv)
}

# predict
pred <- predict.eben(m, newX)
print(pred)

test

The return value of EBelasticNet.Gaussian contains residual variance. So we can easily confirm the residual variance calculated by predict.eben (we implemented above) equal to the one in the return value of EBelasticNet.Gaussian.

>m <- EBelasticNet.Gaussian(BASIS, y, alpha=0.5, lambda=0.5)
> pred <- predict.eben(m, BASIS)
> m$residVar
[1] 106.6435
> sum((pred-y)**2)/1000
[1] 106.634

RubyでもRみたいにorderをとる

Rのorderは、例えば次のように、ソートした時の本のインデックスを得る関数です。

> order(c(3,1,2))
[1] 2 3 1

これをRubyでやります。

class Array
  def order
    # my original version
    #(0..(self.size-1)).to_a.sort{ |i,j| self[i] <=> self[j] }

    # http://stackoverflow.com/questions/14446181/originals-indexes-of-sorted-elements-in-ruby
    self.map.with_index.sort_by(&:first).map(&:last)
  end
end

# [1,3,2].order #=> [0, 2, 1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment