Search code examples
pythonmachine-learningnumberstraining-data

Generate a trained model on number series and identify the next number


I want to create a model in which i will be passing a number series and it will identify the series pattern and will generate the next value.

ex: Input: 2, 4, 6, 8, 10, 12, 14 ,16.....200

Above is my input, my model will identify itself that it's a 2x series and will give 202, 204,.... as output.

Can this be done using an machine learning algorithm?


Solution

  • This is a basic model I created for your case:

    import numpy as np
    def models(array):
        if (array[2] - array[1]) == (array[-1] - array[-2]):
            desc = "Model is linear"
            val = array[-1] + (array[2] - array[1])
        elif (array[2] / array[1]) == (array[-1] / array[-2]):
            desc = "Model is exponential"
            val = array[-1] * (array[2]/array[1])
        else: 
            desc = "Model is not linear nor exponential"
            val = np.nan
        return desc, val
    

    Here I am trying it:

    c = [1,2,3,4,5]
    print(models(c))
    

    Output (should be 6):

    ('Model is linear', 6)
    

    Another attempt with different numbers:

    c = [2,4,8,16,32]
    print(models(c))
    

    Output (should be 64):

    ('Model is exponential', 64.0)
    

    Regardless of this particular case, you should try and find Machine Learning courses to understand what can you achieve with machine learning and learn how to program an actual machine learning model. Otheriwse the scope is just too wide.