Preparing your string input for machine learning processing was actually the hardest part. Getting a prediction from a machine learning model is very simple. You only need to use one simple function to get predictions from your machine learning model. With the results, populate the user interface and tell the user which gender a provided name most likely has.
- [Instructor] So far we have done everything so that we can input our names in a way that our model understands. Now, we can deal with the first predictions and get a prediction for a name. We're going to add that in our ViewController.swift file. I'm going to add a new function right below the features function that we added in the last video. I'm going to call that predictGenderFromName. This is going to receive one perimeter, which is a name string, and it's going to return an optional string, so that we could also return nil if we had to.
Going to make some space for us here. The first thing that we can do is get our name features for our model. I'm simply calling our features function and provide it with the string that we want to get features from, which is our name that we have as a parameter for this function. That's all there is to it. Now, the very interesting part follows, which is the initialization of our machine learning model. I'm defining an object for that and constant.
All I need to do here now is to call GenderByName, or use the class GenderByName and initialize a model object from that. Now I can create an if let statement for the prediction. If we get a prediction, or this works, then we can continue. I'm using a try statement, using my model, and call the prediction function, which throws an error. This is why we need to use a try statement here. Our input is a dictionary with string keys and double values, and this is what we have in our name features object.
If this works, we do have a prediction. What we can do is check if the prediction and its class label is, for example, equal to F, so female. Then what we'd like to do is return female as a string. So, female. If this is not going to be an F, this means it should be an M, so we can return male. If nothing of that works, and we did not return anything, then we can return nil at the end of our function.
That is all there is to it to get a prediction from our machine learning model. We still need to populate our user interface. We're going to do that in the text field, should return function in line 70, which is a delegate function of the text field that we have created as an outlet at the top of our class which is the nameTextField. If the user hits return, then we want to analyze our name. All we need to do here is using our gender label that we have, acts as its text property, and then what we can do is use the predict gender from name function, using our text field, and its text and unwrap this text property, because we can assume now that this works.
In a productive environment you should do a check here maybe first. Now I'd like to say, let's just try this in the simulator, running this on the iPhone 8 simulator. Here we have our application. I can enter a name, like Peter, for example. We get the gender, male, and we try it with let's say, Elizabeth. And this is a female name. This is really cool and as you can see here, the code that we need for the prediction are really just three lines, or maybe four.
This is very simple and very cool.
- What are machine learning, Core ML, Vision, and NLP?
- Adding a machine learning model to a project
- Getting predictions from machine learning models
- Converting existing machine learning models for Core ML
- Classifying images and detecting objects with Vision and Core ML
- Analyzing natural language text with NSLinguisticTagger