データ準備

Machine Learning with PySpark

Andrew Collier

Data Scientist, Fathom Data

その列は本当に必要?

+-----+-------+-------+------+----+----+------+------+----+-----------+
|maker|  model| origin|  type| cyl|size|weight|length| rpm|consumption|
+-----+-------+-------+------+----+----+------+------+----+-----------+
|Mazda|   RX-7|non-USA|Sporty|null| 1.3|  2895| 169.0|6500|       9.41|
|  Geo|  Metro|non-USA| Small|   3| 1.0|  1695| 151.0|5700|        4.7|
| Ford|Festiva|    USA| Small|   4| 1.3|  1845| 141.0|5000|       7.13|
+-----+-------+-------+------+----+----+------+------+----+-----------+

maker と model を削除します。

Machine Learning with PySpark

列の削除

# 不要な列を drop するか...
cars = cars.drop('maker', 'model')

# ...保持したい列だけを select します。 cars = cars.select('origin', 'type', 'cyl', 'size', 'weight', 'length', 'rpm', 'consumption')
+-------+------+----+----+------+------+----+-----------+
| origin|  type| cyl|size|weight|length| rpm|consumption|
+-------+------+----+----+------+------+----+-----------+
|non-USA|Sporty|null| 1.3|  2895| 169.0|6500|       9.41|
|non-USA| Small|   3| 1.0|  1695| 151.0|5700|        4.7|
|    USA| Small|   4| 1.3|  1845| 141.0|5000|       7.13|
+-------+------+----+----+------+------+----+-----------+
Machine Learning with PySpark

欠損の除外

# 欠損はいくつ?
cars.filter('cyl IS NULL').count()
1

cylinders 列で欠損の行を削除します。

cars = cars.filter('cyl IS NOT NULL')

任意の列に欠損がある行をすべて削除します。

cars = cars.dropna()
Machine Learning with PySpark

列の変換(ミューテーション)

from pyspark.sql.functions import round

# 新しい 'mass' 列を作成
cars = cars.withColumn('mass', round(cars.weight / 2.205, 0))

# length をメートルに変換 cars = cars.withColumn('length', round(cars.length * 0.0254, 3))
+-------+-----+---+----+------+------+----+-----------+-----+
| origin| type|cyl|size|weight|length| rpm|consumption| mass|
+-------+-----+---+----+------+------+----+-----------+-----+
|non-USA|Small|  3| 1.0|  1695| 3.835|5700|        4.7|769.0|
|    USA|Small|  4| 1.3|  1845| 3.581|5000|       7.13|837.0|
|non-USA|Small|  3| 1.3|  1965| 4.089|6000|       5.47|891.0|
+-------+-----+---+----+------+------+----+-----------+-----+
Machine Learning with PySpark

カテゴリカルデータのインデックス化

from pyspark.ml.feature import StringIndexer

indexer = StringIndexer(inputCol='type',
                        outputCol='type_idx')

# 文字列にインデックス値を割り当て indexer = indexer.fit(cars)
# インデックス列を作成 cars = indexer.transform(cars)

stringOrderType で順序を変更できます。

+-------+--------+
|   type|type_idx|
+-------+--------+
|Midsize|     0.0| <- 最頻値
|  Small|     1.0|
|Compact|     2.0|
| Sporty|     3.0|
|  Large|     4.0|
|    Van|     5.0| <- 最もまれ
+-------+--------+
Machine Learning with PySpark

原産国のインデックス化

# 原産国をインデックス化:
#
# USA     -> 0
# non-USA -> 1
#
cars = StringIndexer(
  inputCol="origin",
  outputCol="label"
).fit(cars).transform(cars)
+-------+-----+
| origin|label|
+-------+-----+
|    USA|  0.0|
|non-USA|  1.0|
+-------+-----+
Machine Learning with PySpark

列のアセンブル

ベクトルアセンブラーで変換します。

from pyspark.ml.feature import VectorAssembler

assembler = VectorAssembler(inputCols=['cyl', 'size'], outputCol='features')

assembler.transform(cars)
+---+----+---------+
|cyl|size| features|
+---+----+---------+
|  3| 1.0|[3.0,1.0]|
|  4| 1.3|[4.0,1.3]|
|  3| 1.3|[3.0,1.3]|
+---+----+---------+
Machine Learning with PySpark

Passons à la pratique !

Machine Learning with PySpark

Preparing Video For Download...