LoginSignup
12
17

More than 5 years have passed since last update.

Google製可視化OSSのFacetsをGoogleColaboratoryで使ってみる

Last updated at Posted at 2018-08-13

こちらの記事を読んで、Google製可視化OSSのFacetsがめっちゃ便利だから使ってみてくれ
「Facetsを使うための準備が大変そうだなぁ」と思ったのでGoogleColaboratoryを利用すれば簡単にできるよという話。

GoogleColaboratoryについて

GoogleColaboratoryって何という人はこちらの記事を読んで下さい。

記事と同じことがやりたい人はGoogleアカウントが必要になるので作っておいてください。

Facetsを利用した可視化

Kagglerおなじみのタイタニックデータセットを可視化してみます。
下記リンク、> Download All からCSVをダウンロードしておいてください。
Kaggle - Titanic: Machine Learning from Disaster

kaggle_titanic_download_1.png

これ以降のコードはGoogleColaboratory上で実行するコードになります。
コピーしてセル毎に貼り付けて実行(Run)してみて下さい。

GoogleColabUpload
# ローカルにダウンロードしたCSVをアップロードします
from google.colab import files
uploaded = files.upload() # then browse, select the files. It's then uploaded

google_colab_upload.gif

titanic_datasets
# datasets load
import pandas as pd
titanic_train_data = pd.read_csv("train.csv")
titanic_test_data = pd.read_csv("test.csv")

titanic_train_data = titanic_train_data.drop(['PassengerId','Name','Ticket','Cabin'], axis=1)
titanic_test_data = titanic_test_data.drop(['Name','Ticket','Cabin'], axis=1)

Facets Dive

facets_titanic_dive
# Display the facets overview visualization for this data
from IPython.core.display import display, HTML
jsonstr = titanic_train_data.to_json(orient='records')
HTML_TEMPLATE = """<link rel="import" href="https://raw.githubusercontent.com/PAIR-code/facets/master/facets-dist/facets-jupyter.html">
        <facets-dive id="elem" height="600"></facets-dive>
        <script>
          var data = {jsonstr};
          document.querySelector("#elem").data = data;
        </script>"""
html = HTML_TEMPLATE.format(jsonstr=jsonstr)
display(HTML(html))

demo_facets_dive.gif

Facets Overview

facets_titanic_overview
import base64
from generic_feature_statistics_generator import GenericFeatureStatisticsGenerator
gfsg = GenericFeatureStatisticsGenerator()
proto = gfsg.ProtoFromDataFrames([{'name': 'train', 'table': titanic_train_data},
                                  {'name': 'test', 'table': titanic_test_data}])
protostr = base64.b64encode(proto.SerializeToString()).decode("utf-8")


HTML_TEMPLATE = """<link rel="import" href="https://raw.githubusercontent.com/PAIR-code/facets/master/facets-dist/facets-jupyter.html" >
        <facets-overview id="elem"></facets-overview>
        <script>
          document.querySelector("#elem").protoInput = "{protostr}";
        </script>"""
html = HTML_TEMPLATE.format(protostr=protostr)
display(HTML(html))

demo_facets_overview.gif

参考

12
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
17