Machine Learning với 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|
+-----+-------+-------+------+----+----+------+------+----+-----------+
Loại bỏ trường maker và model.
# Hoặc bỏ các cột không cần... cars = cars.drop('maker', 'model')# ...hoặc chọn các cột cần giữ lại. 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|
+-------+------+----+----+------+------+----+-----------+
# Có bao nhiêu giá trị thiếu?
cars.filter('cyl IS NULL').count()
1
Loại bỏ bản ghi thiếu giá trị ở cột cylinders.
cars = cars.filter('cyl IS NOT NULL')
Loại bỏ bản ghi thiếu ở bất kỳ cột nào.
cars = cars.dropna()
from pyspark.sql.functions import round # Tạo cột 'mass' mới cars = cars.withColumn('mass', round(cars.weight / 2.205, 0))# Đổi 'length' sang mét 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|
+-------+-----+---+----+------+------+----+-----------+-----+
from pyspark.ml.feature import StringIndexer indexer = StringIndexer(inputCol='type', outputCol='type_idx')# Gán chỉ mục cho chuỗi indexer = indexer.fit(cars)# Tạo cột chỉ mục cars = indexer.transform(cars)
Dùng stringOrderType để đổi thứ tự.
+-------+--------+
| type|type_idx|
+-------+--------+
|Midsize| 0.0| <- giá trị xuất hiện nhiều nhất
| Small| 1.0|
|Compact| 2.0|
| Sporty| 3.0|
| Large| 4.0|
| Van| 5.0| <- giá trị ít xuất hiện nhất
+-------+--------+
# Đánh chỉ mục quốc gia xuất xứ:
#
# USA -> 0
# non-USA -> 1
#
cars = StringIndexer(
inputCol="origin",
outputCol="label"
).fit(cars).transform(cars)
+-------+-----+
| origin|label|
+-------+-----+
| USA| 0.0|
|non-USA| 1.0|
+-------+-----+
Dùng vector assembler để biến đổi dữ liệu.
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 với PySpark