I am trying to train a neural network to predict the next point of a sine wave using MLPRegressor, looking at performances I cannot get the R2 to be above 0.90, any help would be greatly appreciated!
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
import numpy as np
import pandas as pd
# Create an `np.array` containing a sequence of 10 values of the sine function.
seq = np.array([np.sin(2*np.pi*t/10) for t in range(10)])
#generate 100 pairs of two-point sequences from the values in `seq`.
num_sequences = 100
x_train = np.array([])
y_train = np.array([])
for i in range(num_sequences):
rand = np.random.randint(10)
x_train = np.append(x_train, [[seq[rand], seq[np.mod(rand 1, 10)]]])
y_train = np.append(y_train, seq[np.mod(rand 1, 10)])
x_train = np.resize(x_train, (100, 2))
x_test = np.array([seq, np.roll(seq, -1)])
y_test = np.array(np.roll(seq, -2))
x_test = np.reshape(x_test, (-1, 2))
# Place the data in a `pandas` `DataFrame`.
pdata = pd.DataFrame({'x1':x_train[:,0], 'x2':x_train[:,1],'y':y_train})
reg_model = MLPRegressor(hidden_layer_sizes = (64,64,64), activation = "relu", random_state = 2, max_iter = 400)
reg_model.fit(x_train, y_train)
y_pred = reg_model.predict(x_test)
print(' R2 = ', r2_score(y_pred, y_test))
print('MAE = ', mean_absolute_error(y_pred, y_test))
print('MSE = ', mean_squared_error(y_pred, y_test))
CodePudding user response:
Your data was not correctly set up.
You can use this code to create a sine wave:
sin_wave = np.sin(np.arange(-1000, 1000, 0.1))
Then use this code to create data for training and testing:
input_seq = np.array([sin_wave[x:x 2] for x in range(len(sin_wave)-2)])
output = np.array([sin_wave[x 2] for x in range(len(sin_wave)-2)])
x_train = input_seq[:1500]
y_train = output[:1500]
x_test = input_seq[1500:]
y_test = output[1500:]
Finally, use this code to build, train and evaluate your model:
reg_model = MLPRegressor(hidden_layer_sizes = (64,64,64), activation = "relu", random_state = 2, max_iter = 400)
reg_model.fit(x_train, y_train)
y_pred = reg_model.predict(x_test)
print(' R2 = ', r2_score(y_pred, y_test))
print('MAE = ', mean_absolute_error(y_pred, y_test))
print('MSE = ', mean_squared_error(y_pred, y_test))
The complete code:
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
import numpy as np
sin_wave = np.sin(np.arange(-1000, 1000, 0.1))
input_seq = np.array([sin_wave[x:x 2] for x in range(len(sin_wave)-2)])
output = np.array([sin_wave[x 2] for x in range(len(sin_wave)-2)])
# Split into Train and test
x_train = input_seq[:1500]
y_train = output[:1500]
x_test = input_seq[1500:]
y_test = output[1500:]
reg_model = MLPRegressor(hidden_layer_sizes = (64,64,64), activation = "relu", random_state = 2, max_iter = 400)
reg_model.fit(x_train, y_train)
y_pred = reg_model.predict(x_test)
print(' R2 = ', r2_score(y_pred, y_test))
print('MAE = ', mean_absolute_error(y_pred, y_test))
print('MSE = ', mean_squared_error(y_pred, y_test))