Machine Learning and AI
Prerequisites: Importing data and Dataset
Machine learning in the Wolfram Language is highly automated: one function chooses a method, preprocesses the data, and trains a model.
Clustering
FindClusters[{1, 2, 10, 11, 12}]
{{1, 2}, {10, 11, 12}}
FindClusters groups similar values (numbers, vectors, strings, images)
without labels. ClusterClassify returns a reusable classifier.
Classification and prediction
(* illustrative: trained models vary between runs and versions *)
training = {
{1.0, 1.1} -> "small", {1.2, 0.9} -> "small", {0.8, 1.0} -> "small",
{5.0, 5.2} -> "large", {5.3, 4.9} -> "large", {4.8, 5.1} -> "large"};
c = Classify[training];
c[{1.1, 1.0}]
c[{5.1, 5.0}, "Probabilities"]
Classifylearns labels from examples written asinput -> label. Herec[{1.1, 1.0}]returnssmall, and"Probabilities"gives the model's confidence for each label.Predictdoes the same for numeric outputs (regression).Method -> "RandomForest","LogisticRegression","GradientBoostedTrees", and others choose the algorithm explicitly.ClassifierMeasurements[c, testSet]reports accuracy, a confusion matrix, and more — always evaluate on data the model didn't see.
Inputs can be numbers, text, images, or associations of mixed features.
Neural networks
(* illustrative: downloads a pretrained model *)
net = NetModel["LeNet Trained on MNIST Data"];
net[image]
- The Wolfram Neural Net Repository has pretrained networks for images,
text, and audio, loaded with
NetModel. NetChainandNetGraphbuild networks from layers;NetTraintrains them on CPU or GPU.ImageIdentify,TextSentences,SpeechRecognize, and similar functions use built-in models directly.
Large language models
(* illustrative: needs an LLM service configured *)
summarize = LLMFunction["Summarize in one sentence: ``"];
summarize["The Wolfram Language is a symbolic language ..."]
LLMFunction, LLMSynthesize, and chat notebooks connect to large
language models (through a Wolfram subscription or your own API key), and
LLMTool lets a model call your Wolfram Language functions. Version 15
builds an AI assistant into notebooks.
Good practice
- Split data into training and test sets (
RandomSamplethenTakeDrop), and seed randomness withSeedRandomfor reproducible experiments. - Treat automatic model choice as a strong baseline, then compare methods
with
ClassifierMeasurementsorPredictorMeasurements. - Save trained models with
Export["model.wmlf", c]and load them in deployed APIs.