新しい特徴量の生成

Pythonで学ぶ探索的データ分析

George Boorman

Curriculum Manager, DataCamp

相関

sns.heatmap(planes.corr(numeric_only=True), annot=True)
plt.show()

価格と所要時間のピアソン相関係数が0.54であることを示すヒートマップ

Pythonで学ぶ探索的データ分析

データ型の確認

print(planes.dtypes)
Airline                    object
Date_of_Journey    datetime64[ns]
Source                     object
Destination                object
Route                      object
Dep_Time           datetime64[ns]
Arrival_Time       datetime64[ns]
Duration                  float64
Total_Stops                object
Additional_Info            object
Price                     float64
dtype: object
Pythonで学ぶ探索的データ分析

経由地の数

print(planes["Total_Stops"].value_counts())
1 stop      4107
non-stop    2584
2 stops     1127
3 stops       29
4 stops        1
Name: Total_Stops, dtype: int64
Pythonで学ぶ探索的データ分析

経由地数のクリーニング

planes["Total_Stops"] = planes["Total_Stops"].str.replace(" stops", "")

planes["Total_Stops"] = planes["Total_Stops"].str.replace(" stop", "")
planes["Total_Stops"] = planes["Total_Stops"].str.replace("non-stop", "0")
planes["Total_Stops"] = planes["Total_Stops"].astype(int)
Pythonで学ぶ探索的データ分析

相関

sns.heatmap(planes.corr(numeric_only=True), annot=True)
plt.show()

価格と経由地数のピアソン相関0.62、所要時間と経由地数の相関0.74を示すヒートマップ

Pythonで学ぶ探索的データ分析

日付

print(planes.dtypes)
Airline                    object
Date_of_Journey    datetime64[ns]
Source                     object
Destination                object
Route                      object
Dep_Time           datetime64[ns]
Arrival_Time       datetime64[ns]
Duration                  float64
Total_Stops                 int64
Additional_Info            object
Price                     float64
dtype: object
Pythonで学ぶ探索的データ分析

月と曜日の抽出

planes["month"] = planes["Date_of_Journey"].dt.month

planes["weekday"] = planes["Date_of_Journey"].dt.weekday
print(planes[["month", "weekday", "Date_of_Journey"]].head())
   month  weekday   Date_of_Journey
0      9        4        2019-09-06
1     12        3        2019-12-05
2      1        3        2019-01-03
3      6        0        2019-06-24
4     12        1        2019-12-03
Pythonで学ぶ探索的データ分析

出発・到着時刻

planes["Dep_Hour"] = planes["Dep_Time"].dt.hour
planes["Arrival_Hour"] = planes["Arrival_Time"].dt.hour
Pythonで学ぶ探索的データ分析

相関

日時属性と価格に相関がないことを示すヒートマップ

Pythonで学ぶ探索的データ分析

カテゴリの作成

print(planes["Price"].describe())
count     7848.000000
mean      9035.413609
std       4429.822081
min       1759.000000
25%       5228.000000
50%       8355.000000
75%      12373.000000
max      54826.000000
Name: Price, dtype: float64
範囲 チケット種別
<= 5228 エコノミー
> 5228 <= 8355 プレエコノミー
> 8335 <= 12373 ビジネス
> 12373 ファースト
Pythonで学ぶ探索的データ分析

記述統計

twenty_fifth = planes["Price"].quantile(0.25)

median = planes["Price"].median()
seventy_fifth = planes["Price"].quantile(0.75)
maximum = planes["Price"].max()
Pythonで学ぶ探索的データ分析

ラベルとビン

labels = ["Economy", "Premium Economy", "Business Class", "First Class"]

bins = [0, twenty_fifth, median, seventy_fifth, maximum]
Pythonで学ぶ探索的データ分析

pd.cut()

pd.cut を呼び出す

planes["Price_Category"] = pd.cut(


Pythonで学ぶ探索的データ分析

pd.cut()

データを渡す

planes["Price_Category"] = pd.cut(planes["Price"],


Pythonで学ぶ探索的データ分析

pd.cut()

ラベルを設定

planes["Price_Category"] = pd.cut(planes["Price"],
                                  labels=labels,

Pythonで学ぶ探索的データ分析

pd.cut()

ビンを指定

planes["Price_Category"] = pd.cut(planes["Price"],
                                  labels=labels,
                                  bins=bins)
Pythonで学ぶ探索的データ分析

価格カテゴリ

print(planes[["Price","Price_Category"]].head())
     Price   Price_Category
0  13882.0      First Class
1   6218.0  Premium Economy
2  13302.0      First Class
3   3873.0          Economy
4  11087.0   Business Class
Pythonで学ぶ探索的データ分析

航空会社別の価格カテゴリ

sns.countplot(data=planes, x="Airline", hue="Price_Category")
plt.show()
Pythonで学ぶ探索的データ分析

航空会社別の価格カテゴリ

各航空会社の価格カテゴリ別フライト数。Jet Airways のファーストが最多であることを示すカウントプロット

Pythonで学ぶ探索的データ分析

Passons à la pratique !

Pythonで学ぶ探索的データ分析

Preparing Video For Download...