themaninblack
themaninblack

Reputation: 167

TensorFlow Machine Learning - Making predictions on user input

I am working on a classification problem involving two classes. I need to train the model on a data set and predict the correct class after taking one value as an input for each of the attributes. Here a snippet of the data set. The classes are 0 and 1. enter image description here

Here is the code with which I am training and testing the model:

X = df.drop(["classification"], axis=1)
y = df["classification"]

x_scaler = MinMaxScaler()
x_scaler.fit(X)
column_names = X.columns
X[column_names] = x_scaler.transform(X)

X_train,  X_test, y_train, y_test = train_test_split(
        X, y, test_size= 0.2, shuffle=True)

I tried taking input from the user as follows:

userInput=input("Enter 14 attributes separated by commas")
#userInput=userInput.split(",")
#userInput=[np.float32(c) for c in userInput]

and predicting with:

pred=model.predict(userInput)

But I get the error:

AttributeError: 'str' object has no attribute 'ndim'

I also tried entering the attributes manually:

prediction=np.array([40,8,1,2,0,2,6,10,34,40,16,23,67,25])
print(prediction.shape) # Shape is (14,)
print(prediction[0].shape) # Shape is ()
print(prediction[0:1].shape) #Shape is (1,)
print(X_test[0:1].shape) #Shape is (1, 14)

and predicting with some ways like:

  (1) pred = model.predict(x=np.array(prediction[0:1].shape))
  (2) pred = model.predict(x=np.array(prediction[0].shape))
  (3) pred = model.predict(prediction)
  (4) pred = model.predict([40,8,1,2,0,2,6,10,34,40,16,23,67,25])
  (5) pred = model.predict(prediction.shape)
  (6) pred = model.predict([40],[8],[1],[2],[0],[2],[6],[10],[34],[40],[16],[23],[67],[25])

But I get this error in cases 1 to 4

ValueError: Error when checking input: expected dense_1_input to have shape (14,) but got array with shape (1,)

and this in case 5 and 6 respectively

AttributeError: 'tuple' object has no attribute 'ndim'
TypeError: predict() takes from 2 to 9 positional arguments but 15 were given

Also, I tried running this instead' and it works:

pred=model.predict(X_test)

But the prediction doesn't in any way. I tried:

(1) print(np.argmax(pred(userInput)))
(2) print(np.argmax(pred(prediction)))

Using .shape also doesn't work, and gives the error:

TypeError: 'list' object is not callable

Shape of training data: (320, 14) Shape of test data : (80, 14)

Is there any way I can take input from user, and use it for the prediction?

Upvotes: 3

Views: 870

Answers (1)

mdgr
mdgr

Reputation: 183

When you predict with pred=model.predict(userInput) you get error AttributeError: 'str' object has no attribute 'ndim' because you are passing a string to your function. You will have to split the string that you read from the terminal and convert the strings to integers.

inputString = "1, 2, 3,4"
sample = inputString.replace(" ", "").split(",")
sample = [int(x) for x in sample]
print(sample)
[1, 2, 3, 4]

For the prediction try to pass your sample to the predict method as:

pred = model.predict([[40,8,1,2,0,2,6,10,34,40,16,23,67,25]])

Upvotes: 1

Related Questions