forked from jlyang1990/Spark_Python_Do_Big_Data_Analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDEMO Spark ML NaiveBayes.py
executable file
·90 lines (71 loc) · 2.91 KB
/
DEMO Spark ML NaiveBayes.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
# -*- coding: utf-8 -*-
"""
-----------------------------------------------------------------------------
Naive Bayes : Spam Filtering
Copyright : V2 Maestros @2016
Problem Statement
*****************
The input data is a set of SMS messages that has been classified
as either "ham" or "spam". The goal of the exercise is to build a
model to identify messages as either ham or spam.
## Techniques Used
1. Naive Bayes Classifier
2. Training and Testing
3. Confusion Matrix
4. Text Pre-Processing
5. Pipelines
-----------------------------------------------------------------------------
"""
#import os
#os.chdir("C:/Personal/V2Maestros/Courses/Big Data Analytics with Spark/Python")
#os.curdir
"""--------------------------------------------------------------------------
Load Data
-------------------------------------------------------------------------"""
#Load the CSV file into a RDD
smsData = SpContext.textFile("SMSSpamCollection.csv",2)
smsData.cache()
smsData.collect()
"""--------------------------------------------------------------------------
Prepare data for ML
-------------------------------------------------------------------------"""
def TransformToVector(inputStr):
attList=inputStr.split(",")
smsType= 0.0 if attList[0] == "ham" else 1.0
return [smsType, attList[1]]
smsXformed=smsData.map(TransformToVector)
smsDf= SpSession.createDataFrame(smsXformed,
["label","message"])
smsDf.cache()
smsDf.select("label","message").show()
"""--------------------------------------------------------------------------
Perform Machine Learning
-------------------------------------------------------------------------"""
#Split training and testing
(trainingData, testData) = smsDf.randomSplit([0.9, 0.1])
trainingData.count()
testData.count()
testData.collect()
#Setup pipeline
from pyspark.ml.classification import NaiveBayes, NaiveBayesModel
from pyspark.ml import Pipeline
from pyspark.ml.feature import HashingTF, Tokenizer
from pyspark.ml.feature import IDF
#Split into words and then build TF-IDF
tokenizer = Tokenizer(inputCol="message", outputCol="words")
hashingTF = HashingTF(inputCol=tokenizer.getOutputCol(), \
outputCol="tempfeatures")
idf=IDF(inputCol=hashingTF.getOutputCol(), outputCol="features")
nbClassifier=NaiveBayes()
pipeline = Pipeline(stages=[tokenizer, hashingTF, \
idf, nbClassifier])
#Build a model with a pipeline
nbModel=pipeline.fit(trainingData)
#Predict on test data (will automatically go through pipeline)
prediction=nbModel.transform(testData)
#Evaluate accuracy
evaluator = MulticlassClassificationEvaluator(predictionCol="prediction", \
labelCol="label",metricName="accuracy")
evaluator.evaluate(prediction)
#Draw confusion matrics
prediction.groupBy("label","prediction").count().show()