PySpark के साथ Machine Learning
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 फील्ड हटाएँ.
# जिन कॉलम की ज़रूरत नहीं, उन्हें 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|
+-------+------+----+----+------+------+----+-----------+
# कितने missing values हैं?
cars.filter('cyl IS NULL').count()
1
cylinders कॉलम में missing values वाले रिकॉर्ड्स हटाएँ.
cars = cars.filter('cyl IS NOT NULL')
किसी भी कॉलम में missing value वाले रिकॉर्ड्स हटाएँ.
cars = cars.dropna()
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|
+-------+-----+---+----+------+------+----+-----------+-----+
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| <- सबसे कम बार आने वाला मान
+-------+--------+
# मूल देश को इंडेक्स करें:
#
# USA -> 0
# non-USA -> 1
#
cars = StringIndexer(
inputCol="origin",
outputCol="label"
).fit(cars).transform(cars)
+-------+-----+
| origin|label|
+-------+-----+
| USA| 0.0|
|non-USA| 1.0|
+-------+-----+
डेटा ट्रांसफॉर्म करने के लिए vector assembler उपयोग करें.
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]|
+---+----+---------+
PySpark के साथ Machine Learning