Reputation: 37
this is driving me crazy. I want to use Keras to predict the value in g for a pressure sensor. I created this code to predict the values based on the voltage output that I measure. The accuracy is very low which, from what I understand, is due to the small data sample. Accordingly the prediction output quality is rather low, which is ok for now.
What I do not figure out: I try to predict a single value and the result is off completely, i.e. when I input 3.4, the result is around 10, whereas it should be around 1000. When I put more values in the input array X_new, the result quality increases drastically (already with two values). What am I missing here? Any input would be highly appreciated.
Here is my code:
import numpy as np
from sklearn import preprocessing, model_selection
from matplotlib import pyplot
from keras.layers import Dense, Activation, LSTM
from keras.models import Sequential, load_model
X = np.array([0.9, 1.75, 2.25, 2.45, 2.7, 2.9, 3.08, 3.2, 3.32, 3.4, 3.45])
y = np.array([10, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000])
X_scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))
y_scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))
X_scaled = (X_scaler.fit_transform(X.reshape(-1, 1)))
y_scaled = (y_scaler.fit_transform(y.reshape(-1, 1)))
X_train, X_test, y_train, y_test = model_selection.train_test_split(X_scaled, y_scaled, test_size=0.4, random_state=3)
model = Sequential()
model.add(Dense(128, input_shape=(1,), activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam', metrics=["accuracy"])
np.random.seed(3)
model.fit(X_train, y_train, epochs=256, batch_size=2, verbose=2)
_, accuracy = model.evaluate(X_train, y_train)
print('Accuracy: %.2f' % (accuracy*100))
model.save("workload_model/model.h5")
predicted = model.predict(X_test)
pyplot.plot(y_scaler.inverse_transform(y_train), color="red")
pyplot.plot(y_scaler.inverse_transform(predicted), color="blue")
pyplot.plot(y_scaler.inverse_transform(y_test), color="green")
print("X=%s\nPredicted=%s" % (X_scaler.inverse_transform(X_test), y_scaler.inverse_transform(predicted)))
# Test with new value
loaded_model = load_model("workload_model/model.h5")
X_new = np.array([3.4])
X_scaled = (X_scaler.fit_transform(X_new.reshape(-1, 1)))
predicted = loaded_model.predict(X_scaled)
print("X=%s\nPredicted=%s" % (X_scaler.inverse_transform(X_scaled), y_scaler.inverse_transform(predicted)))
pyplot.show()
Upvotes: 0
Views: 391
Reputation: 772
X_new = np.array([3.4])
X_scaled = (X_scaler.fit_transform(X_new.reshape(-1, 1)))
predicted = loaded_model.predict(X_scaled)
this line should be
X_scaled = (X_scaler.transform(X_new.reshape(-1, 1)))
You're changing your scaler, that's why it works better with more data
Upvotes: 1