Robust Sine Function Fitting

Published: 28 February 2022
on channel: Mutluhan özkan
44
0

import random
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from numpy import asarray
from matplotlib import pyplot
import math
import numpy
define the dataset
x =numpy.arange(-3.14,3.14, 0.005)
y = asarray([math.sin(i)+ random.uniform(-0.3, 0.3) for i in x])
print(x.min(), x.max(), y.min(), y.max())
reshape arrays into into rows and cols
x = x.reshape((len(x), 1))
y = y.reshape((len(y), 1))
separately scale the input and output variables
scale_x = MinMaxScaler()
x = scale_x.fit_transform(x)
scale_y = MinMaxScaler()
y = scale_y.fit_transform(y)
print(x.min(), x.max(), y.min(), y.max())
design the neural network model
model = Sequential()
model.add(Dense(10, input_dim=1, activation='relu', kernel_initializer='he_uniform'))
model.add(Dense(10, activation='sigmoid', kernel_initializer='he_uniform'))
model.add(Dense(1))
define the loss function and optimization algorithm
model.compile(loss='mse', optimizer='adam')
ft the model on the training dataset
model.fit(x, y, epochs=500, batch_size=10, verbose=0)
make predictions for the input data
yhat = model.predict(x)
inverse transforms
x_plot = scale_x.inverse_transform(x)
y_plot = scale_y.inverse_transform(y)
yhat_plot = scale_y.inverse_transform(yhat)
report model error
print('MSE: %.3f' % mean_squared_error(y_plot, yhat_plot))
plot x vs y
pyplot.scatter(x_plot,y_plot, label='Actual')
plot x vs yhat
pyplot.scatter(x_plot,yhat_plot, label='Predicted')
pyplot.title('Input (x) versus Output (y) Sin Function')
pyplot.xlabel('Input Variable (x)')
pyplot.ylabel('Output Variable (y)')
pyplot.legend()
pyplot.show()


On this page of the site you can watch the video online Robust Sine Function Fitting with a duration of hours minute second in good quality, which was uploaded by the user Mutluhan özkan 28 February 2022, share the link with friends and acquaintances, this video has already been watched 44 times on youtube and it was liked by 0 viewers. Enjoy your viewing!