Now, the first step is head over to the course resources list and head over to the skeleton project on 1
GitHub. 2
Now, this skeleton project doesn't have any code in it and it only has the bare-bones storyboard design. 3
So go ahead and git clone or download and unzip this project and open it inside Xcode 10. 4
Now, you can see that all we have onscreen is just a simple app with a single label 5
that is the sentiment label, as well as a text field and a prediction button. 6
And those are linked up as two IBOutlets for the textField and the sentimentLabel, and one IBAction 7
for when the user presses the predict button. 8
That's all I placed inside the skeleton project. 9
So once we're ready with that, we can actually go ahead and close it down because we need to, first, build 10
our machine learning model that is going to do this the sentiment analysis. 11
Now, in order to build any machine learning model, we're gonna need some data. So you can download the 12
Twitter Sentiment Data inside the Course Resources list as well. 13
There is the download link here. 14
And if you click on it, download should start automatically, and make sure that you unzip that file so 15
that you end up with a CSV. 16
Now, this public data set comes from a company called Sanders Analysis. And at one point, they collected 17
1,000 tweets that mentioned the AAPL, 18
so the Apple Stock or the @Apple handle. 19
And then they employed a bunch of people, humans, to read through each of the tweets and give it a rating 20
of positive, negative, or neutral. 21
So let's take a look at this data and see what it looks like. 22
So I want you to head over to Google Sheets and create a brand new spreadsheet. 23
Now, in this spreadsheet, you're going to open up that CSV file by uploading that CSV file that you just 24
downloaded and extracted. 25
So this is what our data looks like. 26
If you scroll down, you can see that there are around thousand tweets and they were found by searching for 27
the handle @Apple, 28
so when somebody is mentioning the Apple brand, basically, and then humans have read through all of these 29
some sensical, some nonsensical tweets to rate it whether as positive, negative, or neutral. 30
And this is the point where we can be grateful that we didn't have to do what we did in previous modules 31
to use Create ML which is generating our own data. 32
I'm pretty sure this would take us days to trawl through Twitter, 33
download the tweets relevant to Apple, and then read through them individually, and specify whether if 34
it's neutral, positive, or negative. 35
So here we have our data. 36
So the next thing we need to do is to use it to create our machine learning model, so that our model 37
learns what positive tweets look like, what neutral tweets look like, and what negative tweets look like. 38
So we can use it to analyze future tweets that it's never seen, that don't exist over here. 39
So as we did previously, in order to use Create ML to generate our machine learning model, we're going 40
to use a macOS blank playground. 41
And I'm just going to save it on my desktop and I'm going to call it TweetSentimentModelMaker. 42
Now, unfortunately, unlike the image recognition builder, there is no live view for creating Natural Language 43
processing models yet. 44
But that's not a problem. 45
All we have to do is just write a few lines of code in order to load up our data and get Create ML 46
to start working on it. 47
Now, the first thing that we're going to do is we're going to delete this variable string, but we're going 48
to keep our Cocoa framework imported. 49
And in addition, we're going to import, of course, CreateML in order to use it to create our machine 50
learning model. 51
Now, the first thing that we need to do is to load up our CSV file and specify the data that we're going 52
to use to train our machine learning model. 53
So to do that, we're going to create a new constant code data and it's going to be initialized as a new 54
instance of a MLDataTable. 55
So this MLDataTable can be initialized from either a JSON data format or a CSV data format. 56
And in our case, our data table is a CSV. 57
So we're going to convert the CSV into a MLDataTable and we'll do that by using the method contentsOf 58
from a URL. 59
And that URL is basically the location where our data exists. 60
So let's go ahead and create it. 61
So we'll just write URL and we'll initialize it using file URL with path and that path has 62
to be a String that represents the entire file path in order to get to this file that we want it to 63
read. 64
Now, that is basically this down here. 65
It's going to be at forward slash, Users/angelayu/Downloads/twitter-sanders- 66
apple3.csv. 67
Now, if you don't want to type all of that out and make sure that you don't make any mistakes, 68
a neat trick is just to head over to Terminal, type cd space, and then we can simply click and drag 69
this file into here, and it'll automatically load up the entire file path that's required to reach that 70
file from the root of our computer. 71
So now I can highlight all of this and copy and paste it in here as the string that I'm going to use 72
to create a file URL to load up and convert into a MLDataTable. 73
Now, we get a error that tells us that the initialization of the MLDataTable can throw an error, 74
but is not marked with "try." 75
So that means that if when we try to convert a file to an MLDataTable and, actually, this file isn't 76
a CSV or a JSON or even if this file plainly just doesn't exist, then we'll get an error. 77
So, and so we need to mark our initialization with a "try." And if we don't have a file here and this fails, 78
then we just don't want our code to continue. 79
Now, the next thing we need to do is, remember, when we created our image recognition model, we had some 80
training data and we had some testing data, so that we could train our model app, and then evaluate how 81
good it is based on images that it had never seen before. 82
Now, we have to do the same thing here with our Twitter Sentiment Analysis Data. 83
Now, you can either randomly go in here and pick out 20 percent of tweets including their sentiment and 84
create a new table, and load that in, and save it, and loaded in, and add it to our script, or we 85
can use a very simple method that just splits up our data randomly into 80 percent for training and 86
20 percent for testing. 87
So the way to do that is to write "let" and we're going to create two constants: one called trainingData 88
and the other is called testingData, 89
and this is going to be equal to the data object that we already have, 90
but split using a method called randomSplit. And this is only available for CreateML, so we can't 91
use this for just, you know, anything that we want to do. 92
It's specifically made so that it's easy to split up a data set to a 80 percent trainingData and 20 93
percent testingData set. 94
Now, this method takes two parameters or two inputs. 95
One is how do you want the random split to be carried out. 96
Do you want it 80-20? 97
Do you want it 70-20? 98
Then you just use a Double. 99
So, for example, 0.8 to represent 80 percent trainingData, 20 percent testingData. 100
If you wanted, say, 20 percent trainingData and 80 percent testingData, then you would just put 101
0.2. 102
Now, the second parameter is a little bit more complex. 103
It's something called a seed and the seed relates to how random number generators work. 104
Now, as we learned previously, random number generation using a computer is never truly random in the same 105
way that it's random in nature. 106
Now, in certain instances, if you want to be able to reproduce the randomness from a random number generator, 107
you can use something called a seed. 108
So, for example, we're currently splitting our data randomly to 80 percent and 20 percent. 109
But if we need to reproduce this in the future to, say, debug our code or try to figure out why is it 110
that our model isn't working, then we can use the same seed to generate the same randomSplit. 111
So in here, I'm just going to put 5, and in the future if I need my data to be split randomly in exactly 112
the same way, then I can simply use that 5 as the seed to achieve that. 113
So, now that we've split our data randomly, then we can go ahead and create a new instance of the ML 114
text classifier. 115
And this is going to create our machine learning model by passing in our training data. 116
So let's call it the sentimentClassifier 117
and it's going to be equal to a MLTextClassifier. 118
Now, we're going to initialize this brand-new text classifier by using the method down here which takes 119
three parameters. 120
One is a MLDataTable of trainingData which we created here by doing our 80-20 split. 121
Next is the name of the textColumn, so the column inside that table that contains the text. And another 122
one which is the name of the column that contains the label, 123
so whether if it was positive, neutral, or negative. And you notice that this method also throws. 124
So in addition to writing the method, we also have to mark it with a "try." 125
Now, the part that we're going to use as the trainingData is going to be this constant that we created 126
here. And this is a random 80 percent of our data that is loaded up from this location. 127
So whatever it is that you called it over here, it has to match over here. 128
Now, how do we figure out what is the name of the text column? 129
Well, let's head back to our data set, CSV, and you can see that right at the top, we've got a name for 130
each column. 131
The column for the text is just called text with a lower case "t." 132
And the column for the labels is called class. 133
So, now we just have to put that in here as the String. 134
So the textColumn is called "text" and the labelColumn is called "class." 135
So this line of code will train up our text classifier by using our trainingData. 136
Now, once that's done, then this object, the sentimentClassifier, is now all machine learning model. 137
But before we're done, we should test it against our testingData to evaluate how accurate it is, 138
so we can generate some evaluationMetrics by using our sentimentClassifier and calling the method 139
evaluation on, and we're going to call it on a object of type MLDataTable which is, of course, going 140
to be our testingData over here. 141
So here, we're going to pass in our testingData set. 142
And at this stage, we will have evaluation metrics that we can then use to figure out the accuracy. 143
And the accuracy-- 144
So let's call it evaluation accuracy. 145
This can be derived by taking the evaluation metrics that we got and finding one of its properties called 146
the classificationError which is the fraction of examples that were incorrectly labeled by this model. 147
So this is going to be a Double. 148
So, for example, if you had 90 percent that were incorrectly labeled, then this number will be 149
0.9. 150
If you only had 10 percent that were incorrectly labeled, then there'll be 0.1. 151
So we can then take 1.0 and subtract the error to get the accuracy, 152
so the percentage that it got right, essentially. 153
And then we can multiply this by 100 in order to get a percentage. 154
Now at this stage, we can already go ahead and click on the play button to see what we get as a result. 155
So you can see that it did all of this in a very, very short period of time and we end up with a evaluation 156
accuracy of close to 70 percent. 157
So this means that in an ideal world, we should really feed our model more data so we can get it close 158
to 75 or 80 percent. 159
But for our purposes, 70 percent is good enough and we're going to go ahead and save our model so that 160
we can use it inside our app. 161
So, first, I'm going to generate some metadata and I'm going to use the MLModelMetadata and I'm going 162
to initialize it with all of these things. 163
So it'll have an author, will have a short description. 164
It doesn't really need a license, but it'll have a version which needs to be a String, so inside quotes, 165
"1.0" 166
And there's also nothing additional I want to add, so I'm also going to delete that. 167
So now that I've created my metadata, I'm going to go ahead and take my sentimentClassifier and I'm 168
going to write to a URL, so that I can save it. 169
And, again, we're going to generate our URL by saying URL(fileURLWithPath: String) 170
and the path I'm going to use is everything up to here. 171
So I'm going to place it inside the same folder that I had my initial data in and I can locate it very 172
easily. 173
Now, after the last forward slash, I'm going to put in the name that I want to give my model, and it's 174
going to be a TweetSentimentClassifier. 175
And, remember, you have to end this with .mlodel as the file extension. 176
And, finally, again, this write method an throw, 177
so we must mark it with a "try." 178
All right. 179
So now that we've written the two lines of code that is going to give our sentimentClassifier some 180
metadata, as well as tell it where it should save it to, then we can go ahead and click the play button 181
to run those two lines of code. And you can see that because this part is blue, when we press this play 182
button, 183
it's only going to start running the code from line 13 onwards. 184
So we don't repeat the part that we've already run before. 185
So let's go ahead and do that. 186
And now if we go into our Downloads folder, you can see there is our brand-new 187
TweetSentimentClassifier.mlmodel. 188
So we've now saved our model. 189
But while we're still inside playgrounds, let's test our sentimentClassifier, and we can do that by using 190
a method called prediction from a String. 191
So the String is any sort of tweet that we might come across in the wild. 192
"@Apple is a terrible company!" And, remember that, again, we have to mark it with a "try" and we're going to 193
go ahead and click play to see what we get. 194
So the output of this method was the label "Negative." 195
So that's working pretty well. 196
So, now let's try it with a positive-sounding tweet, say, something like, "I just found the best restaurant ever, 197
and it's @DuckandWaffle." 198
So now let's try this. 199
Most humans will realize is a very positive sounding tweet. 200
And, again, our classifier is working pretty well, classified that as "Positive" as well. 201
Now, the last one that I'm going to try is something that sounds a bit more neutral. 202
So I'm going to type a tweet that I think sounds kind of neutral. 203
Let's try. 204
"I think @CocaCola ads are just ok." 205
Okay. 206
So let's run this and see what we get. 207
And, again, "Neutral." 208
So our sentimentClassifier is actually predicting pretty accurately based off these three lines of tweets 209
that I've just come up with out of the blue. 210
Now, one of the reasons why we have a much lower accuracy for our Natural Language processing model compared 211
to our Image Classification model is that Image Classification has less ambiguity. 212
If I see a picture of a giraffe, then I will label it as a picture of a giraffe, 213
and I would expect my image recognition model to pick up that it's a picture of a giraffe. 214
But with something like sentiment, whether if something is negative, positive, or neutral, then even if 215
you show two people the same tweet, often they might come to different conclusion. 216
Some people might read sarcasm, or some people might be more sensitive, or interpret the tweet in a different 217
way. 218
So don't be too disheartened if your Natural Language models are a little bit less accurate when you 219
run the training data on it compared to the image recognition. 220
It's a very, very different task and it's actually a much harder task for a machine to perform. 221
And looking at our sample tweets, it's actually not doing such a bad job at all. 222
So in the next lesson, we're going to be creating our Twittermenti project. 223
We're going to be adding a brand-new sentiment classifying model to our Twittermenti project and we're 224
going to continue building our app.