セクターとIPO年ごとに複数の指標を計算する
seaborn の pointplot() 関数は、数値変数の要約統計量を、カテゴリ変数の異なるレベル間で比較するのに役立ちます。
seaborn.pointplot(x=None, y=None, hue=None, data=None, ...)
動画では、時価総額(数値変数)を、IPO(カテゴリ変数)が2000年より前(第1のレベル)か2000年以降(第2のレベル)かで分けて可視化しました。
この演習では、NYSE と NASDAQ について、2000年以降の各年の平均時価総額を、95パーセンタイルを超える外れ値を除外したうえで比較します。pandas は pd、matplotlib.pyplot は plt としてインポート済みで、参照列 'Exchange' を持つ listings DataFrame がワークスペースに用意されています。
この演習はコースの一部です
Pythonでの金融データのインポートと管理
演習の手順
seabornをsnsとしてインポートします。- すべての取引所のうち、
'amex'を除外し、IPO が2000年以降の企業だけにlistingsをフィルタリングします。 - 列
'IPO Year'のデータを整数に変換します。 - 時価総額を百万USD単位で表す列
market_cap_mを作成します。 market_cap_mについて、95パーセンタイルを超える値を除外します。listingsに対してpointplotを作成し、xに列'IPO Year'、yに'market_cap_m'、hueに'Exchange'を指定します。xticksを45度回転させてから結果を表示します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Import the seaborn library as sns
____
# Exclude IPOs before 2000 and from the 'amex'
listings = ____[(____['IPO Year'] > ____) & (listings.Exchange != ____)]
# Convert IPO Year to integer
listings['IPO Year'] = ____['IPO Year'].____(____)
# Create market_cap_m
listings['market_cap_m'] = ____['Market Capitalization'].div(1e6)
# Exclude outliers
listings = listings[listings.____ < listings.____.____(.95)]
# Create the pointplot
sns.pointplot(x=____, y=____, hue=____, data=____)
# Rotate xticks
plt.____(____=____)
# Show the plot
plt.show()