I'm studying neural networks, and I took some sample codes to debug ...
I would like to understand, how can I convert a float type output to binary, because when I try to make the forecast, I know the chance of the event occurring, but I don't understand which of the events will happen, in the case 0
or 1
.
This is my entry for LSTM, a 3-dimensional matrix:
[[-0.11366585 0.06156679 0.00605332 ... -1.02887191 1.458945 -1.00647382]
[ 0.89127279 0.39238284 0.03161757 ... 0.97193829 1.52031675 -0.76247354]]
...
[[ 3.09754916 0.15064888 2.71586375 ... 0.97193829 -0.03852574 0.54641066]
[-0.08069378 0.6887738 -0.29432661 ... 0.97193829 0.02207886 -0.37703]]
This is the data output:
[[0.36732605 0.6326739 ]
[0.3584979 0.64150214]
[0.5920879 0.40791208]
...
[0.5283355 0.47166446]
[0.5267493 0.4732507 ]
[0.5926927 0.4073073 ]]
how it should be:
[[1. 0.]
[1. 0.]
[0. 1.]
[1. 0.]
[0. 1.]
...
[1. 0.]
[1. 0.]
[0. 1.]
[1. 0.]
[0. 1.]]
In this case, the correct answers are those in the right column (>) and both 0 and 1 can be between 0% to 100%, it's not because it's below 50% that the answer would be 0
And this is the code of my model:
model = Sequential()
model.add(LSTM(32, input_shape=(X.shape[1], X.shape[2]), return_sequences=True))
model.add(Dropout(0.2))
# second layer
model.add(LSTM(32, return_sequences=False))
model.add(Dropout(0.2))
# fourth layer and output
model.add(Dense(16, activation='relu'))
model.add(Dense(2, activation='softmax'))
# compile layers
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
When I use the prediction function it only gives me the percentages, but it does not indicate whether it will be 0 or 1, how can I solve this?
You are getting the probabilities of each class 0 and 1. Do the following:
import numpy as np
np.argmax(output,axis=1)
If you are using Keras you can also:
model.predict_classes(X_train)