[kaggle x Titanic] 初心者向けチュートリアルでの躓き&解決策


開発環境

なぜこのチュートリアルにたどり着けたか

「機械学習の力をつけるならkaggleで良カーネルを写経するのがおすすめ!」とTwitterで流れてきたことで俄然興味を持ち登録したものの初心者向けのよいカーネルが見つけられず放置していました。
しかしTwitter上で親切な方がDMでおすすめのカーネルを教えてくれたのです

私のTwitterアカウント: @spring_suzuran

エラー1 @3.1 Import Libraries

FileNotFoundError: [WinError 2] 指定されたファイルが見つかりません。

from subprocess import check_output
print(check_output(["ls", "../input"]).decode("utf8"))

上の2行を実行したときにエラーが出ました。原因はWindowsにはlsバイナリがないということです。

解決策: エラーが出たコードを以下のコードに差し替えます。

import os
print(os.listdir('./input'))

出力結果

['gender_submission.csv', 'test.csv', 'train.csv']

参考サイト: stackoverflow

KeyError: "['Sex_Code'] not in index"

「3.25 Split Training and Testing Data」でKeyErrorが出ました。「'Sex_Code'」がないよ、と。しかしSex_Codeは以前にも何度も出てきた、ないはずはない、と思っていたら、前のセルで「'Sex_code'」と書いていましたf^^;

コードを一気に実行したい!

kaggleのタイタニックを写経していると、セルがかなり増えていきます。セルを1つ実行するならShift+Enterでよいのですが、写経を続けていくと何十ものセルを一気に実行したいという欲求がでてきます。

それというのも新しく書き上げたセルを実行するのに、以前に書いたセルでimportしたライブラリが必要となる場面があるのです。
「sklearnがありません」「pandasがありません」と言われないためにimportしたライブラリはきちんと前もってもれなく実行しておきたい。しかし何十ものセルを最初からひたすらShift+Enterし続けるのは怠惰なプログラマをめざし続ける私にとっては苦痛以外の何者でもない。。。
と思って「jupyter notebook run all cells」とGoogle検索すると、ありました!

'help'タブ> 'edit keyboard shortcuts' > 'run all cells'。私はCtrl-Rを選択しました。

For the latest jupyter notebook, (version 5) you can go to the 'help' tab in the top of the notebook and then select the option 'edit keyboard shortcuts' and add in your own customized shortcut for the 'run all' function.

参考サイト: stackoverflow

graphvizでFileNotFoundError

importpip install graphvizもしたけど、「graphvizのdotファイルないよ」と言われる。実はgraphviz自体のインストールが必要なのでした。環境変数登録後はPCの再起動を忘れずに!
参考サイト: Teratail京都女子大学現代社会学部

インストールされているか確認するには

Anaconda Promptでpip show graphvizを実行すると情報を出してくれる。

(base) C:\Users\yukiya>pip show graphviz
Name: graphviz
Version: 0.9
Summary: Simple Python interface for Graphviz
Home-page: https://github.com/xflr6/graphviz
Author: Sebastian Bank
Author-email: [email protected]
License: MIT
Location: c:\users\yukiya\anaconda3\lib\site-packages
Requires:
Required-by:

パスを確認したいとき

Jupyter notebookで以下のコードを実行する。

import sys
import pprint

pprint.pprint(sys.path)

出力結果

['',
 'C:\\Users\\yukiya\\Anaconda3\\python36.zip',
 'C:\\Users\\yukiya\\Anaconda3\\DLLs',
 'C:\\Users\\yukiya\\Anaconda3\\lib',
 'C:\\Users\\yukiya\\Anaconda3',
 'C:\\Users\\yukiya\\Anaconda3\\lib\\site-packages',
 'C:\\Users\\yukiya\\Anaconda3\\lib\\site-packages\\win32',
 'C:\\Users\\yukiya\\Anaconda3\\lib\\site-packages\\win32\\lib',
 'C:\\Users\\yukiya\\Anaconda3\\lib\\site-packages\\Pythonwin',
 'C:\\Users\\yukiya\\Anaconda3\\lib\\site-packages\\IPython\\extensions',
 'C:\\Users\\yukiya\\.ipython']

うっすらピンクでUserWarning

Jupyter notebookでコードを書いて実行すると、コードを実行できないわけではないけど警告が出るという場面に遭遇します。ガン無視して進めることもできますが、もやもやするので、課題提出完了後、警告に対して耳を傾けるようにしました。
該当コード

#how does embark port factor with class, sex, and survival compare
e = sns.FacetGrid(data1, col = 'Embarked')
e.map(sns.pointplot, 'Pclass', 'Survived', 'Sex', ci=95.0, palette = 'deep')
e.add_legend()

警告文

C:\Users\ayumusato\Anaconda3\lib\site-packages\seaborn\axisgrid.py:703: UserWarning: Using the pointplot function without specifying `order` is likely to produce an incorrect plot.
C:\Users\ayumusato\Anaconda3\lib\site-packages\seaborn\axisgrid.py:708: UserWarning: Using the pointplot function without specifying `hue_order` is likely to produce an incorrect plot.

order = [1,2,3], hue_order=["male", "female"]を追記する必要があるようです。

完成コード

#how does embark port factor with class, sex, and survival compare
e = sns.FacetGrid(data1, col = 'Embarked')
e.map(sns.pointplot, 'Pclass', 'Survived', 'Sex', ci=95.0, palette = 'deep',
         order = [1,2,3], hue_order=["female", "male"])
e.add_legend()

参考サイト: stackoverflow