Home > front end >  My Neural network is perfroming regression instead of classification
My Neural network is perfroming regression instead of classification

Time:08-30

I am working on a Data Science project which requires implementation of a neural network. The dataset I am providing for training is not sequential and has class labels. But I don't know why it is treating it as sequence of events.

I am using the following code for the model:

model = keras.Sequential([
    layers.Dense(100, activation='relu'),
    layers.Dropout(0.4),
    layers.Dense(100, activation='relu'),
    layers.Dropout(0.4),
    layers.Dense(100, activation='relu'),
    layers.Dropout(0.4),
    layers.Dense(100, activation='relu'),
    layers.Dense(6, activation='softmax')
])
model.compile(
    optimizer="adam",
    loss="sparse_categorical_crossentropy",
    metrics=['accuracy']
)
model.fit(X_train, y_train,epochs=100,batch_size=2100)
y_pred=model.predict(X_test)
print(accuracy_score(y_test,y_pred))

The accuracy_score function is giving this error

Classification metrics can't handle a mix of multiclass and continuous-multioutput targets

This error messages shows when the algorithm is performing the regression. How can I solve this issue?

Edit 1

As suggested by Michael Hodel I have applied the OneHotEncoder instead of using LabelEncoder.

data['label']= ohc.fit_transform(data[['label']])

It is giving me error

sparse matrix length is ambiguous; use getnnz() or shape[0]

Edit 2

I used pd.get_dummies() instead of 'OneHotEncoder`

CodePudding user response:

Your y_pred are floats, but accuracy_score expects integers. I'd recommend you one-hot encode your labels and use the categorical_crossentropy loss function. Then y_pred represent the class probabilities which can simply converted to predictions via y_pred.argmax(axis=1). An example with mock data:

from sklearn.metrics import accuracy_score
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
import numpy as np

n_classes = 6
n_variables = 100
n_examples = 1000
X = np.random.normal(0, 1, (n_examples, n_variables))
y_ = np.random.choice(n_classes, size=n_examples)
y = np.eye(n_classes)[y_]

idx = int(n_examples * 0.8)
X_train, y_train = X[:idx], y[:idx]
X_test, y_test = X[idx:], y[idx:]

model = keras.Sequential([
    layers.Dense(100, activation='relu'),
    layers.Dropout(0.4),
    layers.Dense(100, activation='relu'),
    layers.Dropout(0.4),
    layers.Dense(100, activation='relu'),
    layers.Dropout(0.4),
    layers.Dense(100, activation='relu'),
    layers.Dense(6, activation='softmax')
])
model.compile(
    optimizer="adam",
    loss="categorical_crossentropy",
    metrics=['accuracy']
)
model.fit(X_train, y_train,epochs=10,batch_size=100)
y_pred=model.predict(X_test)
print(accuracy_score(y_test.argmax(axis=1), y_pred.argmax(axis=1)))

Here y_ are your classes of shape (n_examples,), and y are the one-hot encoded classes of shape (n_examples, n_classes) and model.predict(X_test) gives predicted probabilities of shape (n_examples, n_classes) and model.predict(X_test).argmax(axis=1) gives prediced classes of shape (n_examples,).

  • Related