Machine Learning
What Are We Going to Learn?
What Are We Going to Learn?
What is machine learning?
Creating a facial recognition classifier
Getting data from the internet
Making a function
Creating a sentiment analysis Magic 8 Ball
Getting input from the user
Using Classify for sentiment analysis
Using randomness
Set Up Your Notebook
Set Up Your Notebook
Before we get started, let’s set up our notebook. You should have already made a Wolfram Cloud account.
Sign in with your Wolfram ID.
Click New Notebook.
If you click any white space in the notebook and then type, the notebook will think you’re writing code.
Type 2 + 2.
Then press Shift and hold down Shift while you press Return/Enter.
Your code should run and give you the number 4 in the next cell.
To write text in your notebook, click a white space and see the horizontal line appear. You can then either click the plus button and select Plain Text, or you can press Command+7.
What Is Machine Learning?
What Is Machine Learning?
Machine learning gives a computer the power to learn about data without a human telling it what to do.
There are two types of machine learning: supervised and unsupervised.
Supervised machine learning is when we give the computer lots of data, and we give it information about the data. For example, we might give the computer lots of words in Spanish and lots of words in German and tell the computer which words come from which language. Then we could give the computer a new word in German that it hasn’t seen before and ask it if the word is German or Spanish. The computer will then be able to tell us!
Let’s look at some examples of pre-trained machine learning in Wolfram!
LanguageIdentify["thank you","merci","gracias","感謝","благодарить"]
ImageIdentify
FindTextualAnswer["The dodo (Raphus cucullatus) is an extinct flightless bird that was endemic to the island of Mauritius, east of Madagascar in the Indian Ocean. The dodo's closest genetic relative was the also extinct Rodrigues solitaire, the two forming the subfamily Raphinae of the family of pigeons and doves. The closest living relative of the dodo is the Nicobar pigeon. A white dodo was once thought to have existed on the nearby island of Réunion, but this is now thought to have been confusion based on the Réunion ibis and paintings of white dodos.\nSubfossil remains show the dodo was about 1 metre (3 ft 3 in) tall and may have weighed 10.6–17.5 kg (23–39 lb) in the wild. The dodo's appearance in life is evidenced only by drawings, paintings, and written accounts from the 17th century.","How big was the dodo?"]
Out[]=
1 metre
Unsupervised machine learning is when we give the computer lots of data, but we don’t tell it anything about the data. In this example, we might give the computer lots of words in Spanish and German but not tell it which language each word is in. Then the computer would analyze the words and try to figure out which category each word falls into. This is really useful when we have lots of data but we don’t know much about it.
For example, we could ask the computer to find groups of similar items within a dataset. Let’s give the computer 100 random colors and ask it to organize them into five categories:
randomcolors=Table[RandomColor[],100];FindClusters[randomcolors,5]
We’re going to be focusing on supervised machine learning today. In Wolfram, there are two big functions for supervised machine learning: Classify and Predict. LanguageIdentify and ImageIdentify use Classify.
Classify takes in data and what category that data falls into, and then when you give it a new piece of data, it tries to figure out which category it should be in. For example:
In[]:=
<|"Red"Join[Table[Hue[x],{x,0,0.08,0.001}],Table[Hue[x],{x,0.9,1,0.001}]],"Blue"Table[Hue[x],{x,0.5,0.7,0.001}]|>
In[]:=
colorclassify=Classify[<|"Red"->Join[Table[Hue[x],{x,0,0.08,0.001}],Table[Hue[x],{x,0.9,1,0.001}]],"Blue"->Table[Hue[x],{x,0.5,0.7,0.001}]|>]
In[]:=
colorclassify
The other type of supervised machine learning in Wolfram is Predict. Predict tries to figure out what value a new piece of data might have, based on the values of the data we’ve given it before. For example, we might give the computer a dataset of ages, heights and genders, and then I want to predict the height of someone with a specific age and gender:
In[]:=
data=Dataset[{"age"10,"height"139,"gender""girl","age"10,"height"140,"gender""girl","age"11,"height"137,"gender""girl","age"11,"height"129,"gender""girl","age"12,"height"132,"gender""girl","age"12,"height"143,"gender""girl","age"13,"height"146,"gender""girl","age"13,"height"150,"gender""girl","age"14,"height"141,"gender""girl","age"14,"height"139,"gender""girl","age"15,"height"149,"gender""girl","age"15,"height"159,"gender""girl","age"10,"height"145,"gender""boy","age"10,"height"148,"gender""boy","age"11,"height"152,"gender""boy","age"11,"height"147,"gender""boy","age"12,"height"151,"gender""boy","age"12,"height"149,"gender""boy","age"13,"height"156,"gender""boy","age"13,"height"153,"gender""boy","age"14,"height"159,"gender""boy","age"14,"height"157,"gender""boy","age"15,"height"159,"gender""boy","age"15,"height"161,"gender""boy"}]
In[]:=
predictor=Predict[data"height"]
In[]:=
predictor[<|"age"->14,"gender"->"girl"|>]
We’re going to be using the Classify function to make two mini-projects today! The first project is making a classifier based on characters from the Harry Potter movies and then seeing what character another image looks most like. In the second project, we’ll make a Magic 8 Ball that responds to the user’s question with a positive, neutral or negative response, based on the sentiment of their input.
Make a Facial Recognition Classifier
Make a Facial Recognition Classifier
Out[]=
Get Some Images
Get Some Images
In[]:=
WebImageSearch["Harry Potter Daniel Radcliffe",20,Method"Google"]
We’d do exactly the same for the other two.
Then we need to use Classify. We just want to associate the name of the category with the data:
classifier=Classify[<|"Harry"WebImageSearch["Harry Potter Daniel Radcliffe",20,Method"Google"],"Ron"WebImageSearch["Ron Weasley Rupert Grint",20,Method"Google"],"Hermione"WebImageSearch["Hermione Granger Emma Watson",20,Method"Google"]|>]
Great! Now we can test it with an image:
classifier
There’s more information we can find out about the result. For example, we can find out the probability that the image falls into each of the categories:
classifier
,"Probabilities"
In[]:=
Style["Which Harry Potter Character Are You?","Text",Bold,18]
ColumnStyle["Which Harry Potter Character Are You?","Text",Bold,18],
,Styleclassifier
,"Text",Bold,18,StyleNormalclassifier
,"Probabilities","Text",14
Then let’s center everything and put a frame around our column:
This is awesome! But now if I want to change the image I’m comparing, I have to put it in three separate places. Let’s make a function that let’s us put the image in once.
We can write our own function by giving it a name, a placeholder for the input data and then using these characters : =. This is called SetDelayed, if you want to look it up!
We can use the same strategy to do this project for any other characters!
We can even do something really cool and use our own photo! The function CurrentImage uses your webcam to take a photo.
Amazing! Congratulations, you’ve made a little application that classifies an image.
Magic 8 Ball
Magic 8 Ball
Now we’re going to use a special form of the Classify function to make a Magic 8 Ball that gives us positive answers if the user input is positive, neutral answers if the user input is neutral and negative answers if the user input is negative.
First, let’s use Classify to analyze the sentiment of a phrase:
Classify has been given lots of phrases and told if they are positive, negative or neutral, so we don’t have to train the classifier like we did for the images.
Let’s start out by writing some code to give a different answer if the classifier returns positive, neutral or negative.
We use a Which statement to do a different action based on a test. First, we test if the result is positive, and if it is, then we print “Yes.” If it’s not positive, we’ll test to see if it’s neutral. If it’s neutral, we’ll print “Maybe.” If it’s not neutral, we’ll test if it’s negative, and if it’s negative, we’ll print “No.” Again, we’ll need a variable for the first input, so we can change it:
Great! Now that we have this code, let’s make a button so that whenever we click the button, the code runs:
Fantastic. So now we have a button that takes a question, classifies it based on its sentiment and outputs a phrase. Let’s get the question from the user:
Awesome. Just like for the answer, we want to be able to use the output later in the code, so we’ll need to use our variable with Dynamic:
Great! Now we can add this to our code:
Perfect! We can even make it a little more complicated and randomly choose a response from a list of positive, neutral and negative responses we’ve written:
Well done! You’ve made a Magic 8 Ball that’s responsive to the sentiment of the user input!
What We Did
What We Did
Learned about machine learning
Saw some examples of built-in classifiers in Wolfram Language
Made our own classifier
Built a function
Made an interactive interface
Explored classifiers for images and text
What’s Next?
What’s Next?
Make your own changes and expand the code for these projects!
Come up with your own project with machine learning:
Classifying
images of leaves
images of pets
images of your handwriting
Predicting
the weather tomorrow
what time the Sun will rise tomorrow
Attend one of our programs!
Computational Adventures—a six-month subscription to get a new mini project every two weeks (suitable for kids and teens), and available for teachers to run as an afterschool activity
Wolfram Spring Program UK—a one-week, in-person Easter program in April for secondary school students in the UK
Wolfram Middle School Summer Camp—a one-week virtual summer program in June for girls and gender-nonconforming students aged 10–14
Wolfram High School Summer Research Program—a three-week, in-person summer program in July for high-school students
Wolfram Summer School—a three-week, in-person program in July for adults
Wolfram U—self-paced classes in all areas of applied computation