第2章準備
2.2.2. Data Preparation2.2.2 データの準備
In supervised learning, we train models to predict a designated target value, given some set of input values. Our first step in processing the dataset is to separate out columns corresponding to input versus target values. We can select columns either by name or via integer-location based indexing (
iloc).英語のヒント
教師あり学習では、入力値の集合が与えられたとき、指定された目標値を予測するようにモデルを訓練する。データセットを処理する最初の手順は、入力値に対応する列と目標値に対応する列とを切り分けることである。列は、名前でも、整数の位置にもとづくインデックス指定(
iloc)でも選べる。You might have noticed that
pandas replaced all CSV entries with value NA with a special NaN (not a number) value. This can also happen whenever an entry is empty, e.g., “3,,,270000”. These are called missing values and they are the “bed bugs” of data science, a persistent menace that you will confront throughout your career. Depending upon the context, missing values might be handled either via imputation or deletion. Imputation replaces missing values with estimates of their values while deletion simply discards either those rows or those columns that contain missing values.英語のヒント
pandasが、値がNAのCSVの項目をすべて、特別なNaN(非数)の値に置き換えたことに気づいたかもしれない。これは、項目が空のとき、たとえば「3,,,270000」のときにも起こる。これらは欠損値と呼ばれ、データサイエンスの「トコジラミ」、つまり、キャリアを通じてずっと向き合うことになるしつこい厄介者である。文脈に応じて、欠損値は補完か削除で扱う。補完は欠損値をその値の推定値で置き換え、削除は欠損値を含む行か列をただ捨てる。Here are some common imputation heuristics. For categorical input fields, we can treat
NaN as a category. Since the RoofType column takes values Slate and NaN, pandas can convert this column into two columns RoofType_Slate and RoofType_nan. A row whose roof type is Slate will set values of RoofType_Slate and RoofType_nan to 1 and 0, respectively. The converse holds for a row with a missing RoofType value.英語のヒント
よく使う補完の経験則をいくつか挙げる。カテゴリーの入力フィールドなら、
NaNを一つのカテゴリーとして扱える。RoofTypeの列はSlateとNaNの値をとるので、pandasはこの列をRoofType_SlateとRoofType_nanの二つの列に変換できる。屋根の種類がSlateの行は、RoofType_SlateとRoofType_nanの値をそれぞれ1と0にする。RoofTypeの値が欠けている行では逆になる。inputs, targets = data.iloc[:, 0:2], data.iloc[:, 2]
inputs = pd.get_dummies(inputs, dummy_na=True)
print(inputs) NumRooms RoofType_Slate RoofType_nan
0 NaN False True
1 2.0 False True
2 4.0 True False
3 NaN False TrueFor missing numerical values, one common heuristic is to replace the
NaN entries with the mean value of the corresponding column.英語のヒント
数値の欠損値については、よくある経験則の一つは、
NaNの項目を対応する列の平均値で置き換えることである。inputs = inputs.fillna(inputs.mean())
print(inputs) NumRooms RoofType_Slate RoofType_nan
0 3.0 False True
1 2.0 False True
2 4.0 True False
3 3.0 False True