-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
174 lines (139 loc) · 6.24 KB
/
Copy pathtrain.py
File metadata and controls
174 lines (139 loc) · 6.24 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# import libraries
import re
import numpy as np
import pandas as pd
from pyspark.sql import SparkSession
from pyspark.sql.types import StringType, LongType
from pyspark.sql.functions import col, sum, countDistinct, udf, max, min, isnull, datediff
from pyspark.ml.feature import VectorAssembler, StringIndexer, OneHotEncoderEstimator
from pyspark.mllib.evaluation import MulticlassMetrics
from pyspark.ml.tuning import CrossValidator, ParamGridBuilder
from pyspark.ml.evaluation import BinaryClassificationEvaluator
from pyspark.ml.classification import LogisticRegression
def get_metrics(res):
"""
Provides accuracy, precision, recall and F1 score given prediction results.
Arguments:
res: Test prediction results
Returns:
None.
Prints the accuracy, precision, recall and f1 score.
"""
total = res.count()
tp = res.where((res.label==1) & (res.prediction==1)).count()
tn = res.where((res.label==0) & (res.prediction==0)).count()
fp = res.where((res.label==0) & (res.prediction==1)).count()
fn = res.where((res.label==1) & (res.prediction==0)).count()
accuracy = (1.0*tp + tn) / total
precision = 1.0*tp / (tp + fp)
recall = 1.0*tp / (tp + fn)
f1 = 2.0 * (precision * recall) / (precision + recall)
print('Accuracy: ', round(accuracy, 2))
print('Precision: ', round(precision, 2))
print('Recall: ', round(recall, 2))
print('F1-Score: ', round(f1, 2))
def extract_features(df):
"""
Create a vector Assembler of the features.
Arguments:
df: Dataframe consisting the relevant data columns.
Returns:
Dataframe with extracted features in the column "features".
"""
feature_df = df.select("userId").distinct()
col_names = []
ts_dt_udf = udf(lambda x: x//1000, LongType())
df = df.withColumn("registration_dt", ts_dt_udf(df.registration).cast("timestamp"))
df = df.withColumn("timestamp_dt", ts_dt_udf(df.ts).cast("timestamp"))
# Session Counts
session_counts = df.groupby('userId').agg(countDistinct('sessionId').alias('session_count'))
feature_df = feature_df.join(session_counts, on="userId")
col_names.append("session_count")
# Page Counts
pages = df.select('page').distinct().sort('page')
pages_list = [r.page for r in pages.collect()]
page_counts = df.groupby('userId').pivot('page', pages_list).count()
# Drop the "Cancel" page column
# Fill NaNs with 0 - This will inherently transform "Cancellation Confirmation" column into "label"
# with 1 as churned and 0 as non churned
page_counts = page_counts.drop("Cancel")
page_counts = page_counts.fillna(value=0)
page_counts = page_counts.withColumnRenamed("Cancellation Confirmation", "label")
# Join these feature columns to our feature dataframe
feature_df = feature_df.join(page_counts, on="userId")
# Normalize by session counts
cut_columns = {'userId', 'session_count', 'label'}
remaining_cols = sorted(list(set(feature_df.columns) - cut_columns))
for column in remaining_cols:
feature_df = feature_df.withColumn(column, col(column) / feature_df.session_count)
col_names.extend(remaining_cols)
# Time since registration
user_ages = df.select(["userId", datediff("timestamp_dt", "registration_dt")]).groupBy("userId").max().select("userId", col("max(datediff(timestamp_dt, registration_dt))").alias("age"))
feature_df = feature_df.join(user_ages, on="userId")
col_names.append("age")
# Total number of events
user_number_events = df.groupBy("userId").count().select("userId", col("count").alias("num_events"))
feature_df = feature_df.join(user_number_events, on="userId")
col_names.append("num_events")
# Include device categorical variable
device_udf = udf(lambda x: str(re.findall(r'\((.*?)\)', x)[0].split(";")[0].split()[0]) if x is not None else None, StringType())
df = df.withColumn("device", device_udf(df.userAgent))
df_device = df.select(["userId", "device"]).distinct()
df_device = StringIndexer(inputCol = "device", outputCol="device_index").fit(df_device).transform(df_device)
df_device = OneHotEncoderEstimator(inputCols=["device_index"], outputCols=["device_classVec"]).fit(df_device).transform(df_device)
feature_df = feature_df.join(df_device.select("userId", "device_classVec"), on="userId")
col_names.append("device_classVec")
print(col_names)
# Assemble the vector
assembler = VectorAssembler(inputCols=col_names, outputCol='features')
return assembler.transform(feature_df)
def preprocess(df):
"""
Cleans the dataset of unwanted rows.
"""
df = df.filter(df.userId != "")
return df
def split_train_test(df_features):
"""
Splits the dataset into training and testing subsets.
"""
train, test = df_features.randomSplit([0.8, 0.2], 42)
return train, test
def trainLR(train):
"""
Trains the model given train dataset
"""
lr = LogisticRegression(labelCol="label", featuresCol="features", maxIter=10, regParam=0.0, elasticNetParam=0)
model = lr.fit(train)
return model
def crossValidationLR(train):
"""
Performs grid search using cross validation and returns a CV model.
"""
paramGrid = ParamGridBuilder()\
.addGrid(lr.regParam, [0.1, 0.01]) \
.addGrid(lr.fitIntercept, [False, True])\
.addGrid(lr.elasticNetParam, [0.0, 0.5, 1.0])\
.build()
crossval = CrossValidator(estimator=lr,
estimatorParamMaps=paramGrid,
evaluator=BinaryClassificationEvaluator(),
numFolds=3)
model = crossval.fit(train)
return model
def evaluate(model, test):
"""
Evaluates the trained model on the test set and prints out evaluation metrics.
"""
predictions = model.transform(test)
get_metrics(predictions)
if __name__ == "__main__":
# create a Spark session
spark = SparkSession.builder.appName("sparkify").getOrCreate()
path = "medium-sparkify-event-data.json"
df = spark.read.json(path)
df = preprocess(df)
df_features = extract_features(df)
train, test = split_train_test(df_features)
model = trainLR(train)
evaluate(model, test)