3
$\begingroup$

The AI must predict the next number in a given sequence of incremental integers (with no obvious pattern) using Python but so far I don't get the intended result! I tried changing the learning rate and iterations but so far no luck!

Example sequence: [1, 3, 7, 8, 21, 49, 76, 224]

Expected result: 467

Result found : 2,795.5

Cost: 504579.43

PS. The same thread exists on AI Stackexchange & Stackoverflow and I've been advised to post it here!

This is what I've done so far:

import numpy as np # Init sequence data =\ [ [0, 1.0], [1, 3.0], [2, 7.0], [3, 8.0], [4, 21.0], [5, 49.0], [6, 76.0], [7, 224.0] ] X = np.matrix(data)[:, 0] y = np.matrix(data)[:, 1] def J(X, y, theta): theta = np.matrix(theta).T m = len(y) predictions = X * theta sqError = 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).T for i in range(iters): h0 = X * theta delta = (1 / m) * (X.T * h0 - X.T * y) theta = theta - alpha * delta J_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 = 1000000 print('\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())) 

Another method I tried but still giving wrong results

import numpy as np from sklearn import datasets, linear_model # Define the problem problem = [1, 3, 7, 8, 21, 49, 76, 224] # create x and y for the problem x = [] y = [] for (xi, yi) in enumerate(problem): x.append([xi]) y.append(yi) x = np.array(x) y = np.array(y) # Create linear regression object regr = linear_model.LinearRegression() regr.fit(x, y) # create the testing set x_test = [[i] for i in range(len(x), 3 + len(x))] # The coefficients print('Coefficients: \n', regr.coef_) # The mean squared error print("Mean squared error: %.2f" % np.mean((regr.predict(x) - y) ** 2)) # Explained variance score: 1 is perfect prediction print('Variance score: %.2f' % regr.score(x, y)) # Do predictions y_predicted = regr.predict(x_test) print("Next few numbers in the series are") for pred in y_predicted: print(pred) 
$\endgroup$
4
  • $\begingroup$ why do you have 467 in your data array? $\endgroup$ Commented Apr 27, 2019 at 19:46
  • $\begingroup$ @JuanEstebandelaCalle that was a typo, 467 is the expected result, I updated the question $\endgroup$ Commented Apr 27, 2019 at 19:49
  • $\begingroup$ Solve this with classical statistics not with learning. Statistics of distributions given you many much more powerful methods for such small data. $\endgroup$ Commented Apr 28, 2019 at 9:48
  • $\begingroup$ so I guess it didn't work out since the bitcoin puzzle is still unsolved... $\endgroup$ Commented Nov 10, 2021 at 15:08

1 Answer 1

3
$\begingroup$

You are solving a problem which is not designed for a ANN, neural network has difficulties when you deal with univariate, few data.

Because they will have to learn a solution based on few examples.

However, a possible solution is achievable:

import numpy as np

# Init sequence data = [[0, 1.0], [1, 3.0], [2, 7.0], [3, 8.0], [4, 21.0], [5, 49.0], [6, 76.0], [7, 224.0]] X = np.matrix(data)[:, 0] y = np.matrix(data)[:, 1] Reg=neural_network.MLPRegressor(solver='lbfgs',random_state=4,hidden_layer_sizes=(100,20,2),learning_rate='adaptive',verbose=True) y2 = np.ravel(y) F=Reg.fit(X=X,y=y2,) F.predict(8) 

Note that the random_state parameter has a very large influence when I use a Neural Network. If you move this parameter (integer) you will find that the solution has a really large range.

$\endgroup$
2
  • $\begingroup$ Can you please be kind and elaborate on how to implement this with the code I have ? I am fairly new in Machine learning $\endgroup$ Commented Apr 27, 2019 at 22:02
  • $\begingroup$ I modified my answer so you can concatenate it with the code you have $\endgroup$ Commented Apr 27, 2019 at 22:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.