Is Machine Learning Algorithms different from Algorithms

Is there any difference between traditional algorithms and machine learning algorithms? After all, machine learning algorithms come from algorithms. Right? 

And then, why should we make a difference? Rather we should say, machine learning algorithms are a part of algorithms which have a history and background. 

Why?

Because, we know, thousand years ago there were algorithms which mathematicians would use to solve problems.

What kind of problems?

Like finding the GCD or the Greatest Common Divisor of two integers.

Thousand years ago Greek mathematicians used Euclidean algorithm to find the greatest common divisors of two numbers.

At that time there were no machines, or computers. As a result, there was no question of machine learning algorithms.

That’s okay.

But still we don’t get the answer.

Is there any difference between traditional algorithms and machine learning algorithms?

Well, let’s try to understand it from a different perspective.

Traditional algorithms solve a problem. Right?

What does an algorithm do?

Take an input and give an output. 

For example, let’s try to find whether a number is prime or not. 

We know that a prime number consists of two factors only. 1 and that particular number.

Like 11. Only 1 and 11 can divide 11.

But 12 is not a prime number, because there are four factors. 1, 2, 3, 6 and 12 can divide 12.

Therefore, the most common algorithm that comes to our mind is to take an input and find whether that number has more than two factors or not.

Let’s write it down. It’s not a difficult algorithm.

import math as m

take_input = input('Please enter any positive integer.')
converted_input = int(take_input)

prime_check = 0
starting_point = 1

while starting_point <= m.sqrt(converted_input):
    # 
    if(converted_input % starting_point == 0):
        if(converted_input / starting_point == 2):
            prime_check = prime_check + 1
        else:
            prime_check = prime_check + 2
    starting_point = starting_point + 1
    
print(f'Number of factors: {prime_check}')

if(prime_check == 2):
    print(f'{converted_input} is Prime')
else:
    print(f'{converted_input} is not Prime')

We can also write the same algorithm in a shorter way.

Let’s give it a try.

import math as m

take_input = input('Please enter any positive integer.')
converted_input = int(take_input)

starting_point = 1
number_of_factors = 1

while starting_point <= m.sqrt(converted_input):
    if(converted_input % starting_point == 0):
        number_of_factors = number_of_factors + 1
    
    starting_point = starting_point + 1
    
print(f'Number of factors: {number_of_factors}')
    
    
if(number_of_factors == 2):
    print(f'{converted_input} is Prime')
else:
    print(f'{converted_input} is not Prime')

Good Algorithms, bad algorithms

However, both the algorithms take much time, when we provide a big number as an input.

For example, when we provide a number like this: 987456321478963.

Both the algorithms take almost 8 minutes to finish it off.

How do we know that?

We can use Python profiling. We will discuss that topic later. 

Let’s write any one of the codes in the following way.

import math as m
import cProfile, pstats, io
from pstats import SortKey
pr = cProfile.Profile()
pr.enable()



take_input = input('Please enter any positive integer.')
converted_input = int(take_input)

starting_point = 1
number_of_factors = 1

while starting_point <= m.sqrt(converted_input):
    if(converted_input % starting_point == 0):
        number_of_factors = number_of_factors + 1
    
    starting_point = starting_point + 1
    
print(f'Number of factors: {number_of_factors}')
    
    
if(number_of_factors == 2):
    print(f'{converted_input} is Prime')
else:
    print(f'{converted_input} is not Prime')
    
    
pr.disable()
s = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())

The above code gives us the full profiling of the algorithms we have used.

Let’s see the output.

That will explain everything.

'''

Please enter any positive integer.987456321478963
Number of factors: 5
987456321478963 is not Prime
         31423822 function calls in 8.300 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 31423818    4.265    0.000    4.265    0.000 {built-in method math.sqrt}
        1    4.035    4.035    4.035    4.035 {built-in method builtins.input}
        2    0.000    0.000    0.000    0.000 {built-in method builtins.print}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

'''

It clearly says that, that the Number of factors is 5, and the number 987456321478963 is not Prime.

But at the same time, it says, there were 31423822 function calls in 8.300 seconds.

That’s too much. Isn’t it?

We can test the same with another longer version of algorithms.

import math as m
import cProfile, pstats, io
from pstats import SortKey
pr = cProfile.Profile()
pr.enable()

take_input = input('Please enter any positive integer.')
converted_input = int(take_input)

prime_check = 0
starting_point = 1

while starting_point <= m.sqrt(converted_input):
    # 
    if(converted_input % starting_point == 0):
        if(converted_input / starting_point == 2):
            prime_check = prime_check + 1
        else:
            prime_check = prime_check + 2
    starting_point = starting_point + 1
    
print(f'Number of factors: {prime_check}')

if(prime_check == 2):
    print(f'{converted_input} is Prime')
else:
    print(f'{converted_input} is not Prime')
    
    
    
pr.disable()
s = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())
    
'''

Please enter any positive integer.987456321478963
Number of factors: 8
987456321478963 is not Prime
         31423822 function calls in 8.130 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 31423818    4.239    0.000    4.239    0.000 {built-in method math.sqrt}
        1    3.891    3.891    3.891    3.891 {built-in method builtins.input}
        2    0.000    0.000    0.000    0.000 {built-in method builtins.print}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


'''

We see the same output.

Therefore, we cannot say these algorithms are good algorithms. Right?

But, we can write a good algorithm as follows.

import cProfile, pstats, io
from pstats import SortKey
pr = cProfile.Profile()
pr.enable()

num = 0

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

take_input = input('Please enter any positive integer.')
converted_input = int(take_input)

if(is_prime(converted_input)):
    print(f'{converted_input} is Prime')
else:
    print(f'{converted_input} is not Prime')
    

pr.disable()
s = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())


'''

Please enter any positive integer.987456321478963
987456321478963 is not Prime
         4 function calls in 3.305 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    3.305    3.305    3.305    3.305 {built-in method builtins.input}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.print}
        1    0.000    0.000    0.000    0.000 /home/sanjib/Documents/development/discrete-python/primes/prime-two.py:8(is_prime)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

'''

We don’t have to write anything more. 

The algorithm speaks for itself. Only 4 function calls.

But what about machine learning algorithms?

Machine learning algorithms now come into picture.

Because we have the same input but two different outputs. Based on that, we can build our machine learning models. 

Comparing the inputs and outputs we can write some new logic that gives us a prediction.

Still not clear?

Then try another definition as follows.

Our algorithms give an output that tells us which model should be better to find the prime number.

In short, machine learning learns from data and solves a specific task, making predictions, etc.

That’s the reason, we cannot think of data science without machine learning algorithms.

Now we can use this model data and predict what should be the better way to find a prime number.

However, beginners often confuse machine learning algorithms with machine learning models.

That we will learn later.

So stay tuned.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

Courses at Educative

GitHub repository

Flutter, Dart and Algorithm

Twitter

Comments

3 responses to “Is Machine Learning Algorithms different from Algorithms”

  1. […] What if I am not good at mathematics? Can I learn machine learning algorithms? […]

  2. […] In addition, algorithms are a set of rules. We lovingly call them recipes to solve problems. […]

  3. […] Because we need different types of algorithms to handle different types of […]

Leave a Reply