使用机器学习/人工智能预测给定随机递增整数序列中的下一个数字 (n+1)

数据挖掘 机器学习 Python 预言
2022-01-20 13:03:48

AI 必须使用 Python 预测给定增量整数序列中的下一个数字(没有明显的模式),但到目前为止我还没有得到预期的结果!我尝试改变学习率和迭代,但到目前为止没有运气!

示例序列:[1, 3, 7, 8, 21, 49, 76, 224]

预期结果:467

找到的结果:2,795.5

费用:504579.43

PS。AI Stackexchange 和 Stackoverflow 上存在相同的线程,我被建议在这里发布!

这是我到目前为止所做的:

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()))

我尝试过的另一种方法但仍然给出错误的结果

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)
1个回答

您正在解决一个不是为 ANN 设计的问题,当您处理单变量、少量数据时,神经网络会遇到困难。

因为他们必须根据几个例子来学习解决方案。

然而,一个可能的解决方案是可以实现的:

将 numpy 导入为 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)

请注意,当我使用神经网络时,random_state 参数的影响非常大。如果你移动这个参数(整数),你会发现解的范围非常大。