So far in this book, when we’ve wanted the Wolfram Language to do something, we’ve written code to tell it exactly what to do. But the Wolfram Language is also set up to be able to learn what to do just by looking at examples, using the idea of machine learning.

We’ll talk about how to train the language yourself. But first let’s look at some built-in functions that have already been trained on huge numbers of examples.

LanguageIdentify takes pieces of text, and identifies what human language they’re in.

LanguageIdentify[{ "thank you","merci","dar las gracias","感謝","благодарить"}]

The Wolfram Language can also do the considerably more difficult “artificial intelligence” task of identifying what an image is of.

ImageIdentify[]

There’s a general function Classify, which has been taught various kinds of classification. One example is classifying the “sentiment” of text.

Classify["Sentiment","I'm so excited to be programming"]

Classify["Sentiment","math can be really hard"]

You can also train Classify yourself. Here’s a simple example of classifying handwritten digits as 0 or 1. You give Classify a collection of training examples, followed by a particular handwritten digit. Then it’ll tell you whether the digit you give is a 0 or 1.

Classify[{->0,->1,->0,->1,->1,->0,->0,->1,->1,->0,->0,->0,->1,->0,->1,->0,->1,->1,->1},
]

To get some sense of how this works—and because it’s useful in its own right—let’s talk about the function Nearest, that finds what element in a list is nearest to what you supply.

Nearest[{10,20,30,40,50,60,70,80},22]

Nearest[{10,20,30,40,50,60,70,80},22,3]

Nearest can find nearest colors as well.

Nearest[{,,,,,,,,,,,,,,,,},,3]

It also works on words.

Nearest[WordList[ ],"good",10]

There’s a notion of nearness for images too. And though it’s far from the whole story, this is effectively part of what ImageIdentify is using.

Something that’s again related is recognizing text (optical character recognition or OCR). Let’s make a piece of text that’s blurred.

Blur[Rasterize[Style["hello",30]],3]

TextRecognize can still recognize the original text string in this.

TextRecognize[]

If the text gets too blurred TextRecognize can’t tell what it says—and you probably can’t either.

Table[Blur[Rasterize[Style["hello",15]],r],{r,0,4}]

Table[TextRecognize[Blur[Rasterize[Style["hello",15]],r]],{r,0,4}]

Something similar happens if we progressively blur the picture of a cheetah. When the picture is still fairly sharp, ImageIdentify will correctly identify it as a cheetah. But when it gets too blurred ImageIdentify starts thinking it’s more likely to be a lion, and eventually the best guess is that it’s a picture of a person.

Table[Blur[,r],{r,0,22,2}]

Table[ImageIdentify[Blur[,r]],{r,0,22,2}]

ImageIdentify normally just gives what it thinks is the most likely identification. You can tell it, though, to give a list of possible identifications, starting from the most likely. Here are the top 10 possible identifications, in all categories.

ImageIdentify[,All,10]

ImageIdentify[,All,10]

In machine learning, one often gives training that explicitly says, for example, “this is a cheetah”, “this is a lion”. But one also often just wants to automatically pick out categories of things without any specific training.

One way to start doing this is to take a collection of things—say colors—and then to find clusters of similar ones. This can be achieved using FindClusters.

FindClusters[{,,,,,,,,,,,,,,,,,,,,,}]

You can get a different view by connecting each color to the three most similar colors in the list, then making a graph out of the connections. In the particular example here, there end up being three disconnected subgraphs.

NearestNeighborGraph[{,,,,,,,,,,,,,,,,,,,,,},3,VertexLabels->All]

A dendrogram is a tree-like plot that lets you see a whole hierarchy of what’s near what.

Dendrogram[{,,,,,,,,,,,,,,,,,,,,,}]

When we compare things—whether they’re colors or pictures of animals—we can think of identifying certain features that allow us to distinguish them. For colors, a feature might be how light the color is, or how much red it contains. For pictures of animals, a feature might be how furry the animal looks, or how pointy its ears are.

In the Wolfram Language, FeatureSpacePlot takes collections of objects and tries to find what it considers the “best” distinguishing features of them, then uses the values of these to position objects in a plot.

FeatureSpacePlot doesn’t explicitly say what features it’s using—and actually they’re usually quite hard to describe. But what happens in the end is that FeatureSpacePlot arranges things so that objects that have similar features are drawn nearby.

FeatureSpacePlot[{,,,,,,,,,,,,,,,,,,,,,}]

If one uses, say, 100 colors picked completely at random, then FeatureSpacePlot will again place colors it considers similar nearby.

FeatureSpacePlot[RandomColor[100]]

Let’s try the same kind of thing with images of letters.

Table[Rasterize[FromLetterNumber[n]],{n,26}]

FeatureSpacePlot will use visual features of these images to lay them out. The result is that letters that look similar—like y and v or e and c—will wind up nearby.

FeatureSpacePlot[Table[Rasterize[FromLetterNumber[n]],{n,26}]]

Here’s the same thing, but now with pictures of cats, cars and chairs. FeatureSpacePlot immediately separates the different kinds of things.

FeatureSpacePlot[{,,,,,,,,,,,,,,}]

Identify what language the word “ajatella” comes from. »

Apply ImageIdentify to an image of a tiger, getting the image using (Key1[ctrl, =]). »

Make a table of image identifications for an image of a tiger, blurred by an amount from 1 to 5. »

Classify the sentiment of “I’m so happy to be here”. »

Find the 10 words in WordList[ ] that are nearest to “happy”. »

Generate 20 random numbers up to 1000 and find which 3 are nearest to 100. »

Generate a list of 10 random colors, and find which 5 are closest to Red. »

Of the first 100 squares, find the one nearest to 2000. »

Find the 3 European flags nearest to the flag of Brazil. »

Make a graph of the 2 nearest neighbors of each color in Table[Hue[h],{h,0,1,.05}]. »

Generate a list of 100 random numbers from 0 to 100, and make a graph of the 2 nearest neighbors of each one. »

Collect the flags of Asia into clusters of similar flags. »

Make raster images of the letters of the alphabet at size 20, then make a graph of the 2 nearest neighbors of each one. »

Generate a table of the results of using TextRecognize on “hello” rasterized at size 50 and then blurred by between 1 and 10. »

Make a dendrogram for images of the first 10 letters of the alphabet. »

Make a feature space plot for the uppercase letters of the alphabet. »

Make a table of image identifications for a picture of the Eiffel Tower, blurred by an amount from 1 to 5. »

Classify the sentiment of the Wikipedia article on “happiness”. »

Of colors in the list Table[Hue[h],{h,0,1,.05}], find which are the 3 nearest to Pink. »

Compute all fractions i/j for i and j up to 500, and find the 10 fractions closest to pi. »

Generate a list of 10 random numbers from 0 to 10, and make a graph of the 3 nearest neighbors of each one. »

Find clusters of similar colors in a list of 100 random colors. »

Make a feature space plot for both upper and lowercase letters of the alphabet. »

How come I’m getting different results from the ones shown here?

Probably because Wolfram Language machine learning functions are continually getting more training—and so their results may change, hopefully always getting better. For TextRecognize, the results can depend in detail on the fonts used, and exactly how they’re rendered and rasterized on your computer.

How does ImageIdentify work inside?

It’s based on artificial neural networks inspired by the way brains seem to work. It’s been trained with millions of example images, from which it’s progressively learned to make distinctions. And a bit like in the game of “twenty questions”, by using enough of these distinctions it can eventually determine what an image is of.

How many kinds of things can ImageIdentify recognize?

At least 10,000—which is more than a typical human. (There are about 5000 “picturable nouns” in English.)

What makes ImageIdentify give a wrong answer?

A common cause is that what it’s asked about isn’t close enough to anything it’s been trained on. This can happen if something is in an unusual configuration or environment (for example, if a boat is not on a bluish background). ImageIdentify usually tries to find some kind of match, and the mistakes it makes often seem very “humanlike”.

Can I ask ImageIdentify the probabilities it assigns to different identifications?

Yes. For example, to find the probabilities for the top 10 identifications in all categories use ImageIdentify[image,All,10,"Probability"].

How many examples does Classify typically need to work well?

If the general area (like everyday images) is one it already knows well, then as few as a hundred. But in areas that are new, it can take many millions of examples to achieve good results.

How does Nearest figure out a distance between colors?

It uses the function ColorDistance, which is based on a model of human color vision.

How does Nearest determine nearby words?

By looking at those at the smallest EditDistance, that is, reached by the smallest number of single-letter insertions, deletions and substitutions.

Can a single graph have several disconnected parts?

Absolutely. An example is the last graph in this section.

What features does FeatureSpacePlot use?

There’s no easy answer. When it’s given a collection of things, it’ll learn features that distinguish them—though it’s typically primed by having seen many other things of the same general type (like images).

Tech Notes