Získání dalších dat

Feature Engineering with PySpark

John Hogue

Lead Data Scientist, General Mills

Úvahy o externích datových sadách

VÝHODY

  • Přidání důležitých prediktorů
  • Doplnění/nahrazení hodnot
  • Levné nebo snadno dostupné

Kombinování dat

NEVÝHODY

  • Může zpomalit analýzu
  • Snadné způsobení úniku dat
  • Nutnost stát se expertem na datovou sadu

Odpovědnost

Feature Engineering with PySpark

O joinech

Orientace ve směrech dat

  • Vlevo: výchozí datová sada
  • Vpravo: nová datová sada k začlenění

SQL Joins

Feature Engineering with PySpark

Joiny v PySpark DataFrame

DataFrame.join(

other, # Other DataFrame to merge
on=None, # The keys to join on
how=None) # Type of join to perform (default is 'inner')
Feature Engineering with PySpark

Příklad joinu v PySparku

# Inspect dataframe head
hdf.show(2)
+----------+--------------------+
|        dt|                  nm|
+----------+--------------------+
|2012-01-02|        New Year Day|
|2012-01-16|Martin Luther Kin...|
+----------+--------------------+
only showing top 2 rows
# Specify join conditon
cond = [df['OFFMARKETDATE'] == hdf['dt']]

# Join two hdf onto df df = df.join(hdf, on=cond, 'left')
# How many sales occurred on bank holidays? df.where(~df['nm'].isNull()).count()
0
Feature Engineering with PySpark

SparkSQL Join

  • Aplikujte SQL na svůj dataframe
# Register the dataframe as a temp table
df.createOrReplaceTempView("df")
hdf.createOrReplaceTempView("hdf")
# Write a SQL Statement
sql_df = spark.sql("""
                      SELECT 
                        *
                      FROM df
                      LEFT JOIN hdf
                      ON df.OFFMARKETDATE = hdf.dt
                   """)
Feature Engineering with PySpark

Pojďme spojit data!

Feature Engineering with PySpark

Preparing Video For Download...