How use Tensorflow model predict single dataset?

15 Views Asked by At

I make simple tensorflow model and try to use model predict sample of single data set but I not work. the code as below.

import tensorflow as tf
import pandas as pd
from sklearn.compose import make_column_transformer
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
from sklearn.model_selection import train_test_split

insurance = pd.read_csv("https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/master/insurance.csv")

scalar=MinMaxScaler()
ct=make_column_transformer(
    (MinMaxScaler(),['age','bmi','children']),
    (OneHotEncoder(handle_unknown='ignore'),['sex','smoker','region'])
)

X=insurance.drop('charges',axis=1)
y=insurance['charges']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

ct.fit(X_train)
X_train_normal=ct.transform(X_train)
X_test_normal=ct.transform(X_test)

tf.random.set_seed(42)

model=tf.keras.Sequential([
    tf.keras.layers.Dense(100,activation='relu'),
    tf.keras.layers.Dense(50,activation='relu'),
    tf.keras.layers.Dense(1,activation='relu')
])

model.compile(loss=tf.keras.losses.mean_absolute_error,
              optimizer=tf.keras.optimizers.legacy.Adam(),
              metrics=tf.keras.metrics.mean_absolute_error
              )
history=model.fit(X_train_normal,y_train,epochs=200,verbose=0)
model_eva=model.evaluate(X_test_normal,y_test)

first_set=insurance.drop('charges',axis=1).loc[0]
first_set=ct.transform(first_set)
pred=model.predict(first_set)

It should to predict the "first_set" but It can't. it show

first_set=ct.transform(first_set)

ValueError: X does not contain any features, but ColumnTransformer is expecting 6 features

How can I fix it. thanks

0

There are 0 best solutions below