3

The problem with this code is that I am giving classifier, One hot encoded data: Means: X-train, X-test, y_train, y_test is one hot encoded. But the classifier is predicting the output: y_pred_test, y_pred_train in Numerical form (which I think is incorrect as well). Can anyone help with this? This is a dummy example so no concern over low accuracy but just to know why it's predicting the output in not One Hot encoded form. Thanks !


# -*- coding: utf-8 -*-
import numpy as np
import pandas as  pd


x=pd.DataFrame()
x['names']= np.arange(1,10)
x['Age'] = np.arange(1,10)

y=pd.DataFrame()
y['target'] = np.arange(1,10)


from sklearn.preprocessing import OneHotEncoder, Normalizer

ohX= OneHotEncoder()
x_enc = ohX.fit_transform(x).toarray()
ohY = OneHotEncoder()
y_enc = ohY.fit_transform(y).toarray()

print (x_enc)
print("____")
print (y_enc)

import keras
from keras import regularizers
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.models import load_model
from keras.layers.advanced_activations import LeakyReLU
marker="-------"

from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import train_test_split


def create_model(learn_rate=0.001):
    model = Sequential()
    model.add(Dense(units = 15, input_dim =18,kernel_initializer= 'normal', activation="tanh"))
    model.add(Dense(units=9, activation = "softmax"))
    model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
    return model

if __name__=="__main__":
  X_train, X_test, y_train, y_test = train_test_split(x_enc, y_enc, test_size=0.33, random_state=42)
  print ("\n\n",marker*5," Classification\nX_train shape is: ",X_train.shape,"\tX_test shape is:",X_test.shape)
  print ("\ny_train shape is: ",y_train.shape,"\t    y_test shape is:",y_test.shape,"\n\n")
  norm = Normalizer()
  #model
  X_train = norm.fit_transform(X_train)
  X_test  = norm.transform(X_test)
  earlyStopping=keras.callbacks.EarlyStopping(monitor='val_loss', patience=0, verbose=0, mode='auto')
  model = KerasClassifier(build_fn=create_model, verbose=0)
  fit_params={'callbacks': [earlyStopping]}
  #grid
#  batch_size =[50,100,200, 300,400]
  epochs = [2,5]
  learn_rate=[0.1,0.001]
  param_grid = dict(  epochs = epochs, learn_rate = learn_rate)
  grid = GridSearchCV(estimator = model, param_grid = param_grid, n_jobs=1)
  #Predicting
  print (np.shape(X_train), np.shape(y_train))
  y_train = np.reshape(y_train, (-1,np.shape(y_train)[1]))
  print ("y_train shape after reshaping", np.shape(y_train))
  grid_result = grid.fit(X_train, y_train, callbacks=[earlyStopping])
  print ("grid score using params: ", grid_result.best_score_, "   ",grid_result.best_params_)
  #scores
  print("SCORES")
  print (grid_result.score(X_test,y_test))
  # summarize results
  #print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
  #means = grid_result.cv_results_['mean_test_score']
  #stds = grid_result.cv_results_['std_test_score']
  #params = grid_result.cv_results_['params']
  #for mean, stdev, param in zip(means, stds, params):
  #    print("%f (%f) with: %r" % (mean, stdev, param))
  print("\n\n")
  print("y_test is",y_test)
  y_hat_test = grid.predict(X_test)
  y_hat_train = grid.predict(X_train)
  print("y_hat_test is ", y_hat_test)
Marcin Możejko
  • 39,542
  • 10
  • 109
  • 120
Akhan
  • 425
  • 1
  • 7
  • 21
  • The classifier is predicting a class probability. If you want the final prediction use: `y_pred.argmax(axis=-1)` – Marcin Możejko Feb 04 '18 at 17:58
  • Sorry I didnt understand your suggestion. **y_test is [[0. 0. 0. 0. 0. 0. 0. 1. 0.] [0. 1. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 1. 0. 0. 0.]]** 'y_hat_test before [2 3 5]' and "after applying axis=-1 thing",y_hat_test after 2 . What does this mean ? – Akhan Feb 04 '18 at 21:24
  • You should only apply it to the prediction from `keras.model`. – Marcin Możejko Feb 04 '18 at 22:27
  • y_hat_test is the prediction value. – Akhan Feb 05 '18 at 22:32
  • Just to add, your suggestion is working on the output of "predictions" made after I saved Model and loaded it. So, the exact story is. 1) Grid SearchCV applied on the Keras model. 2) model fitted on train data. 3) model now making prediction in label Encoded form (that I can decode) 4) Saved keras model in hdf5 format 5) loaded keras Model, now this loaded model making predict proba type output like probability of each class for each sample. – Akhan Feb 06 '18 at 18:33

0 Answers0