Use Artificial Intelligence to predict next number (n+1) in a sequence

2024/10/5 20:02:29

The AI must predict the next number in a given sequence of incremental integers using Python, but so far I haven't gotten the intended result. I tried changing the learning rate and iterations but so far without any luck.

The next number is supposed to be predicted based on this PATTERN:

First number in the sequence (1) is a random int in the interval of [2^0 (current index), 2^1(next index) and so on so forth...

The AI should be able to make the decision of which number to choose from the interval

The problem I encountered is implementing the pattern mentioned above into the AI so it can predict the n+1, since I am fairly new to machine learning I don't know how to feed the AI that pattern and which libraries I have to work with.

This is the code I used:

import numpy as np# Init sequence
data =\[[1, 3, 7, 8, 21, 49, 76, 224, 467, 514, 1155, 2683, 5216, 10544, 51510, 95823,198669, 357535, 863317, 1811764, 3007503, 5598802, 14428676, 33185509, 54538862,111949941, 227634408, 400708894, 1033162084, 2102388551, 3093472814, 7137437912, 14133072157,20112871792, 42387769980, 100251560595, 146971536592, 323724968937, 1003651412950, 1458252205147,2895374552463, 7409811047825, 15404761757071, 19996463086597, 51408670348612, 119666659114170,191206974700443, 409118905032525, 611140496167764, 2058769515153876, 4216495639600700, 6763683971478124,9974455244496710, 30045390491869460, 44218742292676575, 138245758910846492, 199976667976342049,525070384258266191]]X = np.matrix(data)[:, 0]
y = np.matrix(data)[:, 1]def J(X, y, theta):theta = np.matrix(theta).Tm = len(y)predictions = X * thetasqError = np.power((predictions-y), [2])return 1/(2*m) * sum(sqError)dataX = np.matrix(data)[:, 0:1]
X = np.ones((len(dataX), 2))
X[:, 1:] = dataX# gradient descent function
def gradient(X, y, alpha, theta, iters):J_history = np.zeros(iters)m = len(y)theta = np.matrix(theta).Tfor i in range(iters):h0 = X * thetadelta = (1 / m) * (X.T * h0 - X.T * y)theta = theta - alpha * deltaJ_history[i] = J(X, y, theta.T)return J_history, theta
print('\n'+40*'=')# Theta initialization
theta = np.matrix([np.random.random(), np.random.random()])# Learning rate
alpha = 0.02# Iterations
iters = 1000000print('\n== Model summary ==\nLearning rate: {}\nIterations: {}\nInitial 
theta: {}\nInitial J: {:.2f}\n'.format(alpha, iters, theta, J(X, y, theta).item()))
print('Training model... ')# Train model and find optimal Theta value
J_history, theta_min = gradient(X, y, alpha, theta, iters)
print('Done, Model is trained')
print('\nModelled prediction function is:\ny = {:.2f} * x + {:.2f}'.format(theta_min[1].item(), theta_min[0].item()))
print('Cost is: {:.2f}'.format(J(X, y, theta_min.T).item()))# Calculate the predicted profit
def predict(pop):return [1, pop] * theta_min# Now
p = len(data)
print('\n'+40*'=')
print('Initial sequence was:\n', *np.array(data)[:, 1])
print('\nNext numbers should be: {:,.1f}'.format(predict(p).item()))
Answer

I think there is no necessity to use AI, the linear regression model is good enough for this task.

Input=[('scale',StandardScaler()),('model',LinearRegression())] # Standardizes the data
pipe=Pipeline(Input)
# perform prediction using a linear regression model using the features Z and targets y
pipe.fit(Z,y)
ypipe=pipe.predict(Z)
https://en.xdnf.cn/q/120102.html

Related Q&A

Calculating size folder python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 8…

Answer Error, Only outputting Zero

I am coding in python and I cannot seem to figure out why when the amount of tickets sold is entered it does not calculate the full price of the tickets that were sold. Any help is appreciated, Thanks.…

finding a pattern match and concatenating the rest of lines in python

I have a small data set to clean. I have opened the text file in Pycharm. The data set is like this:Code-6667+ Name of xyz company+ Address + Number+ Contact person+ Code-6668+ Name of abc company, A…

Flat a list of containing only one dict

I have a dict which contain a list product which will contain only one dict: d = {"thickness": 90.0,"mass_surf": 37.8,"res_therm": 0.75,"codename": "codenam…

How to breakup a list of list in a given way in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Incrementing a counter while assigning it in python

Is it possible to do the following in python somehow?nodename = node%s % (++num if ct[0] != J else num)Or do I need to first do the increment and the assign it on the next line:if ct[0] != J:num += 1n…

Append two arrays together into one? (Numpy/Python)

I currently have an array of strings and Im trying to join it together with another array of strings to form a complete word to do some web parsing. For example:`Var1 [A B C, .... ....] Var2 …

Python 2.7 Isolate multiple JSON objects in a string

Ive to parse a text file that contains different kind of data. The most challenging is a line that contains three different JSON object (strings) and other data between them. Ive to divide the Json da…

pass different C functions with pointer arrays as the function argument to a class

I am trying to pass different functions which have pointers as arguments to a python function. One example of the input function as input parameter is the given normal function: Sample.pyxfrom cpython …

dynamic filter choice field in django

I am using django-filter to filter results on a page. I am able to use static choice filters like this:filters.pyFILTER_CHOICES = ( (, All States), (AL, Alabama), (AK, Alaska), (AZ, Arizona), #and so f…