What is Machine Learning?

dakota_day

Dakota Day

Posted on July 29, 2024

What is Machine Learning?

When I first hear the words 'machine learning', I think "AI that has the ability to grow more accurate over time".

While my first thoughts feel fairly accurate, IBM gives us a more direct definition: "A branch of AI and computer science that focuses on using data and algorithms to enable AI to imitate the way that humans learn, gradually improving its accuracy."
Given that information, how does it work?

Algorithms

Machine learning is a pretty broad topic when looking at models and algorithms. We can break them down by looking at the ways they learn:

  • Supervised Learning

Supervised Learning
Uses labeled datasets to train algorithms to predict outcomes and recognize patterns. We, the "supervisor", provide the dataset and the desired outcomes.

Some popular algorithms that use this technique are:
--Naive Bayes Classifier
--Decision Trees
--K Nearest Neighbors
--Linear Regression
--Neural Networks

  • Unsupervised Learning

Unsupervised Learning
Uses unlabeled datasets to learn for itself.
Since the datasets are unlabeled, the algorithms can only use clustering or association techniques to group similar inputs together

  • Reinforcement Learning Algorithms here are reward based. That means the program makes decisions on whether it gains or loses rewards. Positive reinforcement occurs when an event happens due to a certain behavior, that behavior increases strength or frequency. Negative reinforcement strengthens a behavior when a negative condition is avoided. The learning for this technique is iterative, so it learns from its mistakes and passes along the knowledge to the next generation. Think of those AI that are made to speedrun videogames!

Neural Networks

We'll take a look at neural networks. Neural networks are designed to to mimic our own brain's thought patterns. They can be used to do things like recognize patterns in speech, translating, and much more! They learn from whatever data is input and make predictions based on that data, growing more accurate the more data there is. I'll be using a simple program, courtesy of the LearnCode.academy YouTube channel, to show off how neural networks work.

For our example program we'll have some html that has a color picker input that will set our background color and some example text that is white by default.

Brain.js

Brain.js is a JavaScript library used for making neural networks, and we'll be using it for our program.

A neural network, can be used to determine if a background is too dark or too light, and change the color of text to make it more readable.

Making the brain

Let's start by making our brain:

//this initializes a new neural network
const network = new brain.NeuralNetwork();
Enter fullscreen mode Exit fullscreen mode

Easy enough.

Brain Training

Human brains learn from information given to them, neural networks are the same. We can use the .train method on the network. This is essentially what the network will use to predict outcomes. In our case, this will determine whether or not to change our text color. Let's give our network some starting data.

//.train takes an array that holds our test data as a parameter
network.train([

//we can get our rgb values by dividing a color's values by 255
//the output should be what you think the background is: light or dark in this case
{input: {r: 0, g: 1, b: 0}, output: {light: 1}},
{input: {r: 0.33, g: 0.24, b: 0.29}, output: {dark: 1}},
]);
Enter fullscreen mode Exit fullscreen mode

Now let's do the event listener for our color input! This is where we'll be determining whether the background is light or dark.

input.addEventListener('input', (color) => {
  //this is just a helper function that converts hex color data to rgb values
  const rgb = getRgb(color.target.value);

  //we set the background color here
  text.style.background = color.target.value

  const result = brain.likely(rgb, network);

  text.style.color = result === 'dark' ? 'white' : 'black'
})
Enter fullscreen mode Exit fullscreen mode

So let's say we want to make our result be either our light value, or our dark value. Brain.js has a method we can use here as well. The .likely method takes our rgb value and neural network as arguments. .likely will then compare the likely hoods of the color being light or dark for us, using the data that we trained it with!

If we were to try and display this on the page we would have some weird things going on. We only provided two inputs for our training data, so our network may have some issues trying to figure out if some rgb combinations are light or dark.

Fixing this is easy! All we do is add more data to train the brain!

{input: {r: 0, g: 1, b: 0}, output: {light: 1}},
{input: {r: 0.33, g: 0.24, b: 0.29}, output: {dark: 1}},
{input: {r: 0.62, g: 0.72, b: 0.88}, output: {light: 1}},
{input: {r: 0.1, g: 0.84, b: 0.72}, output: {light: 1}},
{input: {r: 0.74, g: 0.78, b: 0.86}, output: {light: 1}},
{input: {r: 0.31, g: 0.35, b: 0.41}, output: {dark: 1}},
{input: {r: 0, g: 1, b: 0.65}, output: {light: 1}},
Enter fullscreen mode Exit fullscreen mode

With this training data, our brain will be able to more accurately predict whether the background is light or dark.

The more you think about it, machine learning is used in a lot of different areas of our life: recommendation systems, social media connections, image recognition and many more. I think it would be fairly safe to say that machine learning has greatly improved different facets of our lives. And the best part? It can only get better from here!

Check out the sources used if you want to learn more:
What Is Machine Learning (ML)
Top 10 Machine Learning Algorithms For Beginners: Supervised, and More
What is Supervised Learning?
What is Unsupervised Learning?
What is Reinforcement Learning
Brain.js Docs
Machine Learning Tutorial for Beginners - USING JAVASCRIPT!

Images used:
Unsupervised Learning
Supervised Learning

💖 💪 🙅 🚩
dakota_day
Dakota Day

Posted on July 29, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What is Machine Learning?
programming What is Machine Learning?

July 29, 2024