I'm new to StackOverflow and I don't speak good english. But I hope you can help me. My problem is that I have a model created with a dataset using tensorflow/keras but I don't know how I can use that model to predict the answer (0/1). Here is my code:
Create Model
import pandas as pd
from sklearn.model_selection import train_test_split
dataset = pd.read_csv("database2.csv")
x = dataset.drop(columns=["good/bad"])
y = dataset["good/bad"]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Dense
from sklearn.metrics import accuracy_score
model = Sequential()
model.add(Dense(units=32, activation="relu", input_dim=len(x_train.columns)))
model.add(Dense(units=64, activation="relu"))
model.add(Dense(units=1, activation="sigmoid"))
model.compile(loss="binary_crossentropy", optimizer="sgd", metrics=["accuracy"])
model.fit(x_train, y_train, epochs=200, batch_size=32)
y_hat = model.predict(x_test)
y_hat = [0 if val < 0.5 else 1 for val in y_hat]
print(accuracy_score(y_test, y_hat))
model.save("tfmodel2.model")
database2.csv
good/bad,version,ihl,len_,id_,frag,ttl,dport,seq,ack,dataofs,reserved,window,urgptr
1,4,5,52,12811,0,128,80,3370329709,0,8,0,64240,0
1,4,5,52,0,0,64,59331,1464637201,3370329710,8,0,64240,0
1,4,5,52,12812,0,128,80,3129455413,0,8,0,64240,0
1,4,5,52,0,0,64,59332,619700928,3129455414,8,0,64240,0
1,4,5,52,12813,0,128,80,3501094825,0,8,0,64240,0
1,4,5,52,0,0,64,59333,2779039288,3501094826,8,0,64240,0
1,4,5,40,12814,0,128,80,3370329710,1464637202,5,0,256,0
1,4,5,40,12815,0,128,80,3129455414,619700929,5,0,256,0
1,4,5,60,12816,0,128,80,3129455414,619700929,5,0,256,0
1,4,5,40,60164,0,64,59332,619700929,3129455434,5,0,502,0
1,4,5,480,12817,0,128,80,3129455434,619700929,5,0,256,0
1,4,5,40,60165,0,64,59332,619700929,3129455874,5,0,501,0
1,4,5,60,12818,0,128,80,3129455874,619700929,5,0,256,0
1,4,5,40,60166,0,64,59332,619700929,3129455894,5,0,501,0
1,4,5,1500,12819,0,128,80,3129455894,619700929,5,0,256,0
1,4,5,40,60167,0,64,59332,619700929,3129457354,5,0,501,0
1,4,5,80,12820,0,128,80,3129457354,619700929,5,0,256,0
1,4,5,40,60168,0,64,59332,619700929,3129457394,5,0,501,0
1,4,5,720,12821,0,128,80,3129457394,619700929,5,0,256,0
1,4,5,40,60169,0,64,59332,619700929,3129458074,5,0,501,0
1,4,5,60,12822,0,128,80,3370329710,1464637202,5,0,256,0
1,4,5,40,57184,0,64,59331,1464637202,3370329730,5,0,502,0
1,4,5,1500,12823,0,128,80,3129458074,619700929,5,0,256,0
1,4,5,40,60170,0,64,59332,619700929,3129459534,5,0,501,0
1,4,5,1500,12824,0,128,80,3129459534,619700929,5,0,256,0
1,4,5,40,60171,0,64,59332,619700929,3129460994,5,0,494,0
1,4,5,1340,12825,0,128,80,3370329730,1464637202,5,0,256,0
1,4,5,40,57185,0,64,59331,1464637202,3370331030,5,0,501,0
1,4,5,40,12826,0,128,80,3501094826,2779039289,5,0,256,0
1,4,5,200,12827,0,128,80,3370331030,1464637202,5,0,256,0
1,4,5,40,57186,0,64,59331,1464637202,3370331190,5,0,501,0
1,4,5,140,12828,0,128,80,3129460994,619700929,5,0,256,0
1,4,5,40,60172,0,64,59332,619700929,3129461094,5,0,501,0
1,4,5,1500,12829,0,128,80,3370331190,1464637202,5,0,256,0
1,4,5,40,57187,0,64,59331,1464637202,3370332650,5,0,501,0
1,4,5,60,12830,0,128,80,3501094826,2779039289,5,0,256,0
1,4,5,40,5921,0,64,59333,2779039289,3501094846,5,0,502,0
How can I use this model to predict something like: 4,5,40,60185,0,64,59332,619701397,3129468394,5,0,501,0
Thanks for supporting me!
CodePudding user response:
but you already have it in your code already:
y_hat = model.predict(x_test)
may be add something like this after it
for i in range(len(y_hat)): print(x_test[i], y_hat[i])
CodePudding user response:
Your dataset is really tiny and your hyperparameters batch_size
, learning_rate
, and epochs
need to be adapted. Your labels good/bad
are always 1, which is strange. But anyway, to make a prediction on your model (which you are actually already doing), just output your predictions:
dataset = pd.read_csv("database2.csv")
x = dataset.drop(columns=["good/bad"])
y = dataset["good/bad"]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from sklearn.metrics import accuracy_score
optimizer = SGD(learning_rate=0.0001)
model = Sequential()
model.add(Dense(units=32, activation="relu", input_dim=len(x_train.columns)))
model.add(Dense(units=64, activation="relu"))
model.add(Dense(units=1, activation="sigmoid"))
model.compile(loss="binary_crossentropy", optimizer=optimizer, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=32)
y_hat = model.predict(x_test)
print('Predictions \n', y_hat)
y_hat = [0 if val < 0.5 else 1 for val in y_hat]
print(accuracy_score(y_test, y_hat))
model.save("tfmodel2.model")
'''
Predictions
[[0.]
[1.]
[0.]
[1.]
[1.]
[1.]
[0.]
[1.]]
Accuracy: 0.625
'''
Your test
dataset has 8 entries, that is why you are seeing 8 predictions, which are either 0 or 1.