1
6

More than 3 years have passed since last update.

データサイエンス100本ノック(構造化データ加工編)をやってみた part6(P-052~P-062)

Last updated at Posted at 2021-08-13

 この記事はデータサイエンスを勉強しながら、データサイエンス協会が提供するデータサイエンス100本ノック(構造化データ加工編)を解く過程を自分用にまとめたものです。

 チャプター   リンク   チャプター   リンク 
 P-001~P-016   part1   P-052~P-062   part6 
 P-017~P-022   part2   P-063~P-068   part7 
 P-023~P-031   part3   P-069~P-078   part8 
 P-032~P-039   part4   P-079~P-088   part9 
 P-040~P-051   part5  P-089~P-100  part10 
  • map()
  • ダミー変数に変換 pd.get_dummies()
  • 標準化 preprocessing.scale()
  • 正規化 preprocessing.minmax_scale()
  • 常用対数化 np.log10(), math.log10()
  • 自然対数化 np.log(), math.log()

P-052

P-052: レシート明細データフレーム(df_receipt)の売上金額(amount)を顧客ID(customer_id)ごとに合計の上、売上金額合計に対して2000円以下を0、2000円超を1に2値化し、顧客ID、売上金額合計とともに10件表示せよ。ただし、顧客IDが"Z"から始まるのものは非会員を表すため、除外して計算すること。

p052.py
df_amount_sum = df_receipt.query('not customer_id.str.startswith("Z")', engine='python').groupby('customer_id').amount.sum().reset_index()
df_amount_sum['amount_cd'] = df_amount_sum['amount'].apply(lambda x: 0 if x <= 2000 else 1)
df_amount_sum.head(10)

P-053

P-053: 顧客データフレーム(df_customer)の郵便番号(postal_cd)に対し、東京(先頭3桁が100〜209のもの)を1、それ以外のものを0に2値化せよ。さらにレシート明細データフレーム(df_receipt)と結合し、全期間において買い物実績のある顧客数を、作成した2値ごとにカウントせよ。

p053.py
df_tmp = df_customer.copy()
df_tmp['postal_flg'] = df_tmp['postal_cd'].apply(lambda x: 1 if 100 <= int(x.split('-')[0]) <= 209 else 0)
pd.merge(df_receipt, df_tmp, how='inner', on='customer_id').groupby('postal_flg').agg({'customer_id':'nunique'})

 解答ではpostal_flg列を作るとき、
 apply(lambda x: 1 if 100 <= int(x[0:3]) <= 209 else 0)
 とコードを書いていました。
 解答のようにデータをint型に変換して先頭の3桁をif文で条件式にかけた方がコードも簡単になりますね。

P-054 map()

P-054: 顧客データデータフレーム(df_customer)の住所(address)は、埼玉県、千葉県、東京都、神奈川県のいずれかとなっている。都道府県毎にコード値を作成し、顧客ID、住所とともに抽出せよ。値は埼玉県を11、千葉県を12、東京都を13、神奈川県を14とすること。結果は10件表示させれば良い。

p054.py
# map は replace でも同じ結果となる。
df_address_cd = df_customer['address'].str[0:3].map({'埼玉県':'11','千葉県':'12', '東京都':'13', '神奈川':'14'}).rename('address_cd')
pd.concat([df_customer[['customer_id', 'address']], df_address_cd], axis=1).head(10)

 map()の引数に辞書dict({key: value})を指定すると、keyと一致する要素がvalueに置き換えられる。
 要素の置換を行うメソッドにはreplace()があるが、すべての要素を別の値に置換するのであれば、map()のほうが処理速度は早い。

 また、map()relpace()の違いは、辞書dict({key: value})が全ての要素を網羅していない場合、map( )はNaNreplace( )はそのままとなる。

P-055

P-055: レシート明細データフレーム(df_receipt)の売上金額(amount)を顧客ID(customer_id)ごとに合計し、その合計金額の四分位点を求めよ。その上で、顧客ごとの売上金額合計に対して以下の基準でカテゴリ値を作成し、顧客ID、売上金額と合計ともに表示せよ。カテゴリ値は上から順に1〜4とする。結果は10件表示させれば良い。
・ 最小値以上第一四分位未満
・ 第一四分位以上第二四分位未満
・ 第二四分位以上第三四分位未満
・ 第三四分位以上

p055.py
df_amount_sum = df_receipt[['customer_id', 'amount']].groupby('customer_id').amount.sum().reset_index()
pct = df_amount_sum.quantile(q=[0, 0.25, 0.5, 0.75, 1])['amount']

# 売り上げ金額の合計を四分位ごとにカテゴリ値を返す関数
def pct_group(x):
    if x < pct[0.25]:
        return 1
    elif x < pct[0.5]:
        return 2
    elif x < pct[0.75]:
        return 3
    else:
        return 4

df_amount_sum['pct_group'] = df_amount_sum['amount'].apply(lambda x: pct_group(x))
df_amount_sum.head(10)

 df_amount_sum.quantile(q=[0, 0.25, 0.5, 0.75, 1])の結果はこちら
スクリーンショット 2021-08-11 21.03.49.png

P-056

P-056: 顧客データフレーム(df_customer)の年齢(age)をもとに10歳刻みで年代を算出し、顧客ID(customer_id)、生年月日(birth_day)とともに抽出せよ。ただし、60歳以上は全て60歳代とすること。年代を表すカテゴリ名は任意とする。先頭10件を表示させればよい。

p056.py
df_age_band = df_customer['age'].apply(lambda x: min(math.floor(x / 10) * 10, 60)).rename('age_band')
df_customer_age_band = pd.concat([df_customer[['customer_id', 'birth_day']],df_age_band],axis=1)
df_customer_age_band.head(10)

 min(math.floor(x / 10) * 10, 60)とすることで
 math.floor(x / 10)が60よりも大きい場合(70, 80歳代の場合)
 60(60歳代)が選択される。

P-057

P-057: 前問題の抽出結果と性別(gender)を組み合わせ、新たに性別×年代の組み合わせを表すカテゴリデータを作成せよ。組み合わせを表すカテゴリの値は任意とする。先頭10件を表示させればよい。

p057.py
df_customer_age_band['age_band_gender'] = df_customer['gender_cd'] + df_customer_age_band['age_band'].astype('str')
df_customer_age_band.head(10)

 カテゴリ値を性別コード + 年代としたage_band_gender列を作成。
 astype()関数を使って年代をstr型に変換してから計算しています。

P-058 pd.get_dummies()

P-058: 顧客データフレーム(df_customer)の性別コード(gender_cd)をダミー変数化し、顧客ID(customer_id)とともに抽出せよ。結果は10件表示させれば良い。

p058.py
pd.get_dummies(df_customer[['customer_id', 'gender_cd']], columns=['gender_cd']).head(10)

 pd.get_dummies():カテゴリ変数をダミー変数に変換。
 columnsでダミー化したい列の列名を指定できる。

P-059 preprocessing.scale()

P-059: レシート明細データフレーム(df_receipt)の売上金額(amount)を顧客ID(customer_id)ごとに合計し、合計した売上金額を平均0、標準偏差1に標準化して顧客ID、売上金額合計とともに表示せよ。標準化に使用する標準偏差は、不偏標準偏差と標本標準偏差のどちらでも良いものとする。ただし、顧客IDが"Z"から始まるのものは非会員を表すため、除外して計算すること。結果は10件表示させれば良い。

p059.py
from sklearn import preprocessing
df_amount_sum = df_receipt.query('not customer_id.str.startswith("Z")', engine='python').groupby('customer_id').amount.sum().reset_index()
df_amount_sum['amount_std'] = preprocessing.scale(df_amount_sum['amount'])
df_amount_sum.head(10)

 preprocessing.scale():標準化を行うためのメソッド(平均を0、標準偏差を1)
 実際にamount_std列にmean()std()を用いてみると、本当に標準化されているのが確認できる。

P-060 preprocessing.minmax_scale()

P-060: レシート明細データフレーム(df_receipt)の売上金額(amount)を顧客ID(customer_id)ごとに合計し、合計した売上金額を最小値0、最大値1に正規化して顧客ID、売上金額合計とともに表示せよ。ただし、顧客IDが"Z"から始まるのものは非会員を表すため、除外して計算すること。結果は10件表示させれば良い。

p060.py
df_amount_sum = df_receipt.query('not customer_id.str.startswith("Z")', engine='python').groupby('customer_id').amount.sum().reset_index()
df_amount_sum['amount_normal'] = preprocessing.minmax_scale(df_amount_sum['amount'])
df_amount_sum.head(10)

 preprocessing.minmax_scale():最小値0、最大値1に正規化

P-061 math.log10(), np.log10()

P-061: レシート明細データフレーム(df_receipt)の売上金額(amount)を顧客ID(customer_id)ごとに合計し、合計した売上金額を常用対数化(底=10)して顧客ID、売上金額合計とともに表示せよ。ただし、顧客IDが"Z"から始まるのものは非会員を表すため、除外して計算すること。結果は10件表示させれば良い。

p061
df_amount_sum = df_receipt.query('not customer_id.str.startswith("Z")', engine='python').groupby('customer_id').amount.sum().reset_index()
df_amount_sum['amount_log10'] = df_amount_sum['amount'].apply(lambda x: math.log10(x))
df_amount_sum.head(10)

  lambdaを使ってamountの全要素にmath.log10()を用いて常用対数化を行っていますが、
 np.log10を使って引数にdf_amount_sum['amount']を渡して書いた方がスッキリしそう。

P-062 np.log()

P-062: レシート明細データフレーム(df_receipt)の売上金額(amount)を顧客ID(customer_id)ごとに合計し、合計した売上金額を自然対数化(底=e)して顧客ID、売上金額合計とともに表示せよ。ただし、顧客IDが"Z"から始まるのものは非会員を表すため、除外して計算すること。結果は10件表示させれば良い。

p062.py
df_amount_sum = df_receipt.query('not customer_id.str.startswith("Z")', engine='python').groupby('customer_id').amount.sum().reset_index()
df_amount_sum['amount_log'] = np.log(df_amount_sum['amount'])
df_amount_sum.head(10)
``` 
1
6
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
1
6