Welcome to the final chapter! In the previous chapter, String Algorithms, we learned how to find exact patterns in text, like searching for a specific word in a book.
But what if the pattern isn't exact? What if you want a computer to look at a photo and say, "That's a cat," or look at a house's size and guess its price? You can't write an if statement for every possible cat photo.
Machine Learning (ML) is a paradigm shift. Instead of giving the computer strict rules, we give it data and let it learn the rules itself.
Imagine you are a botanist. You have a pile of Iris flowers. You know the Petal Length and Petal Width of each.
The Old Way: You write code: if (length < 2) return "Setosa";
The Problem: What about the medium ones? The border is fuzzy. Your rules get complicated quickly.
The ML Way: You feed 100 measurements into a Neural Network and say: "These are the answers. Figure out the math."
A Neural Network is inspired by the human brain. It is made of layers of "Neurons."
Each line connecting a circle is a Weight. Think of a weight as a "Volume Knob."
How does the network make a decision? It takes the inputs, multiplies them by the Weights, adds them up, and pushes the result through an Activation Function.
The Activation Function (like Sigmoid or ReLU) decides if the neuron should "fire." It's like a filter that turns the math result into a simple number between 0 and 1.
Output = Activation(Input * Weight + Bias)When the network starts, the weights are random. It knows nothing. It might guess "Setosa" for a giant flower.
Training Loop:
This repeats thousands of times until the error is near zero.
We will use the provided NeuralNetwork class to solve our flower problem.
We need to tell the system how many layers we want.
// From machine_learning/neural_network.cpp
using namespace machine_learning::neural_network;
// Define structure: {Neurons, Activation Function}
NeuralNetwork myNN({
{4, "none"}, // Input Layer
{6, "relu"}, // Hidden Layer (uses ReLU logic)
{3, "sigmoid"} // Output Layer (uses Sigmoid logic)
});
We give the network data (iris.csv). We tell it to study (Epochs) 100 times.
// Train the model
// Arguments: Filename, Last_Col_Is_Label, Epochs, Learning_Rate...
myNN.fit_from_csv(
"iris.csv",
true, // The last column contains the answer
100, // Study the data 100 times
0.3, // Learning Rate (how fast to change weights)
false // Normalization flag
);
Now that it has learned, we give it a new flower it has never seen before.
// A new flower with measurements: {5.0, 3.4, 1.6, 0.4}
std::vector<std::valarray<double>> new_flower = {{5, 3.4, 1.6, 0.4}};
// Ask the network
auto result = myNN.single_predict(new_flower);
// The result is a list of probabilities, e.g., {0.9, 0.05, 0.05}
// Index 0 (Setosa) has the highest probability.
How does the C++ code actually "learn"? Let's look at the sequence of events during one training step.
Inside __detailed_single_prediction, the code loops through layers. It multiplies the input by the kernel (weights) and applies the activation function.
// Simplified from machine_learning/neural_network.cpp
// current_pass holds the data moving through the network
for (const auto &l : layers) {
// 1. Matrix Multiplication: Data * Weights
current_pass = multiply(current_pass, l.kernel);
// 2. Activation: Apply Sigmoid/Relu to the result
current_pass = apply_function(current_pass, l.activation_function);
// Save state for later
details.emplace_back(current_pass);
}
This is where the learning happens inside fit(). We calculate the cur_error and update l.kernel (the weights).
// Simplified from machine_learning/neural_network.cpp inside fit()
// 1. Calculate how much the weights contributed to the error
// This uses the "Chain Rule" of calculus (derivative)
grad = multiply(transpose(activations[j]), cur_error);
// 2. Adjust the weights (Kernel)
// We subtract the gradient multiplied by the Learning Rate
this->layers[j].kernel = this->layers[j].kernel - (gradients[j] * learning_rate);
Analogy:
kernel (Weights) eventually becomes perfect at predicting the flower type.Congratulations! You have completed the Machine Learning chapter and the C-Plus-Plus tutorial series.
We moved from storing simple numbers in Fundamental Data Structures, to sorting them, to connecting them in Graph Algorithms, and finally, to building a mathematical brain that learns patterns from them.
Key Takeaways from this chapter:
You now possess the foundational knowledge of algorithms in C++. From here, you can build search engines, secure communication tools, or intelligent AI systems. Happy coding!
Generated by Code IQ