1
$\begingroup$

I am trying to perform classification task using Keras and tensorflow. However, the learning converges after achieving an accuracy of 57%. All my inputs and outputs are categorical data. Am I using the correct approach to train? Are there any other example in the web which is trying to solve the similar problem?

data.csv

religion,caste,qualification,marital_status,sex,nature_activity
2,20,5,1,1,10
2,20,5,1,1,10
2,20,5,1,1,10
2,20,5,1,1,10
1,3,5,1,1,13
1,4,4,1,2,3
1,3,4,2,1,1
1,3,3,2,1,1

Source code

from keras.models import Sequential
from keras.layers import Dense
import pandas
from sklearn.metrics import classification_report
from sklearn.preprocessing import LabelEncoder
from keras.utils import np_utils
import numpy
from sklearn.model_selection import train_test_split
from keras.wrappers.scikit_learn import KerasClassifier
import sys
import matplotlib.pyplot as plt
# fix random seed for reproducibility
numpy.random.seed(7)

# load pima indians dataset
dataframe = pandas.read_csv("data.csv",header=None)
dataset=dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,0:4].astype(float)
Y = dataset[:,4]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.0015, random_state=42)
encoder = LabelEncoder()
encoder.fit(Y_train)
encoded_Y = encoder.transform(Y_train)
one_hot_enc_Y=np_utils.to_categorical(encoded_Y)
model=Sequential()
model.add(Dense(500,input_dim=4,activation='relu'))
model.add(Dense(100,activation='relu'))
model.add(Dense(50,activation='relu'))
model.add(Dense(3,activation='softmax'))

# Compile model
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
# Fit the model
history=model.fit(X_train, one_hot_enc_Y, validation_split=0.33,epochs=300, batch_size=5)
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
# summarize history for loss
plt.plot(history.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train'], loc='upper right')
plt.show()
# evaluate the model
scores = model.evaluate(X_train, one_hot_enc_Y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
sys.exit()
$\endgroup$
  • 1
    $\begingroup$ Welcome to AI stackexchange...since there are multiple softwares for ML tasks I suggest you explain what the code does and you may get better and more constructive answers $\endgroup$ – DuttaA Feb 22 '18 at 12:03
  • $\begingroup$ Question is clearly tagged with the framework and dependencies that I am using(Keras,Tensorflow). But for your understanding, I am trying to train my model to solve multi-class classification problem. See the reference machinelearningmastery.com/…. The only exception is that all my inputs are categorical data. $\endgroup$ – Madhav Bhattarai Feb 23 '18 at 5:00
  • 1
    $\begingroup$ Just suggesting this community is not so big or diverse like stackoverflow $\endgroup$ – DuttaA Feb 23 '18 at 6:16
1
$\begingroup$

You might find "What should I do when my neural network doesn't learn?" and "Reasons why your Neural Network is not working" interesting.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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