第5章多層パーセプトロン
5.7.4. Data Preprocessing5.7.4 データの前処理
Let’s take a look at the first four and final two features as well as the label (SalePrice) from the first four examples.
英語のヒント
最初の四つの事例について、最初の四つと最後の二つの特徴量、そしてラベル(SalePrice)を見てみよう。
print(data.raw_train.iloc[:4, [0, 1, 2, 3, -3, -2, -1]]) Id MSSubClass MSZoning LotFrontage SaleType SaleCondition SalePrice
0 1 60 RL 65.0 WD Normal 208500
1 2 20 RL 80.0 WD Normal 181500
2 3 60 RL 68.0 WD Normal 223500
3 4 70 RL 60.0 WD Abnorml 140000We can see that in each example, the first feature is the identifier. This helps the model determine each training example. While this is convenient, it does not carry any information for prediction purposes. Hence, we will remove it from the dataset before feeding the data into the model. Furthermore, given a wide variety of data types, we will need to preprocess the data before we can start modeling.
英語のヒント
各事例で、最初の特徴量は識別子であることがわかる。これは、モデルが各訓練事例を見分けるのに役立つ。便利ではあるが、予測のための情報は何も含んでいない。そのため、データをモデルに入れる前に、データセットから取り除く。さらに、データ型がさまざまなので、モデリングを始める前にデータを前処理する必要がある。
Let’s start with the numerical features. First, we apply a heuristic, replacing all missing values by the corresponding feature’s mean. Then, to put all features on a common scale, we standardize the data by rescaling features to zero mean and unit variance:
英語のヒント
数値の特徴量から始めよう。まず、経験則を当てはめて、欠損値をすべて、対応する特徴量の平均で置き換える。次に、すべての特徴量を共通の尺度にそろえるため、特徴量を平均0、分散1にスケールし直して、データを標準化する。
(5.7.1)
where and denote mean and standard deviation, respectively. To verify that this indeed transforms our feature (variable) such that it has zero mean and unit variance, note that and that . Intuitively, we standardize the data for two reasons. First, it proves convenient for optimization. Second, because we do not know a priori which features will be relevant, we do not want to penalize coefficients assigned to one feature more than any other.
英語のヒント
ここでとは、それぞれ平均と標準偏差を表す。これで本当に特徴量(変数)が平均0、分散1になるよう変換されることを確かめるには、であり、であることに注意すればよい。直観的には、データを標準化する理由は二つある。第一に、最適化に好都合になる。第二に、どの特徴量が意味をもつかは事前にはわからないので、ある特徴量に割り当てられた係数を、ほかのものより重く罰したくはない。
Next we deal with discrete values. These include features such as “MSZoning”. We replace them by a one-hot encoding in the same way that we earlier transformed multiclass labels into vectors (see Section 4.1.1). For instance, “MSZoning” assumes the values “RL” and “RM”. Dropping the “MSZoning” feature, two new indicator features “MSZoning_RL” and “MSZoning_RM” are created with values being either 0 or 1. According to one-hot encoding, if the original value of “MSZoning” is “RL”, then “MSZoning_RL” is 1 and “MSZoning_RM” is 0. The
pandas package does this automatically for us.英語のヒント
次に、離散的な値を扱う。これには「MSZoning」のような特徴量が含まれる。以前に多クラスのラベルをベクトルに変換したのと同じやり方で(4.1.1節を参照)、これらをワンホット符号化で置き換える。たとえば、「MSZoning」は値「RL」と「RM」をとる。「MSZoning」の特徴量を取り除くと、値が0か1のどちらかである、二つの新しい指標特徴量「MSZoning_RL」と「MSZoning_RM」が作られる。ワンホット符号化に従えば、「MSZoning」の元の値が「RL」なら、「MSZoning_RL」は1で「MSZoning_RM」は0である。
pandasパッケージは、これを自動でやってくれる。@d2l.add_to_class(KaggleHouse)
def preprocess(self):
# Remove the ID and label columns
label = 'SalePrice'
features = pd.concat(
(self.raw_train.drop(columns=['Id', label]),
self.raw_val.drop(columns=['Id'])))
# Standardize numerical columns
numeric_features = features.dtypes[features.dtypes!='object'].index
features[numeric_features] = features[numeric_features].apply(
lambda x: (x - x.mean()) / (x.std()))
# Replace NAN numerical features by 0
features[numeric_features] = features[numeric_features].fillna(0)
# Replace discrete features by one-hot encoding
features = pd.get_dummies(features, dummy_na=True)
# Save preprocessed features
self.train = features[:self.raw_train.shape[0]].copy()
self.train[label] = self.raw_train[label]
self.val = features[self.raw_train.shape[0]:].copy()You can see that this conversion increases the number of features from 79 to 331 (excluding ID and label columns).
英語のヒント
この変換によって、特徴量の数が79から331に増える(IDとラベルの列を除く)ことがわかる。
data.preprocess()
data.train.shape(1460, 331)