I created a simple regression model for training on a csv data. I have successfully done the training and evaluation as well, but when I am trying to pass a single row for predicting its output, I am getting a input shape error.
I have tried it in google collab.
Here is the code below.
# load libraries and csv
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
insurance_data = pd.read_csv("https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/master/insurance.csv")
insurance_data.head()
# import the Classes.
from sklearn.compose import make_column_transformer
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
from sklearn.model_selection import train_test_split
# set up Normalization and One Hot encoding for columns.
CT = make_column_transformer(
(MinMaxScaler(), ["age", "bmi", "children"]), #normalize all these columns only.
(OneHotEncoder(handle_unknown="ignore"), ["sex", "smoker", "region"])
)
# make X and Y.
X1 = insurance_data.drop("charges", axis=1)
Y1 = insurance_data["charges"]
# make Train and Test data.
X1_train, X1_test, Y1_train, Y1_test = train_test_split(X1, Y1, test_size=0.2, random_state=42)
# Fit column transformer to our Training data only.
CT.fit(X1_train)
# transform training and test data with Normalization
X1_train_normalized = CT.transform(X1_train)
X1_test_normalized = CT.transform(X1_test)
X1_train.shape, X1_train_normalized.shape
# make the model,
tf.random.set_seed(43)
insurance_v3 = tf.keras.Sequential([
tf.keras.layers.Input(shape=(11)),
tf.keras.layers.Dense(100, activation=None, name="hidden1"),
tf.keras.layers.Dense(10, activation=None, name="hidden2"),
tf.keras.layers.Dense(1, activation=None, name="out1")
], name="insurance_model_v3")
insurance_v3.compile(
loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
metrics=["mae"]
)
insurance_v3.fit(X1_train_normalized, Y1_train, epochs=100, verbose=0)
insurance_v3.evaluate(X1_test_normalized, Y1_test)
# test custom output.
t = X1_train_normalized[2]
t = tf.convert_to_tensor(t, tf.float64)
t = tf.expand_dims(t, 1)
t
insurance_v3.predict(t)
I have taken a row from the Normalized X training (which I am not supposed to do) just to see whether it accepts it and returns a Y value. But it is not working at all.
Can you tell me how to do it properly so than I can take values from a website form, turn it into a tensor and then pass it to the model and then the model returns a value? as I am supposed to use this model in a django app.
I tried converting the row into a tensor but everytime it is giving me one or the other error. At first, I am getting an ndim error of required dimension of 2, but then sometimes I get an error of unrank tensor.
You must reshape input (t) before feeding it to the network, and for reshaping tensors you must enable behavior. try this, it runs successfully:
from tensorflow.python.ops.numpy_ops import np_config
np_config.enable_numpy_behavior()
t = t.reshape(1, -1)
insurance_v3.predict(t)