apache arrowのrubyバインディングをgobject introspectionで
TRANSCRIPT
![Page 1: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/1.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
Apache ArrowのRubyバインディングを
GObject Introspectionで
須藤功平株式会社クリアコード
名古屋Ruby会議032017-02-11
![Page 2: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/2.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
内容
まくら1.
本題2.
オチ3.
![Page 3: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/3.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
やりたいこと
Rubyでデータ分析
![Page 4: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/4.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
データ分析?
いろいろある
![Page 5: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/5.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
やりたいデータ分析
全文検索関連
同義語・関連語の自動抽出例:整数とIntegerは同じ
✓
最近アツいキーワードの抽出✓
...✓
![Page 6: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/6.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
課題
道具が足りないライブラリーがない・メンテナンスされていない
✓
✓
道具が使いにくい✓
![Page 7: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/7.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
解決方法
やる(道具を整備する)
![Page 8: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/8.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
世間の様子
主にJavaとPythonを使う道具が揃っている✓
✓
組み合わせてデータ分析✓
![Page 9: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/9.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
組み合わせた様子
https://arrow.apache.org/より(Apache License 2.0)
![Page 10: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/10.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
組み合わせの課題
データ交換コストが高い
データの直列化でCPUの8割を使用
by https://arrow.apache.org/✓
✓
![Page 11: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/11.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
解決方法
データのフォーマットを統一しよう!
![Page 12: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/12.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
統一した様子
https://arrow.apache.org/より(Apache License 2.0)
![Page 13: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/13.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
これはチャンス!
RubyがArrow対応すると…
既存のシステムとやりとりできる✓
「この部分だけはRubyを使う」をできる
✓
✓
![Page 14: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/14.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
一部でRuby
前述の図を変更(Apache License 2.0)
![Page 15: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/15.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
一部でRubyを…使える?
道具がないじゃん!
![Page 16: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/16.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
道具
Groongプロジェクト(CC BY 3.0)
![Page 17: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/17.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
Rroonga
全文検索エンジンライブラリー
SQLで操作とかじゃなくオブジェクトとして触れる
✓
✓
![Page 18: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/18.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
オブジェクト例
転置索引オブジェクト
![Page 19: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/19.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
転置索引
雑な説明
Hashみたいなもの✓
キー:トークン(単語みたいなもの)✓
✓
値:キー(トークン)を含む文書の一覧✓
✓
![Page 20: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/20.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
転置索引オブジェクト(1)
index = Groonga["Words.index"]token = "オブジェクト"p index.estimate_size(token)# => 19048# 「オブジェクト」を含む文書は# 約19048個ありそう
![Page 21: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/21.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
転置索引オブジェクト(2)
index.open_cursor(token) do |cursor| # 最初の出現情報 posting = cursor.next # 「オブジェクト」を含む最初の文書のID p posting.record.id # => 17 # この文書が何個「オブジェクト」を含むか p posting.term_frequency # => 1 # 文書の内容 puts posting.record.documentend
![Page 22: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/22.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
分析に使ってみよう
るりまを分析↓
関連語を抽出
![Page 23: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/23.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
関連語の抽出方法例
文書を前処理1.
全文書をトピックに分類
どんなトピックがあるかは学習✓
2.
同じトピックで使われやすい単語を抽出→関連語!
3.
![Page 24: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/24.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
文書の前処理
単語の出現頻度(Bag of Wordsという)
に変換
![Page 25: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/25.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
単語の出現頻度
"名古屋マジ名古屋"# ↓{ "名古屋" => 2, # 2回 "マジ" => 1, # 1回}
![Page 26: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/26.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
単語は数値に変換
# "名古屋" => 10# "マジ" => 20{ 10 => 2, # 「名古屋」2回 20 => 1, # 「マジ」1回}
![Page 27: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/27.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
なんで数値にするの?
計算しやすい
![Page 28: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/28.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
Rroongaで前処理できる?
出現回数は求められる?
👌:posting.term_frequency✓
✓
単語を数値に変換できる?👌:全文検索エンジンの必須機能✓
✓
![Page 29: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/29.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
Rroongaで前処理(1)
bow = {} # 「名古屋」の分だけindex.open_cursor("名古屋") do |cursor| cursor.each do |posting| record_id = posting.record_id term_id = posting.term_id # "名古屋"のID term_frequency = posting.term_frequency # 出現回数 bow[record_id] ||= {} bow[record_id][term_id] = term_frequency # bow: { # ↓実際は文字列じゃなくてID # 2 => {"名古屋" => 9} # 文書2では9回出現 # 5 => {"名古屋" => 19} # 文書5では19回出現 # } endend
![Page 30: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/30.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
Rroongaで前処理(2)
bow = {}index.table.each do |token| # 全トークンを処理 index.open_cursor(token) do |cursor| cursor.each do |posting| # ...同じ... end endend# bow: { # 完成!# 2 => {"名古屋" => 9, "マジ" => 2}# 5 => {"名古屋" => 19, "寄席" => 1},# ...# }
![Page 31: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/31.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
前処理終わり
次はトピックに分類どんなトピックがあるかは学習✓
✓
![Page 32: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/32.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
トピックの学習方法
LDA(Latent Dirichlet Allocation/潜在的ディリクレ配分法)
他にも色々ある参考:http://www.ism.ac.jp/~daichi/lectures/H24-TopicModel/ISM-2012-TopicModels-daichi.pdf
![Page 33: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/33.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
RubyでLDA
lda-ruby gem
![Page 34: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/34.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
RubyでLDAをしない!
なぜなら!Apache Arrowとか
GObject Introspectionの話をする
機会がなくなるからだ!
![Page 35: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/35.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
PythonでLDA
scikit-learn(gensimの方がよく使われているかも)
![Page 36: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/36.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
scikit-learnでLDA
import sklearn.decompositionLDA = sklearn.decomposition.LatentDirichletAllocationmodel = LDA(n_topics=100, # 100トピックに分類 learning_method="online", total_samples=len(bag_of_words)) # 文書数for words in bag_of_words: # 前処理結果 model.partial_fit(words) # 要フォーマット変換model.components_ # ここに学習したトピックがある
![Page 37: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/37.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
前処理結果がない!
前処理はRubyでやったPython側にはない✓
前処理結果がないとscikit-learnで分析できない!
✓
✓
どうしよう!
そうだ!Apache Arrowだ!✓
✓
![Page 38: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/38.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
Arrowでつながる世界
(Apache License 2.0)
![Page 39: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/39.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
RubyでArrow
ArrowはC++のライブラリーRubyからは使えない✓
✓
どうしよう!やる(バインディングを作る)✓
✓
![Page 40: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/40.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
Arrowのバインディング
arrow-glibgithub.com/kou/arrow-glib✓
Arrowのラッパー・C APIを提供✓
✓
GObject Introspection対応バインディングを実行時に生成✓
✓
![Page 41: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/41.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
使い方:準備
require "gi"Arrow = GI.load("Arrow")
![Page 42: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/42.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
使い方:配列を作る
# Arrowは高速に1次元・2次元配列を# 扱うAPIを提供するライブラリーbuilder = Arrow::UInt32ArrayBuilder.newbuilder.append(29) # 要素追加builder.append(9) # 要素追加term_ids = builder.finish # 配列作成p term_ids.length # => 2p term_ids.get_value(0) # => 29p term_ids.get_value(1) # => 9
![Page 43: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/43.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
少し…使いにくい!
GObject Introspectionだいたいいい感じになる!(すごい!)✓
が!Ruby特有のところは一手間必要✓
✓
![Page 44: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/44.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
使いやすさ検討
builder = Arrow::UInt32ArrayBuilder.newbuilder.append(29)builder.append(9)term_ids = builder.finish# ↓term_ids = Arrow::UInt32ArrayBuilder.build([29, 9])
![Page 45: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/45.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
一手間
class Arrow::ArrayBuilder class << self def build(values) builder = new values.each do |value| builder.append(value) end builder.finish end endend
![Page 46: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/46.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
もう一手間
class Arrow::UInt32Array class << self def new(values) UInt32ArrayBuilder.build(values) end endend
![Page 47: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/47.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
一手間後
builder = Arrow::UInt32ArrayBuilder.newbuilder.append(29)builder.append(9)term_ids = builder.finish# ↓term_ids = Arrow::UInt32Array.new([29, 9])
![Page 48: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/48.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
さらに使いやすさ検討
p term_ids.get_value(0) # => 29p term_ids.get_value(1) # => 9# ↓p term_ids[0] # => 29p term_ids[1] # => 9
![Page 49: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/49.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
二手間
class Arrow::Array def [](i) get_value(i) endend
![Page 50: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/50.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
もう一手間
class Arrow::Array include Enumerable def each length.times do |i| yield(self[i]) end endend
![Page 51: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/51.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
二手間後
p term_ids.get_value(0) # => 29p term_ids.get_value(1) # => 9# ↓p term_ids.to_a # => [29, 9]
![Page 52: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/52.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
一手間はどこに書くの?
# こう?require "gi"Arrow = GI.load("Arrow")module Arrow class Array # ... endend
![Page 53: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/53.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
違う!
GI.loadはデモ用のAPIちゃんと作るときは使わない✓
✓
GI::Loaderを継承#post_loadフック時に一手間✓
✓
![Page 54: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/54.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
GI::Loader#post_load
class Arrow::Loader < GI::Loader private def post_load(*args) require "arrow/array" require "arrow/array-builder" endend
![Page 55: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/55.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
arrow/array.rb
class Arrow::Array include Enumerable def each length.times do |i| yield(self[i]) end endend
![Page 56: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/56.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
arrow.rb
require "arrow/loader"module Arrow Loader.load("Arrow", self) # ↑の中で#post_loadが呼ばれるend
![Page 57: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/57.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
使い方
require "arrow"
term_ids = Arrow::UInt32Array.new([29, 9])p term_ids.to_a # => [29, 9]
![Page 58: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/58.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
すごい!
Rubyっぽい!
![Page 59: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/59.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
実装
RArrowgithub.com/kou/rarrow✓
RのArrowバインディングみたいでアレかもしれない✓
✓
他の一手間例もアリ
例:.open {|io| ...}で自動close✓
✓
![Page 60: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/60.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
前処理結果を渡す
忘れていたでしょ?
Arrowの話をしていたのはこのため✓
✓
手順
Rubyで書き出しa.
Pythonで読み込みb.
✓
![Page 61: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/61.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
Rubyで書き出し
FOS = Arrow::IO::FileOutputStream # 長いからSW = Arrow::IPC::StreamWriter # 長いからFOS.open("/tmp/bow", false) do |output_stream| # schema:カラム名と型の定義(省略) SW.open(output_stream, schema) do |writer| bow.each do |record_id, words| # record_batch(省略): # テーブルからN行だけ抜き出したもの writer.write_record_batch(record_batch) end endend
![Page 62: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/62.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
Pythonで読み出し
from scipy.sparse import csr_matriximport pandas as pdimport pyarrow as Awith A.io.MemoryMappedFile("/tmp/bow", "rb") as source: reader = A.ipc.StreamReader(source) for record_batch in reader: # ストリームで順に処理 # Pandasのデータフレームに変換(ゼロコピー!) df = record_batch.to_pandas() # 疎な行列に変換 corpus = csr_matrix((df["score"].values, df["term_id"].values, [0, df["term_id"].size]), shape=(1, n_features)) model.partial_fit(corpus) # 学習
![Page 63: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/63.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
トピックを確認
for topic in model.components_: n_top_terms = 10 # このトピックに関連している # 上位10単語を計算 top_term_indexes = # ↓[::-1]でreverse topic.argsort()[:-n_top_terms-1:-1] for i in top_term_indexes: term_id = i # 単語ID score = topic[i] # 関連度 print("%s:%s" % (term_id, score))
![Page 64: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/64.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
単語IDじゃわからん!
単語はIDにして計算したけど、確認は単語でしたい!✓
✓
単語とIDの管理は…Rroonga!トピックをRubyに持っていかないと✓
そうだ!Apache Arrowだ!✓
✓
![Page 65: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/65.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
Arrowでつながる世界
(Apache License 2.0)
![Page 66: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/66.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
Pythonで書き出し
with open("/tmp/topics", "wb") as sink: # schema:batch.schemaで取得できる(省略) writer = A.ipc.StreamWriter(sink, schema) def topic_to_df(topic): # 前述のトピックを確認した処理と同じ values = [[i, topic[i]] for i in topic.argsort()[:-10-1:-1]] return pd.DataFrame(values, columns=["term_id", "score"]) for topic in model.components_: df = topic_to_df(topic) batch = A.RecordBatch.from_pandas(df) writer.write_batch(batch) writer.close()
![Page 67: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/67.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
Rubyで読み込み
MMF = Arrow::IO::MemoryMappedFile # 長いからSR = Arrow::IPC::StreamReader # 長いからMMF.open("/tmp/topics", :read) do |input| SR.open(input) do |reader| reader.each do |record_batch| record_batch.each do |record| # 単語IDを単語に変換 term = index.table[record["term_id"]] p [term, record["score"]] end end endend
![Page 68: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/68.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
実際の結果
1: uzqpuaglzig, あきらめ, ご覧2: useloopback, タスク3: プラットホーム, mydog4: delegateclass, barbar...
微妙…
![Page 69: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/69.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
大事なこと
Garbage in,Garbage out
![Page 70: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/70.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
前処理をがんばる
いらない文書を無視✓
いらないトークンを無視✓
![Page 71: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/71.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
いらない文書を無視
entries = Groonga["Entries"]# 全文検索エンジンで検索するので高速!すごい!target_entries = entries.select do |record| (record.version == "2.4.0") - (record.document =~ "@todo") # ↑@todoな文書を対象外end# ... do |posting| # 存在チェックも全文検索エンジンがやるので高速! next if target_entries.key?(posting.record_id)# ... end
![Page 72: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/72.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
いらないトークンを無視
n_entries = target_entries.size# ほとんどの文書に含まれるなら重要じゃないtoo_many_threshold = n_entries * 0.9# ごく一部の文書にしか含まれないなら重要じゃないtoo_less_threshold = n_entries * 0.01# ... do |term| n_match_documents = index.estimate_size(term) next if n_match_documents >= too_much_threshold next if n_match_documents <= too_less_threshold# ... end
![Page 73: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/73.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
実際の結果
1: self, ブロック2: each, enumerator3: integer, 整数, 表示4: ruby, object...
それっぽくなってきた!
![Page 74: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/74.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
まとめ(1)
Rubyでデータ分析全部はムリ✓
でも一部ならいける✓
✓
![Page 75: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/75.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
まとめ(2)
一部ならいけるのは…
Apache Arrowの登場データ交換しやすくなる
✓
Rroongaの存在Rubyで高速に自然言語処理できる
✓
✓
![Page 76: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/76.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
まとめ(3)
RubyでApache Arrowバインディングが必要✓
GObject Introspectionベースで作った(arrow-glib)
✓
✓
![Page 77: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/77.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
まとめ(4)
GObject Introspectionだいたいいい感じ✓
さらに一手間でグッとよくなる✓
✓
![Page 78: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/78.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
まとめ(5)
実際に分析してみた
Rubyで前処理↓Arrow
✓
Pythonで分析↓Arrow
✓
Rubyで確認(本当は全文検索用DBに入れて活用する)
✓
✓
![Page 79: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/79.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
まとめ(6)
Rubyでデータ分析いけそう!Arrowでデータの受け渡しが容易に→分析処理へのRubyの参加が容易に
✓
RroongaでRubyでも高速に自然言語処理をできる
✓
✓
![Page 80: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/80.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
おしらせ(1)
Rubyでデータ分析したい人はクリアコードに相談してね!道具の用意・活用を手伝える✓
✓
![Page 81: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/81.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
おしらせ(2)
Groonga Meatup名古屋2017
時間:明日の午前(10:00-)✓
場所:Misoca✓
✓
![Page 82: Apache ArrowのRubyバインディングをGObject Introspectionで](https://reader034.vdocuments.site/reader034/viewer/2022051520/58a2a9941a28ab0d0a8b6bfd/html5/thumbnails/82.jpg)
Apache ArrowのRubyバインディングをGObject Introspectionで Powered by Rabbit 2.2.1
別案
RroongaのPython版を作るRubyもArrowも必要なくなる…✓
けど、より高速に前処理できる!✓
Cythonで作ろうかと思っている…✓
✓