|
|
|
|
|
|
|
|
|
|
|
|
|
import tensorflow as tf |
|
import pandas as pd |
|
import seaborn as sns |
|
import matplotlib.pyplot as plt |
|
import os |
|
|
|
|
|
|
|
|
|
|
|
df = pd.read_csv('C://Users//luona//Downloads//Kilometres-miles.csv') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print('Define input(X) and output(Y) variables') |
|
X_train = df['Kilometres'] |
|
Y_train = df['Miles'] |
|
|
|
|
|
|
|
|
|
|
|
print('Creating the model') |
|
model = tf.keras.Sequential() |
|
model.add(tf.keras.layers.Dense(units=1, input_shape=[1])) |
|
|
|
|
|
|
|
|
|
|
|
print('Compiling the model') |
|
model.compile(optimizer=tf.keras.optimizers.Adam(1), loss='mean_squared_error') |
|
|
|
|
|
|
|
|
|
|
|
print('Training the model') |
|
epochs_hist = model.fit(X_train, Y_train, epochs = 250) |
|
|
|
|
|
|
|
|
|
|
|
print('Evaluating the model') |
|
print(epochs_hist.history.keys()) |
|
|
|
|
|
plt.plot(epochs_hist.history['loss']) |
|
plt.title('Evolution of the error associated with the model') |
|
plt.xlabel('Epoch') |
|
plt.ylabel('Training Loss') |
|
plt.legend('Training Loss') |
|
plt.show() |
|
|
|
|
|
|
|
|
|
|
|
kilometers = 100 |
|
predictedMiles = model.predict([kilometers]) |
|
print("The conversion from Kilometres to Miles is as follows: " + str(predictedMiles)) |
|
|
|
|
|
|
|
|
|
|
|
milesByFormula = kilometers * 0.6214 |
|
print("The conversion from kilometers to miles using the mathematical formula is as follows:" + str(milesByFormula)) |
|
diference = milesByFormula - predictedMiles |
|
print("Prediction error:" + str(diference)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|