forked from springboard-curriculum/mec-mini-projects
/
Mini_Project_Spark_ML.py
761 lines (533 loc) · 24.5 KB
/
Mini_Project_Spark_ML.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
# Databricks notebook source
# MAGIC %md
# MAGIC ## Exercise Overview
# MAGIC In this exercise we will play with Spark [Datasets & Dataframes](https://spark.apache.org/docs/latest/sql-programming-guide.html#datasets-and-dataframes), some [Spark SQL](https://spark.apache.org/docs/latest/sql-programming-guide.html#sql), and build a couple of binary classifiaction models using [Spark ML](https://spark.apache.org/docs/latest/ml-guide.html) (with some [MLlib](https://spark.apache.org/mllib/) too).
# MAGIC <br><br>
# MAGIC The set up and approach will not be too dissimilar to the standard type of approach you might do in [Sklearn](http://scikit-learn.org/stable/index.html). Spark has matured to the stage now where for 90% of what you need to do (when analysing tabular data) should be possible with Spark dataframes, SQL, and ML libraries. This is where this exercise is mainly trying to focus.
# MAGIC <br>
# MAGIC Feel free to adapt this exercise to play with other datasets readily availabe in the Databricks enviornment (they are listed in a cell below).
# MAGIC
# MAGIC ##### Getting Started
# MAGIC To get started you will need to create and attach a databricks spark cluster to this notebook. This notebook was developed on a cluster created with:
# MAGIC - Databricks Runtime Version 4.0 (includes Apache Spark 2.3.0, Scala 2.11)
# MAGIC - Python Version 3
# MAGIC
# MAGIC ##### Links & References
# MAGIC
# MAGIC Some useful links and references of sources used in creating this exercise:
# MAGIC
# MAGIC **Note**: Right click and open as new tab!
# MAGIC <br>
# MAGIC 1. [Latest Spark Docs](https://spark.apache.org/docs/latest/index.html)
# MAGIC 1. [Databricks Homepage](https://databricks.com/)
# MAGIC 1. [Databricks Community Edition FAQ](https://databricks.com/product/faq/community-edition)
# MAGIC 1. [Databricks Self Paced Training](https://databricks.com/training-overview/training-self-paced)
# MAGIC 1. [Databricks Notebook Guide](https://docs.databricks.com/user-guide/notebooks/index.html)
# MAGIC 1. [Databricks Binary Classification Tutorial](https://docs.databricks.com/spark/latest/mllib/binary-classification-mllib-pipelines.html#binary-classification)
# COMMAND ----------
# MAGIC %md
# MAGIC #### Get Data
# MAGIC
# MAGIC Here we will pull in some sample data that is already pre-loaded onto all databricks clusters.
# MAGIC
# MAGIC Feel free to adapt this notebook later to play around with a different dataset if you like (all available are listed in a cell below).
# COMMAND ----------
# display datasets already in databricks
display(dbutils.fs.ls("/databricks-datasets"))
# COMMAND ----------
# MAGIC %md
# MAGIC Lets take a look at the '**adult**' dataset on the filesystem. This is the typical US Census data you often see online in tutorials. [Here](https://archive.ics.uci.edu/ml/datasets/adult) is the same data in the UCI repository.
# MAGIC
# MAGIC _As an aside: [here](https://github.com/GoogleCloudPlatform/cloudml-samples/tree/master/census) this same dataset is used as a quickstart example for Google CLoud ML & Tensorflow Estimator API (in case youd be interested in playing with tensorflow on the same dataset as here)._
# COMMAND ----------
# MAGIC %fs ls databricks-datasets/adult/adult.data
# COMMAND ----------
# MAGIC %md
# MAGIC **Note**: Above %fs is just some file system cell magic that is specific to databricks. More info [here](https://docs.databricks.com/user-guide/notebooks/index.html#mix-languages).
# COMMAND ----------
# MAGIC %md
# MAGIC #### Spark SQL
# MAGIC
# MAGIC Below we will use Spark SQL to load in the data and then register it as a Dataframe aswell. So the end result will be a Spark SQL table called _adult_ and a Spark Dataframe called _df_adult_.
# MAGIC <br><br>
# MAGIC This is an example of the flexibility in Spark in that you could do lots of you ETL and data wrangling using either Spark SQL or Dataframes and pyspark. Most of the time it's a case of using whatever you are most comfortable with.
# MAGIC <br><br>
# MAGIC When you get more advanced then you might looking the pro's and con's of each and when you might favour one or the other (or operating direclty on RDD's), [here](https://databricks.com/blog/2016/07/14/a-tale-of-three-apache-spark-apis-rdds-dataframes-and-datasets.html) is a good article on the issues. For now, no need to overthink it!
# COMMAND ----------
# MAGIC %sql
# MAGIC -- drop the table if it already exists
# MAGIC DROP TABLE IF EXISTS adult
# COMMAND ----------
# MAGIC %sql
# MAGIC -- create a new table in Spark SQL from the datasets already loaded in the underlying filesystem.
# MAGIC -- In the real world you might be pointing at a file on HDFS or a hive table etc.
# MAGIC CREATE TABLE adult (
# MAGIC age DOUBLE,
# MAGIC workclass STRING,
# MAGIC fnlwgt DOUBLE,
# MAGIC education STRING,
# MAGIC education_num DOUBLE,
# MAGIC marital_status STRING,
# MAGIC occupation STRING,
# MAGIC relationship STRING,
# MAGIC race STRING,
# MAGIC sex STRING,
# MAGIC capital_gain DOUBLE,
# MAGIC capital_loss DOUBLE,
# MAGIC hours_per_week DOUBLE,
# MAGIC native_country STRING,
# MAGIC income STRING)
# MAGIC USING com.databricks.spark.csv
# MAGIC OPTIONS (path "/databricks-datasets/adult/adult.data", header "true")
# COMMAND ----------
# look at the data
#spark.sql("SELECT * FROM adult LIMIT 5").show()
# this will look prettier in Databricks if you use display() instead
display(spark.sql("SELECT * FROM adult LIMIT 5"))
# COMMAND ----------
# MAGIC %md
# MAGIC If you are more comfortable with SQL then as you can see below, its very easy to just get going with writing standard SQL type code to analyse your data, do data wrangling and create new dataframes.
# COMMAND ----------
# Lets get some summary marital status rates by occupation
result = spark.sql(
"""
SELECT
occupation,
SUM(1) as n,
ROUND(AVG(if(LTRIM(marital_status) LIKE 'Married-%',1,0)),2) as married_rate,
ROUND(AVG(if(lower(marital_status) LIKE '%widow%',1,0)),2) as widow_rate,
ROUND(AVG(if(LTRIM(marital_status) = 'Divorced',1,0)),2) as divorce_rate,
ROUND(AVG(if(LTRIM(marital_status) = 'Separated',1,0)),2) as separated_rate,
ROUND(AVG(if(LTRIM(marital_status) = 'Never-married',1,0)),2) as bachelor_rate
FROM
adult
GROUP BY 1
ORDER BY n DESC
""")
display(result)
# COMMAND ----------
# MAGIC %md
# MAGIC You can easily register dataframes as a table for Spark SQL too. So this way you can easily move between Dataframes and Spark SQL for whatever reason.
# COMMAND ----------
# register the df we just made as a table for spark sql
sqlContext.registerDataFrameAsTable(result, "result")
spark.sql("SELECT * FROM result").show(5)
# COMMAND ----------
# MAGIC %md
# MAGIC #### <span style="color:darkblue">Question 1</span>
# MAGIC
# MAGIC 1. Write some spark sql to get the top 'bachelor_rate' by 'education' group?
# COMMAND ----------
### Question 1.1 Answer ###
result = spark.sql(
"""
SELECT
education,
ROUND(AVG(if(LTRIM(marital_status) = 'Never-married',1,0)),2) as bachelor_rate
FROM
adult
GROUP BY education
ORDER BY bachelor_rate DESC
LIMIT 1
""")
result.show()
# COMMAND ----------
# MAGIC %md
# MAGIC #### Spark DataFrames
# MAGIC
# MAGIC Below we will create our DataFrame from the SQL table and do some similar analysis as we did with Spark SQL but using the DataFrames API.
# COMMAND ----------
# register a df from the sql df
df_adult = spark.table("adult")
cols = df_adult.columns # this will be used much later in the notebook, ignore for now
# COMMAND ----------
# look at df schema
df_adult.printSchema()
# COMMAND ----------
# look at the df
display(df_adult)
#df_adult.show(5)
# COMMAND ----------
# MAGIC %md
# MAGIC Below we will do a similar calculation to what we did above but using the DataFrames API
# COMMAND ----------
# import what we will need
from pyspark.sql.functions import when, col, mean, desc, round
# wrangle the data a bit
df_result = df_adult.select(
df_adult['occupation'],
# create a 1/0 type col on the fly
when( col('marital_status') == ' Divorced' , 1 ).otherwise(0).alias('is_divorced')
)
# do grouping (and a round)
df_result = df_result.groupBy('occupation').agg(round(mean('is_divorced'),2).alias('divorced_rate'))
# do ordering
df_result = df_result.orderBy(desc('divorced_rate'))
# show results
df_result.show(5)
# COMMAND ----------
# MAGIC %md
# MAGIC As you can see the dataframes api is a bit more verbose then just expressing what you want to do in standard SQL.<br><br>But some prefer it and might be more used to it, and there could be cases where expressing what you need to do might just be better using the DataFrame API if it is too complicated for a simple SQL expression for example of maybe involves recursion of some type.
# COMMAND ----------
# MAGIC %md
# MAGIC #### <span style="color:darkblue">Question 2</span>
# MAGIC 1. Write some pyspark to get the top 'bachelor_rate' by 'education' group using DataFrame operations?
# COMMAND ----------
### Question 2.1 Answer ###
# wrangle the data a bit
df_result = df_adult.select(
df_adult['education'],
# create a 1/0 type col on the fly
when( col('marital_status') == ' Never-married' , 1 ).otherwise(0).alias('is_bachelor')
)
# do grouping (and a round)
df_result = df_result.groupBy('education').agg(round(mean('is_bachelor'),2).alias('bachelor_rate'))
# do ordering
df_result = df_result.orderBy(desc('bachelor_rate'))
# show results
df_result.show(1)
# COMMAND ----------
# MAGIC %md
# MAGIC #### Explore & Visualize Data
# MAGIC
# MAGIC It's very easy to [collect()](https://spark.apache.org/docs/latest/rdd-programming-guide.html#printing-elements-of-an-rdd) your Spark DataFrame data into a Pandas df and then continue to analyse or plot as you might normally.
# MAGIC <br><br>
# MAGIC Obviously if you try to collect() a huge DataFrame then you will run into issues, so usually you would only collect aggregated or sampled data into a Pandas df.
# COMMAND ----------
import pandas as pd
# do some analysis
result = spark.sql(
"""
SELECT
occupation,
AVG(IF(income = ' >50K',1,0)) as plus_50k
FROM
adult
GROUP BY 1
ORDER BY 2 DESC
""")
# collect results into a pandas df
df_pandas = pd.DataFrame(
result.collect(),
columns=result.schema.names
)
# look at df
print(df_pandas.head())
# COMMAND ----------
print(df_pandas.describe())
# COMMAND ----------
print(df_pandas.info())
# COMMAND ----------
# MAGIC %md
# MAGIC Here we will just do some very basic plotting to show how you might collect what you are interested in into a Pandas DF and then just plot any way you normally would.
# MAGIC
# MAGIC For simplicity we are going to use the plotting functionality built into pandas (you could make this a pretty as you want).
# COMMAND ----------
import matplotlib.pyplot as plt
# i like ggplot style
plt.style.use('ggplot')
# get simple plot on the pandas data
myplot = df_pandas.plot(kind='barh', x='occupation', y='plus_50k')
# display the plot (note - display() is a databricks function -
# more info on plotting in Databricks is here: https://docs.databricks.com/user-guide/visualizations/matplotlib-and-ggplot.html)
display(myplot.figure)
# COMMAND ----------
# MAGIC %md
# MAGIC You can also easily get summary stats on a Spark DataFrame like below. [Here](https://databricks.com/blog/2015/06/02/statistical-and-mathematical-functions-with-dataframes-in-spark.html) is a nice blog post that has more examples.<br><br>So this is an example of why you might want to move from Spark SQL into DataFrames API as being able to just call describe() on the Spark DF is easier then trying to do the equivilant in Spark SQL.
# COMMAND ----------
# describe df
df_adult.select(df_adult['age'],df_adult['education_num']).describe().show()
# COMMAND ----------
# MAGIC %md
# MAGIC ### ML Pipeline - Logistic Regression vs Random Forest
# MAGIC
# MAGIC Below we will create two [Spark ML Pipelines](https://spark.apache.org/docs/latest/ml-pipeline.html) - one that fits a logistic regression and one that fits a random forest. We will then compare the performance of each.
# MAGIC
# MAGIC **Note**: A lot of the code below is adapted from [this example](https://docs.databricks.com/spark/latest/mllib/binary-classification-mllib-pipelines.html).
# COMMAND ----------
from pyspark.ml import Pipeline
from pyspark.ml.feature import OneHotEncoder, StringIndexer, VectorAssembler
categoricalColumns = ["workclass", "education", "marital_status", "occupation", "relationship", "race", "sex", "native_country"]
stages = [] # stages in our Pipeline
for categoricalCol in categoricalColumns:
# Category Indexing with StringIndexer
stringIndexer = StringIndexer(inputCol=categoricalCol, outputCol=categoricalCol + "Index")
# Use OneHotEncoder to convert categorical variables into binary SparseVectors
# encoder = OneHotEncoderEstimator(inputCol=categoricalCol + "Index", outputCol=categoricalCol + "classVec")
encoder = OneHotEncoder(inputCols=[stringIndexer.getOutputCol()], outputCols=[categoricalCol + "classVec"])
# Add stages. These are not run here, but will run all at once later on.
stages += [stringIndexer, encoder]
# COMMAND ----------
# Convert label into label indices using the StringIndexer
label_stringIdx = StringIndexer(inputCol="income", outputCol="label")
stages += [label_stringIdx]
# COMMAND ----------
# Transform all features into a vector using VectorAssembler
numericCols = ["age", "fnlwgt", "education_num", "capital_gain", "capital_loss", "hours_per_week"]
assemblerInputs = [c + "classVec" for c in categoricalColumns] + numericCols
assembler = VectorAssembler(inputCols=assemblerInputs, outputCol="features")
stages += [assembler]
# COMMAND ----------
# Create a Pipeline.
pipeline = Pipeline(stages=stages)
# Run the feature transformations.
# - fit() computes feature statistics as needed.
# - transform() actually transforms the features.
pipelineModel = pipeline.fit(df_adult)
dataset = pipelineModel.transform(df_adult)
# Keep relevant columns
selectedcols = ["label", "features"] + cols
dataset = dataset.select(selectedcols)
display(dataset)
# COMMAND ----------
### Randomly split data into training and test sets. set seed for reproducibility
(trainingData, testData) = dataset.randomSplit([0.7, 0.3], seed=100)
print(trainingData.count())
print(testData.count())
# COMMAND ----------
from pyspark.sql.functions import avg
# get the rate of the positive outcome from the training data to use as a threshold in the model
training_data_positive_rate = trainingData.select(avg(trainingData['label'])).collect()[0][0]
print("Positive rate in the training data is {}".format(training_data_positive_rate))
# COMMAND ----------
# MAGIC %md
# MAGIC #### Logistic Regression - Train
# COMMAND ----------
from pyspark.ml.classification import LogisticRegression
# Create initial LogisticRegression model
lr = LogisticRegression(labelCol="label", featuresCol="features", maxIter=10)
# set threshold for the probability above which to predict a 1
lr.setThreshold(training_data_positive_rate)
# lr.setThreshold(0.5) # could use this if knew you had balanced data
# Train model with Training Data
lrModel = lr.fit(trainingData)
# get training summary used for eval metrics and other params
lrTrainingSummary = lrModel.summary
# Find the best model threshold if you would like to use that instead of the empirical positve rate
fMeasure = lrTrainingSummary.fMeasureByThreshold
maxFMeasure = fMeasure.groupBy().max('F-Measure').select('max(F-Measure)').head()
lrBestThreshold = fMeasure.where(fMeasure['F-Measure'] == maxFMeasure['max(F-Measure)']) \
.select('threshold').head()['threshold']
print("Best threshold based on model performance on training data is {}".format(lrBestThreshold))
# COMMAND ----------
# MAGIC %md
# MAGIC #### GBM - Train
# COMMAND ----------
# MAGIC %md
# MAGIC #### <span style="color:darkblue">Question 3</span>
# MAGIC 1. Train a GBTClassifier on the training data, call the trained model 'gbModel'
# COMMAND ----------
### Question 3.1 Answer ###
from pyspark.ml.classification import GBTClassifier
# Create initial GBTClassifier model
gb = GBTClassifier(labelCol="label", featuresCol="features", maxIter=10)
# Train model with Training Data
gbModel = gb.fit(trainingData)
# COMMAND ----------
# MAGIC %md
# MAGIC #### Logistic Regression - Predict
# COMMAND ----------
# make predictions on test data
lrPredictions = lrModel.transform(testData)
# display predictions
display(lrPredictions.select("label", "prediction", "probability"))
#display(lrPredictions)
# COMMAND ----------
# MAGIC %md
# MAGIC ### GBM - Predict
# COMMAND ----------
# MAGIC %md
# MAGIC #### <span style="color:darkblue">Question 4</span>
# MAGIC 1. Get predictions on the test data for your GBTClassifier. Call the predictions df 'gbPredictions'.
# COMMAND ----------
### Question 4.1 Answer ###
# make predictions on test data
gbPredictions = gbModel.transform(testData)
display(gbPredictions)
# COMMAND ----------
# MAGIC %md
# MAGIC #### Logistic Regression - Evaluate
# COMMAND ----------
# MAGIC %md
# MAGIC #### <span style="color:darkblue">Question 5</span>
# MAGIC
# MAGIC 1. Complete the print_performance_metrics() function below to also include measures of F1, Precision, Recall, False Positive Rate and True Positive Rate.
# COMMAND ----------
from pyspark.ml.evaluation import BinaryClassificationEvaluator
from pyspark.mllib.evaluation import BinaryClassificationMetrics, MulticlassMetrics
def print_performance_metrics(predictions):
# Evaluate model
evaluator = BinaryClassificationEvaluator(rawPredictionCol="rawPrediction")
auc = evaluator.evaluate(predictions, {evaluator.metricName: "areaUnderROC"})
aupr = evaluator.evaluate(predictions, {evaluator.metricName: "areaUnderPR"})
print("auc = {}".format(auc))
print("aupr = {}".format(aupr))
# get rdd of predictions and labels for mllib eval metrics
predictionAndLabels = predictions.select("prediction","label").rdd
# Instantiate metrics objects
binary_metrics = BinaryClassificationMetrics(predictionAndLabels)
multi_metrics = MulticlassMetrics(predictionAndLabels)
# Area under precision-recall curve
print("Area under PR = {}".format(binary_metrics.areaUnderPR))
# Area under ROC curve
print("Area under ROC = {}".format(binary_metrics.areaUnderROC))
# Accuracy
print("Accuracy = {}".format(multi_metrics.accuracy))
# Confusion Matrix
print(multi_metrics.confusionMatrix())
### Question 5.1 Answer ###
# F1
print("F1 = {}".format(multi_metrics.weightedFMeasure(1.0)))
# Precision
print("Precision = {}".format(multi_metrics.weightedPrecision))
# Recall
print("Recall = {}".format(multi_metrics.weightedRecall))
# FPR
print("FPR = {}".format(multi_metrics.weightedFalsePositiveRate))
# TPR
print("TPR = {}".format(multi_metrics.weightedTruePositiveRate))
print_performance_metrics(lrPredictions)
# COMMAND ----------
# MAGIC %md
# MAGIC #### GBM - Evaluate
# COMMAND ----------
print_performance_metrics(gbPredictions)
# COMMAND ----------
# MAGIC %md
# MAGIC ## Cross Validation
# COMMAND ----------
# MAGIC %md
# MAGIC For each model you can run the below comand to see its params and a brief explanation of each.
# COMMAND ----------
print(lr.explainParams())
# COMMAND ----------
print(gb.explainParams())
# COMMAND ----------
# MAGIC %md
# MAGIC #### Logisitic Regression - Param Grid
# COMMAND ----------
from pyspark.ml.tuning import ParamGridBuilder, CrossValidator
# Create ParamGrid for Cross Validation
lrParamGrid = (ParamGridBuilder()
.addGrid(lr.regParam, [0.01, 0.5, 2.0])
.addGrid(lr.elasticNetParam, [0.0, 0.5, 1.0])
.addGrid(lr.maxIter, [2, 5])
.build())
# COMMAND ----------
# MAGIC %md
# MAGIC #### GBM - Param Grid
# COMMAND ----------
# MAGIC %md
# MAGIC #### <span style="color:darkblue">Question 6</span>
# MAGIC
# MAGIC 1. Build out a param grid for the gb model, call it 'gbParamGrid'.
# COMMAND ----------
### Question 6.1 Answer ###
# Create ParamGrid for Cross Validation
gbParamGrid = (ParamGridBuilder()
.addGrid(gb.maxDepth, [5, 10])
.addGrid(gb.maxIter, [2, 5])
.build())
# COMMAND ----------
# MAGIC %md
# MAGIC #### Logistic Regression - Perform Cross Validation
# COMMAND ----------
# set up an evaluator
evaluator = BinaryClassificationEvaluator(rawPredictionCol="rawPrediction")
# Create CrossValidator
lrCv = CrossValidator(estimator=lr, estimatorParamMaps=lrParamGrid, evaluator=evaluator, numFolds=2)
# Run cross validations
lrCvModel = lrCv.fit(trainingData)
# this will likely take a fair amount of time because of the amount of models that we're creating and testing
# COMMAND ----------
# below approach to getting at the best params from the best cv model taken from:
# https://stackoverflow.com/a/46353730/1919374
# look at best params from the CV
print(lrCvModel.bestModel._java_obj.getRegParam())
print(lrCvModel.bestModel._java_obj.getElasticNetParam())
print(lrCvModel.bestModel._java_obj.getMaxIter())
# COMMAND ----------
# MAGIC %md
# MAGIC #### GBM - Perform Cross Validation
# COMMAND ----------
# MAGIC %md
# MAGIC #### <span style="color:darkblue">Question 7</span>
# MAGIC 1. Perform cross validation of params on your 'gb' model.
# MAGIC 1. Print out the best params you found.
# COMMAND ----------
### Question 7.1 Answer ###
# Create CrossValidator
gbCv = CrossValidator(estimator=gb, estimatorParamMaps=gbParamGrid, evaluator=evaluator, numFolds=2)
# Run cross validations
gbCvModel = gbCv.fit(trainingData)
# COMMAND ----------
### Question 7.2 Answer ###
# look at best params from the CV
print(gbCvModel.bestModel._java_obj.getMaxDepth())
print(gbCvModel.bestModel._java_obj.getMaxIter())
# COMMAND ----------
# MAGIC %md
# MAGIC #### Logistic Regression - CV Model Predict
# COMMAND ----------
# Use test set to measure the accuracy of our model on new data
lrCvPredictions = lrCvModel.transform(testData)
display(lrCvPredictions)
# COMMAND ----------
# MAGIC %md
# MAGIC #### GBM - CV Model Predict
# COMMAND ----------
gbCvPredictions = gbCvModel.transform(testData)
display(gbCvPredictions)
# COMMAND ----------
# MAGIC %md
# MAGIC #### Logistic Regression - CV Model Evaluate
# COMMAND ----------
print_performance_metrics(lrCvPredictions)
# COMMAND ----------
# MAGIC %md
# MAGIC #### GBM - CV Model Evaluate
# COMMAND ----------
print_performance_metrics(gbCvPredictions)
# COMMAND ----------
# MAGIC %md
# MAGIC #### Logistic Regression - Model Explore
# COMMAND ----------
print('Model Intercept: ', lrCvModel.bestModel.intercept)
# COMMAND ----------
lrWeights = lrCvModel.bestModel.coefficients
lrWeights = [(float(w),) for w in lrWeights] # convert numpy type to float, and to tuple
lrWeightsDF = sqlContext.createDataFrame(lrWeights, ["Feature Weight"])
display(lrWeightsDF)
# COMMAND ----------
# MAGIC %md
# MAGIC ### Feature Importance
# COMMAND ----------
# MAGIC %md
# MAGIC #### <span style="color:darkblue">Question 8</span>
# MAGIC 1. Print out a table of feature_name and feature_coefficient from the Logistic Regression model.
# MAGIC <br><br>
# MAGIC Hint: Adapt the code from here: https://stackoverflow.com/questions/42935914/how-to-map-features-from-the-output-of-a-vectorassembler-back-to-the-column-name
# COMMAND ----------
### Question 8.1 Answer ###
# from: https://stackoverflow.com/questions/42935914/how-to-map-features-from-the-output-of-a-vectorassembler-back-to-the-column-name
from itertools import chain
attrs = sorted(
(attr["idx"], attr["name"]) for attr in (chain(*lrCvPredictions
.schema[lrCvModel.bestModel.summary.featuresCol]
.metadata["ml_attr"]["attrs"].values())))
lrCvFeatureCoeff = pd.DataFrame([(name, lrCvModel.bestModel.coefficients[idx]) for idx, name in attrs],columns=['feature_name','feature_coefficient'])
print(lrCvFeatureCoeff)
# COMMAND ----------
gbCvFeatureImportance = pd.DataFrame([(name, gbCvModel.bestModel.featureImportances[idx]) for idx, name in attrs],columns=['feature_name','feature_importance'])
print(gbCvFeatureImportance.sort_values(by=['feature_importance'],ascending =False))
# COMMAND ----------
# MAGIC %md
# MAGIC #### <span style="color:darkblue">Question 9</span>
# MAGIC 1. Build and train a RandomForestClassifier and print out a table of feature importances from it.
# COMMAND ----------
### Question 9.1 Answer ###
from pyspark.ml.classification import RandomForestClassifier
rf = RandomForestClassifier(labelCol="label", featuresCol="features")
rfModel = rf.fit(trainingData)
rfFeatureImportance = pd.DataFrame([(name, rfModel.featureImportances[idx]) for idx, name in attrs],columns=['feature_name','feature_importance'])
print(rfFeatureImportance.sort_values(by=['feature_importance'],ascending=False))