-
Notifications
You must be signed in to change notification settings - Fork 0
/
titanic_classification.py
173 lines (77 loc) · 2.55 KB
/
titanic_classification.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
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
#!/usr/bin/env python
# coding: utf-8
# # Logistic Regression Code Along
# This is a code along of the famous titanic dataset, its always nice to start off with this dataset because it is an example you will find across pretty much every data analysis language.
# In[1]:
from pyspark.sql import SparkSession
# In[2]:
spark = SparkSession.builder.appName('myproj').getOrCreate()
# In[3]:
data = spark.read.csv('titanic.csv',inferSchema=True,header=True)
# In[4]:
data.printSchema()
# In[7]:
data.columns
# In[8]:
my_cols = data.select(['Survived',
'Pclass',
'Sex',
'Age',
'SibSp',
'Parch',
'Fare',
'Embarked'])
# In[29]:
my_final_data = my_cols.na.drop()
# ### Working with Categorical Columns
#
# Let's break this down into multiple steps to make it all clear.
# In[12]:
from pyspark.ml.feature import (VectorAssembler,VectorIndexer,
OneHotEncoder,StringIndexer)
# In[13]:
#indexers = [StringIndexer(inputCol=column, outputCol=column+"_index").fit(df).transform(df) for column in df.columns ]
gender_indexer = StringIndexer(inputCol='Sex',outputCol='SexIndex')
gender_encoder = OneHotEncoder(inputCol='SexIndex',outputCol='SexVec')
# In[14]:
embark_indexer = StringIndexer(inputCol='Embarked',outputCol='EmbarkIndex')
embark_encoder = OneHotEncoder(inputCol='EmbarkIndex',outputCol='EmbarkVec')
# In[15]:
assembler = VectorAssembler(inputCols=['Pclass',
'SexVec',
'Age',
'SibSp',
'Parch',
'Fare',
'EmbarkVec'],outputCol='features')
# In[30]:
from pyspark.ml.classification import LogisticRegression
# ## Pipelines
#
# Let's see an example of how to use pipelines (we'll get a lot more practice with these later!)
# In[17]:
from pyspark.ml import Pipeline
# In[18]:
log_reg_titanic = LogisticRegression(featuresCol='features',labelCol='Survived')
# In[19]:
pipeline = Pipeline(stages=[gender_indexer,embark_indexer,
gender_encoder,embark_encoder,
assembler,log_reg_titanic])
# In[20]:
train_titanic_data, test_titanic_data = my_final_data.randomSplit([0.7,.3])
# In[21]:
fit_model = pipeline.fit(train_titanic_data)
# In[22]:
results = fit_model.transform(test_titanic_data)
# In[23]:
from pyspark.ml.evaluation import BinaryClassificationEvaluator
# In[24]:
my_eval = BinaryClassificationEvaluator(rawPredictionCol='prediction',
labelCol='Survived')
# In[26]:
results.select('Survived','prediction').show()
# In[27]:
AUC = my_eval.evaluate(results)
# In[28]:
AUC
# ## Great Job!