Loading web-font TeX/Main/Regular

April 27, 2013

KDD Cup 2013 - Author-Paper Identification Challengeのメモ

KDD Cup 2013のタスクは2つ。その1つはMicrosoft Academic Search (MAS)の文献検索での著者の名前の曖昧性がテーマ。

MASの文献は

- 同じ著者が色んな名前で登録されてる
- 違う著者が同じ名前で登録されてる

という名前の曖昧性のせいで文献に対して著者がちゃんと割り当てられない。そこでタスクは、ノイズを含んだ著者と論文の組み合わせを入力して、本物の著者と論文の組み合わせを出力するというもの。

色々準備されてるのでとっかかりやすい。

- 説明 / Description - KDD Cup 2013 - Author-Paper Identification Challenge - Kaggle
- チュートリアル / git://github.com/benhamner/Kdd2013AuthorPaperIdentification.git
- もう一方 / Data - KDD Cup 2013 - Author Disambiguation - Kaggle

データ


詳細はData - KDD Cup 2013 - Author-Paper Identification Challenge - Kaggle



- paperauthor : 12775821
- trainconfirmed : 123447
- traindeleted : 112462
- validpaper : 90088

- author : 247203
- paper : 2257249
- journal : 15151
- conference : 4545

入出力


入力

著者のIDと文献のIDのリスト

# SELECT * FROM validpaper LIMIT 5;
 authorid | paperid 
----------+---------
       55 |    2507
       55 |   15471
       55 |   19294
       55 |   20444
       55 |   24074
(5 rows)

出力

著者のIDとスペース区切りの文献のID

% head -n2 Submissions/basicCoauthorBenchmarkRev2.csv
AuthorId,PaperIds
2080775,2200312 1047104 280462 1467879


チュートリアル


benhamner/Kdd2013AuthorPaperIdentification · GitHubに載っているチュートリアルで。scikit-learnRandom forestの実装を使っている。trainconfirmedを正例、traindeletedを負例として学習し、validpaperを判別している。

PostgreSQLのバックアップを配布してくれているので、PostgreSQLのインストール。
brew install postgresql
データベース作成。
createdb Kdd2013AuthorPaperIdentification
復元。
pg_restore -Fc -U [ユーザ名] -d Kdd2013AuthorPaperIdentification dataRev2.postgres
起動。
postgres -D /usr/local/var/postgres
確認。
psql -l
接続。
psql Kdd2013AuthorPaperIdentification

PythonBenchmark内のSETTINGS.jsonのファイル出力先とuser名を変更しておく。

実行のために必要なpsycopg2のような必要なモジュールはエラーを見て何が足りないか確認して適宜pipでインストール。
sudo pip install psycopg2

- 8.7.1. sklearn.ensemble.RandomForestClassifier — scikit-learn 0.14-git documentation

特徴量

authoridとpaperidを基に以下の値をテーブルから取ってくる。

- AuthorJournalCounts (著者のジャーナル数)
- AuthorConferenceCounts (著者の会議数)
- AuthorPaperCounts (著者の文献数)
- PaperAuthorCounts (文献の著者数)
- SumPapersWithCoAuthors (共著者との文献数の和)

ターゲット

trainconfirmedが1、traindeletedが0。

パラメータ
RandomForestClassifier(n_estimators=50, 
                       verbose=2,
                       n_jobs=1,
                       min_samples_split=10,
                       random_state=1)

結果

0.85078

ちなみに何も学習せずだと

0.67551

April 5, 2013

PythonでWebサイトのビデオを抽出してYouTube Data APIクライアントでプレイリストに登録する

YouTube系まとめサイトの動画を流しっぱなしにしておくために,以前herokuにRailsでpost-tube.satomacoto.comというのを自分用につくった.が,無料で使えるデータベースの制限を超えちゃってたので,WebサイトのYouTubeへのリンクを抽出してYouTubeのPlaylistに登録するPythonのスプリクトをやっつけで書いてみた.こんな風にプレイリストを作成する→http://www.youtube.com/user/stmct/videos.忘れたときのためにメモ.


Google Data API


Google Data APIを使ってYouTubeのプレイリスト作成や動画登録を行う.Pythonから操作するためにgdata-python-clientが用意されている.このサイトからダウンロードした gdata-...zip を解凍したら

sudo python setup.py install

でインストール.

使い方は

Developer's Guide: Python - YouTube — Google Developers


APIキーの取得


http://code.google.com/apis/youtube/dashboard/でAPIキーを取得する.


ClientLogin


ローカルで動かすのでClientLoginで認証する.認証はPlaylistの作成や動画の登録に必要.下記のスクリプトでは以下の部分を設定する.YouTubeのアカウント名は http://www.youtube.com/user/stmct だったらstmctにあたるところ.Googleを2段階認証にしている場合はアプリケーション固有のパスワードを生成する.

username = 'YouTubeのアカウント名'
email = 'APIキーを取得したGoogleのアカウント名'
password = 'Googleのパスワード'
developer_key = '取得したAPIキー'


コード


使い方

python youtube_gdata_create_playlist.py http://...

汚いコードだな…

#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
まとめサイトなどURLに含まれるYouTubeをプレイリストに保存する
$ python youtube_gdata_playlist.py http://...
'''
# 以下要設定
# cf. YouTubeクライアント
# https://developers.google.com/youtube/1.0/developers_guide_python#ClientLogin
####
username = ''
email = ''
password = ''
developer_key = ''
####
import sys
import time
import re
import urllib2
import gdata.youtube
import gdata.youtube.service
from BeautifulSoup import BeautifulSoup
class YouTube(object):
"""
YouTube Data API
https://code.google.com/p/gdata-python-client/
https://developers.google.com/youtube/1.0/developers_guide_python
"""
def __init__(self, username, email, password, developer_key):
self.yt_service = gdata.youtube.service.YouTubeService()
self.yt_service.ssl = True
self.username = username
# ClientLogin for installed applications
self.yt_service.email = email
self.yt_service.password = password
self.yt_service.developer_key = developer_key
self.yt_service.ProgrammaticLogin()
def AddPlaylist(self, title, description=""):
'''
プレイリストを追加する
https://developers.google.com/youtube/1.0/developers_guide_python#AddingPlaylists
Parameters
==========
- title : str
- description : str
Returns
=======
playlist_uri : str
'''
# タイトルと説明を表示
limit = 0
for i in range(len(title)):
if limit > 60:
break
if ord(title[i]) > 255:
limit += 3
else:
limit += 1
tmp_title = title[:i] if i != len(title) - 1 else title
print title # 20字
print description
new_public_playlistentry = self.yt_service.AddPlaylist(tmp_title, description)
if isinstance(new_public_playlistentry, gdata.youtube.YouTubePlaylistEntry):
print 'New playlist added'
return 'http://gdata.youtube.com/feeds/playlists/' + new_public_playlistentry.id.text.split('/')[-1]
else:
return None
def AddVideoToPlaylist(self, playlist_uri, video_id):
'''
プレイリストにビデオを追加する
Parameters
==========
- playlist_uri : str
- video_id : str
'''
playlist_video_entry = self.yt_service.AddPlaylistVideoEntryToPlaylist(
playlist_uri, video_id)
if isinstance(playlist_video_entry, gdata.youtube.YouTubePlaylistVideoEntry):
print video_id
def get_video_ids(url):
'''
urlに含まれるYouTubeのvideo idを抽出する
'''
page = urllib2.urlopen(url)
soup = BeautifulSoup(page)
title = soup('title')[0].string
video_ids = []
targets = [['embed', 'src', 'v\/([-\w]+)'],
['iframe', 'src', 'youtube.com\/embed\/([-\w]+)'],
['a', 'href', 'youtube.com\/watch\?v=([-\w]+)'],
['a', 'href', 'youtu\.be\/([-\w]+)']]
for tag, attr, exp in targets:
for element in soup(tag):
src = element.get(attr)
if src:
result = re.search(exp, src)
if result:
video_ids.append(result.group(1))
tmp = []
for video_id in video_ids:
if video_id not in tmp:
tmp.append(video_id)
return title, tmp
if __name__ == '__main__':
try:
# コマンドライン引数から取得先のURL
url = sys.argv[1]
except:
exit()
# urlのタイトルと含まれるビデオの取得
title, video_ids = get_video_ids(url)
print len(video_ids)
# YouTubeクラスインスタンス
yt = YouTube(username, email, password, developer_key)
# プレイリストの作成
playlist_uri = yt.AddPlaylist(title, title + " - " + url)
# 失敗
failed = []
# リトライ回数
retry = 0
# ビデオ番号
n = 0
# ビデオがあってリトライ回数が10を超えない限り継続
while video_ids and retry < 10:
n += 1
print n,
video_id = video_ids.pop(0)
try:
yt.AddVideoToPlaylist(playlist_uri, video_id)
time.sleep(1)
except gdata.service.RequestError as e:
print video_id
print type(e), e
# 短期間にアクセスしすぎないように...
if 'too_many_recent_calls' in str(e):
video_ids = [video_id] + video_ids
time.sleep(40) # 40秒待機
retry += 1
n -= 1
except:
failed.append(video_id)
print video_id
print sys.exc_info()
# 残り
print 'rest: %s' % ','.join(video_ids)
# エラー
print 'failed: %s' % ','.join(failed)
# プレイリストURI
print playlist_uri



...


  • NAVERみたいに次のページがある場合に対応するか
  • 人がまとめたものを使うってのはまずいのかな
  • post-tube.satomacoto.comってどうやってつくったんだっけかな
  • 連続再生だけが目的じゃなくてサイトごとにどんなビデオが貼り付けられているかとか自分の再生履歴やらお気に入りやらで何かしようとか思ってたんだっけ
  • プレイリストの履歴は取れないからな…

April 2, 2013

語/語の組み合わせの大人らしさ

検索エンジンにクエリを投げて,セーフサーチのオン/オフを切り替えたときに返ってきた件数をうまいことして,語/語の組み合わせの大人度合いが測れないかと思ったけどあんまりうまくいかなかった.


イメージ


すっごく単純にすると

  • 大人な語
    • 語がどれほど大人か
    • 大人(眼球) = \log \frac{n(眼球, off)}{n(眼球, strict)}
    • ただしn(q,a)はクエリqのセーフサーチの設定aのoff/moderate/strictのとき結果件数.検索結果が0件の場合もあるだろうから分母には1足しておいてもいい.
  • 大人な組み合わせ
    • 語を組み合わせることでどれくらい大人っぽくなるか
    • 大人組(目玉, 玉子) = \log \frac{n(目玉 and 玉子, off)}{n(目玉 and 玉子, strict)} - 大人(目玉) - 大人(玉子)

みたいな感じ.あんま考えてないのでこれで比較ができるかわかんないけど.大人がゲシュタルト崩壊…


でも


普通に考えてセーフサーチが強いほうが検索結果少ないだろ,と思ってたら,
眼球 - Google 検索
セーフサーチ: オフ
約 57,800,000 件 (0.20 秒) 
眼球 - Google 検索
セーフサーチ: 強
約 71,800,000 件 (0.19 秒) 
セーフサーチ強のほうが多いこともある…どういうことだってばよ



Bingもあんま変わらん.そういうもんなのかな.


ちなみにWebの検索結果件数を使って人間関係を抽出している論文(件数だけじゃないけど)→Web 上の情報からの人間関係ネットワークの抽出