1 of 16

3 Exercise: �Creating Features

2 of 16

In this exercise you'll start developing the features

you identified in Exercise 2 as having the most potential.

As you work through this exercise,

you might take a moment to look at

the data documentation again and consider

whether the features we're creating make sense

from a real-world perspective, and

whether there are any useful combinations that stand out to you.

ドキュメント読むべし

feature生成

Feature組み合わせ

3 of 16

列の削除を無効に

最後のほうの6列を出力

4 of 16

Let's start with a few mathematical combinations.

We'll focus on features describing areas

having the same units (square-feet) makes it easy to combine them in sensible ways.

Since we're using XGBoost (a tree-based model), we'll focus on ratios and sums

比と和に注目

面積に注目

5 of 16

1) Create Mathematical Transforms

Create the following features:

  • LivLotRatio:   the ratio of GrLivArea to LotArea
  • Spaciousness:  the sum of FirstFlrSF and SecondFlrSF 

        divided by TotRmsAbvGrd

  • TotalOutsideSF: the sum of WoodDeckSF, OpenPorchSF, 

        EnclosedPorchThreeseasonporch, and 

        ScreenPorch

Gr Liv Area (Continuous):

Above grade (ground) living area square feet

Lot Area (Continuous): Lot size in square feet

区画面積

TotRmsAbvGrd (Discrete): Total rooms above grade (does not include bathrooms)

above grade means

the portion of a home that is above the ground

6 of 16

7 of 16

If you've discovered an interaction effect

between a numeric feature and a categorical feature,

you might want to model it explicitly using a one-hot encoding,

like so:

# One-hot encode Categorical feature,

adding a column prefix "Cat"

X_new = pd.get_dummies(df.Categorical, prefix="Cat")

# Multiply row-by-row

X_new = X_new.mul(df.Continuous, axis=0)

# Join the new features to the feature set

X = X.join(X_new)

pandasでカテゴリ変数をダミー変数に変換pandas.get_dummies()関数を使う

One-hot encodeing

8 of 16

If you've discovered an interaction effect

between a numeric feature and a categorical feature,

you might want to model it explicitly using a one-hot encoding,

like so:

# One-hot encode Categorical feature,

adding a column prefix "Cat"

X_new = pd.get_dummies(df.Categorical, prefix="Cat")

# Multiply row-by-row

X_new = X_new.mul(df.Continuous, axis=0)

# Join the new features to the feature set

X = X.join(X_new)

9 of 16

3) Count Feature

Let's try creating a feature that describes

how many kinds of outdoor areas a dwelling has.

Create a feature PorchTypes that counts

how many of the following are greater than 0.0:

WoodDeckSF

OpenPorchSF

EnclosedPorch

Threeseasonporch ScreenPorch

10 of 16

面積0以上の要素を

数えている

11 of 16

4) Break Down a Categorical Feature

MSSubClass describes the type of a dwelling

MS SubClass (Nominal): Identifies the type of dwelling involved in the sale.

020 1-STORY 1946 & NEWER ALL STYLES

030 1-STORY 1945 & OLDER

040 1-STORY W/FINISHED ATTIC ALL AGES

045 1-1/2 STORY - UNFINISHED ALL AGES

050 1-1/2 STORY FINISHED ALL AGES

060 2-STORY 1946 & NEWER

070 2-STORY 1945 & OLDER

075 2-1/2 STORY ALL AGES

080 SPLIT OR MULTI-LEVEL

085 SPLIT FOYER

090 DUPLEX - ALL STYLES AND AGES

120 1-STORY PUD (Planned Unit Development) - 1946 & NEWER

150 1-1/2 STORY PUD - ALL AGES

160 2-STORY PUD - 1946 & NEWER

180 PUD - MULTILEVEL - INCL SPLIT LEV/FOYER

190 2 FAMILY CONVERSION - ALL STYLES AND AGES

12 of 16

13 of 16

You can see that there is a more general categorization

described (roughly) by the first word of each category.

Create a feature containing only these first words

by splitting MSSubClass at the first underscore _.

(Hint: In the split method use an argument n=1.)

_の前の語の切り出し

14 of 16

5) Use a Grouped Transform

The value of a home often depends on

how it compares to typical homes in its neighborhood.

Create a feature MedNhbdArea 

that describes the median of GrLivArea 

grouped on Neighborhood.

15 of 16

Now you've made your first new feature set! �If you like, you can run the cell below �to score the model �with all of your new features added:��

から改善した

16 of 16

次のTopic へGO

Keep Going

Untangle spatial relationships 

by adding cluster labels to your dataset.