-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_features.py
45 lines (35 loc) · 1.37 KB
/
generate_features.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Script to generate features.
"""
import os
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
from utils.constants import AREA_CODES, STATES, FEATURE_COLS, TARGET_COL, USER_COL
PREPROCESSED_DATA = os.path.join(os.getenv("TEMP_DATA_BUCKET"),
os.getenv("PREPROCESSED_DATA"))
FEATURES_DATA = os.path.join(os.getenv("TEMP_DATA_BUCKET"),
os.getenv("FEATURES_DATA"))
def generate_features(spark):
"""Generate features."""
joined_df = spark.read.parquet(PREPROCESSED_DATA)
for area_code in AREA_CODES:
joined_df = joined_df.withColumn(
"Area_Code={}".format(area_code),
F.when(F.col("Area_Code") == area_code, 1).otherwise(0)
)
for state in STATES:
joined_df = joined_df.withColumn(
"State={}".format(state),
F.when(F.col("State") == state, 1).otherwise(0)
)
joined_df = joined_df.select(FEATURE_COLS + [TARGET_COL] + [USER_COL])
return joined_df
def main():
"""Generate features"""
print("\tGenerating features")
with SparkSession.builder.appName("FeatureGeneration").getOrCreate() as spark:
spark.sparkContext.setLogLevel("FATAL")
features_data = generate_features(spark).toPandas()
features_data.to_csv(FEATURES_DATA, index=False)
if __name__ == "__main__":
main()