forked from jonesberg/DataAnalysisWithPythonAndPySpark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_prep.py
More file actions
169 lines (132 loc) · 3.77 KB
/
data_prep.py
File metadata and controls
169 lines (132 loc) · 3.77 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
#!/usr/bin/env python3
# pylint: disable=missing-function-docstring
from typing import Optional
import pyspark.sql.functions as F
import pyspark.sql.types as T
from pyspark.ml.feature import Imputer, MinMaxScaler, VectorAssembler
from pyspark.sql import SparkSession
spark = (
SparkSession.builder.appName("Recipes ML model - Are you a dessert?")
.config("spark.driver.memory", "8g")
.getOrCreate()
)
food = spark.read.csv(
"./data/recipes/epi_r.csv", inferSchema=True, header=True
)
def sanitize_column_name(name):
"""Drops unwanted characters from the column name.
We replace spaces, dashes and slashes with underscore,
and only keep alphanumeric characters."""
answer = name
for i, j in ((" ", "_"), ("-", "_"), ("/", "_"), ("&", "and")):
answer = answer.replace(i, j)
return "".join(
[
char
for char in answer
if char.isalpha() or char.isdigit() or char == "_"
]
)
food = food.toDF(*[sanitize_column_name(name) for name in food.columns])
# Keeping only the relevant values for `cakeweek` and `wasteless`.
# Check the exercises for a more robust approach to this.
food = food.where(
(F.col("cakeweek").isin([0.0, 1.0]) | F.col("cakeweek").isNull())
& (F.col("wasteless").isin([0.0, 1.0]) | F.col("wasteless").isNull())
)
IDENTIFIERS = ["title"]
CONTINUOUS_COLUMNS = [
"rating",
"calories",
"protein",
"fat",
"sodium",
]
TARGET_COLUMN = ["dessert"]
BINARY_COLUMNS = [
x
for x in food.columns
if x not in CONTINUOUS_COLUMNS
and x not in TARGET_COLUMN
and x not in IDENTIFIERS
]
food = food.dropna(
how="all",
subset=[x for x in food.columns if x not in IDENTIFIERS],
)
food = food.dropna(subset=TARGET_COLUMN)
@F.udf(T.BooleanType())
def is_a_number(value: Optional[str]) -> bool:
if not value:
return True
try:
_ = float(value)
except ValueError:
return False
return True
for column in ["rating", "calories"]:
food = food.where(is_a_number(F.col(column)))
food = food.withColumn(column, F.col(column).cast(T.DoubleType()))
# TODO: REMOVE THIS
maximum = {
"calories": 3203.0,
"protein": 173.0,
"fat": 207.0,
"sodium": 5661.0,
}
inst_sum_of_binary_columns = [
F.sum(F.col(x)).alias(x) for x in BINARY_COLUMNS
]
sum_of_binary_columns = (
food.select(*inst_sum_of_binary_columns).head().asDict()
)
num_rows = food.count()
too_rare_features = [
k
for k, v in sum_of_binary_columns.items()
if v < 10 or v > (num_rows - 10)
]
BINARY_COLUMNS = list(set(BINARY_COLUMNS) - set(too_rare_features))
food = food.withColumn(
"protein_ratio", F.col("protein") * 4 / F.col("calories")
).withColumn("fat_ratio", F.col("fat") * 9 / F.col("calories"))
CONTINUOUS_COLUMNS += ["protein_ratio", "fat_ratio"]
from pyspark.ml.classification import LogisticRegression
lr = LogisticRegression(
featuresCol="features", labelCol="dessert", predictionCol="prediction"
)
from pyspark.ml import Pipeline
import pyspark.ml.feature as MF
imputer = MF.Imputer( # <1>
strategy="mean",
inputCols=[
"calories",
"protein",
"fat",
"sodium",
"protein_ratio",
"fat_ratio",
],
outputCols=[
"calories_i",
"protein_i",
"fat_i",
"sodium_i",
"protein_ratio_i",
"fat_ratio_i",
],
)
continuous_assembler = MF.VectorAssembler(
inputCols=["rating", "calories_i", "protein_i", "fat_i", "sodium_i"],
outputCol="continuous",
)
continuous_scaler = MF.MinMaxScaler(
inputCol="continuous",
outputCol="continuous_scaled",
)
preml_assembler = MF.VectorAssembler(
inputCols=BINARY_COLUMNS
+ ["continuous_scaled"]
+ ["protein_ratio_i", "fat_ratio_i"],
outputCol="features",
)