DEV Community

kw4rgs
kw4rgs

Posted on

Machine Learning: To Infinity and Beyond !

You

Imagine being in the prehistoric era of programming, where even calculating the sum of two numbers, A and B, required a formula worthy of a trigonometry treaty. But now, with a simple line of code, we can do it:

value = A + B 
Enter fullscreen mode Exit fullscreen mode

Easy, right? However, what if we want to complicate things a bit?

We decide to be finicky and set some conditions. For example, what if A is greater than zero and also greater than B? In that case, our formula remains the same: value = A + B. But what if A doesn't meet those rules? That's when we get creative! The formula becomes: value = (A + B) * -1.

def calculate_value(A, B):
    if A > 0 and A > B:
        value = A + B
    else:
        value = (A + B) * -1
    return value
Enter fullscreen mode Exit fullscreen mode

That's how we do things in the wild west of programming, yeehaw!

But wait, the fun doesn't end here. What if we want to add a bit of spice and only calculate the value when A is an even, positive integer and also divisible by 2, and B is a negative integer? And that's not all, what if we also want A to be within a specific range, say between 20 and 50, and B to be a prime number less than 20?

def is_prime(n):
    if n <= 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    i = 3
    while i * i <= n:
        if n % i == 0:
            return False
        i += 2
    return True

def calculate_value(A, B):
    if A % 2 == 0 and A > 0 and 20 <= A <= 50 and B < 0 and is_prime(abs(B)):
        value = A + B
    else:
        value = (A + B) * -1
    return value

Enter fullscreen mode Exit fullscreen mode

Exactly, time to add more conditionals as if there were no tomorrow!

exactly

It works, my dear cowboy, but let me tell you two things: first, there's a snake in your boot, and second, we don't ride horses or settle things at gunpoint anymore; there are more modern ways to get things done now, and that's where our star commando astronaut comes into play: Machine Learning!

Machine Learning can handle all these conditions with the grace of a juggler in a circus, freeing us from the complications of coded logic and allowing us to explore a territory of infinite possibilities.

Instead of being locked in a cage of rules and conditions, we simply ask the model, "Whats'up my friend, can you tell me what the value is?" And voilà, the model takes care of all the heavy lifting, leaving us free to grab a coffee and laugh about how we could have done the same with lines, rules, conditions, and more lines of code.

Machine Learning not only simplifies our lives but also adds a bit of spice to the boring task of doing mathematics. So put on your cowboy hat and get ready for an exciting ride through the wild west of machine learning.

A practical example

notflix

Imagine you're working on a recommendation system for a movie streaming platform. With conventional programming, we would have to design an algorithm that looks a bit like a spy movie: "If the user has watched more action movies than comedies, and has given more than five stars to movies featuring a certain actor, then recommend more action movies with that actor. Operation: Movie Recommendation in Progress!"

However, this approach might make you feel more like a detective trying to solve a mystery than a software engineer. And, to be honest, nobody likes being in the office until late at night searching for clues about user viewing habits.

Luckily, we have the hero of the story: Machine Learning! Instead of trying to decipher the complexities of user preferences, we simply feed historical viewing data to our model and let it do the heavy lifting. The result? Movie recommendations so perfect that they'll make you wonder if the model has been spying on your conversations with your friends about what you want to watch tonight.

For this, we can use one of these approaches:

  1. Content-based recommendation: Think of this as the friend who always suggests movies because "if you liked Toy Story, you'll surely love Shrek." It's like the system is a movie buff who knows you so well that it can predict your preferences just by looking at movie characteristics. It's like having a movie buddy who knows all your dark screen secrets and serves you the best recommendations effortlessly.

  2. Collaborative filtering: This approach is like if a bunch of your movie buff friends got together to form a "Recommendation League." They observe your viewing habits, the movies you've liked or disliked, and then take turns suggesting movies based on what they've liked. It's like if your friends said, "Hey, you loved that alien sci-fi movie, and I did too! You should watch this other one that's just as great." We just hope none of your friends are horror movie fanatics, or you'll end up with a nightmare recommendation list!

Just a reminder

Machine

Ultimately, both conventional programming and Machine Learning are powerful tools in the software development arsenal. Conventional programming has served us well for decades, allowing us to build robust and reliable systems based on rules and coded logic. However, Machine Learning has taken computer capabilities to a whole new level by allowing them to learn from data and autonomously adapt to new situations.

While conventional programming is ideal for problems with well-defined rules and clear logical structures, Machine Learning shines in situations where rules are hard to specify explicitly or where data is complex and changing. Its ability to uncover hidden patterns in large datasets and make accurate predictions makes it an invaluable tool in fields such as data analysis, computer vision, and natural language processing.

In summary, the choice between conventional programming and Machine Learning largely depends on the nature of the problem being addressed and the resources available. Both approaches have their strengths and weaknesses, and what's more important, they coexist in a world where innovation and adaptability are key.

And what if instead of having to have thought of all the conditionals to find the value, I had asked ChatGPT to give me the code?

Well, that's another tale for another post.

Top comments (0)