1
$\begingroup$

CODE

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('D:\\test.csv')
X = dataset.iloc[:, 0:2].values
y = dataset.iloc[:, 2].values   
print (X)

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_0 = LabelEncoder()
X[:, 0] = labelencoder_X_0.fit_transform(X[:, 0])
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
print (X)
onehotencoder = OneHotEncoder(categorical_features = [0,1])
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 0:]
X = X[:, 1:]
print (X)

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print (X_test)

import keras
from keras.models import Sequential
from keras.layers import Dense
import keras.utils

classifier = Sequential()
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 9))
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu'))
# Adding the output layer
classifier.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
classifier.fit(X_train, y_train, batch_size = 10, nb_epoch = 100)

_, accuracy = classifier.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))


#predictions
dataset = pd.read_csv('D:\\test1.csv')
a = dataset.iloc[:, 0:2].values
labelencoder_a_0 = LabelEncoder()
a[:, 0] = labelencoder_a_0.fit_transform(a[:, 0])
labelencoder_a_1 = LabelEncoder()
a[:, 1] = labelencoder_a_1.fit_transform(a[:, 1])
onehotencoder = OneHotEncoder(categorical_features = [0,1])
a = onehotencoder.fit_transform(a).toarray()
a = a[:, 0:]
a = a[:, 1:]

predictions = classifier.predict_classes(a)
# summarize the first 2 cases
for i in range(2):
    print('%s => %d' % (a[i].tolist(), predictions[i]))

data after label encoders looks like

[[0 0]
 [0 1]
 [0 2]
 [0 3]
 [0 4]
 [0 5]
 [0 6]
 [0 7]
 [0 8]]

after one-hot looks like

[[1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1.]]

after [X_test = sc.transform(X_test)] looks like

[[ 0.          1.         -0.40824829 -0.40824829 -0.40824829 -0.40824829
  -0.40824829 -0.40824829 -0.40824829]
 [ 1.          0.         -0.40824829 -0.40824829 -0.40824829 -0.40824829
  -0.40824829 -0.40824829 -0.40824829]]

and X_train looks like

[[-0.40824829 -0.40824829 -0.40824829 -0.40824829  0.          2.44948974
   0.         -0.40824829 -0.40824829]
 [-0.40824829 -0.40824829 -0.40824829 -0.40824829  0.         -0.40824829
   0.         -0.40824829  2.44948974]
 [-0.40824829 -0.40824829 -0.40824829 -0.40824829  0.         -0.40824829
   0.          2.44948974 -0.40824829]
 [ 2.44948974 -0.40824829 -0.40824829 -0.40824829  0.         -0.40824829
   0.         -0.40824829 -0.40824829]
 [-0.40824829  2.44948974 -0.40824829 -0.40824829  0.         -0.40824829
   0.         -0.40824829 -0.40824829]
 [-0.40824829 -0.40824829 -0.40824829  2.44948974  0.         -0.40824829
   0.         -0.40824829 -0.40824829]
 [-0.40824829 -0.40824829  2.44948974 -0.40824829  0.         -0.40824829
   0.         -0.40824829 -0.40824829]]

It is trained successfully. But when i run prediction code it gives error

Error when checking input: expected dense_205_input to have shape (9,) but got array with shape (6,)

variable (a) after one_hot looks like

[[1. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 1.]]

I can't understand what is the problem with my prediction sample.

$\endgroup$

1 Answer 1

1
$\begingroup$

Your problem originates in the fact that your testing set consists of examples of just 6 possible classes, while your model is trained with a training set of 9 different labels.

When converting the test set to one-hot-encoding, you need to add the number of possible labels as the 'n_values' parameter. Otherwise by default it relies on the number of classes in the test set (which is 6 in you case).

$\endgroup$
3
  • $\begingroup$ O yesss thanks a lot. The error is removed by using n_value parameter. but how ??? i mean what n_value do with my data set. $\endgroup$ Aug 15, 2019 at 13:08
  • $\begingroup$ Only you know the answer to this question. How many different classes do you want your model to be able to recognize and classify? $\endgroup$
    – Mark.F
    Aug 15, 2019 at 13:38
  • $\begingroup$ only 2. i have set it with 0 and 1. $\endgroup$ Aug 15, 2019 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.