image_output_2.png image_output_1.png
from sklearn.metrics import confusion_matrix, classification_report, cohen_kappa_score

from tensorflow.keras.layers import Conv1D, MaxPooling1D, GlobalMaxPooling1D, GlobalAveragePooling1D

from tensorflow.keras.layers import Dense, Input, Activation, Embedding, Dropout

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

plt.rcParams['figure.figsize'] = [12, 5]

train = pd.read_csv('../input/drugsComTrain_raw.csv')
test = pd.read_csv('../input/drugsComTest_raw.csv')

data = pad_sequences(sequences, maxlen=sequence_length)

ratings = train['rating'].values
labels = 1.0 * (ratings >= 8) + 1.0 * (ratings >= 5)

hot_labels = to_categorical(labels)
hot_labels[:3]

VALIDATION_SPLIT = 0.25
N = int(VALIDATION_SPLIT * data.shape[0])
indices = np.arange(data.shape[0])
np.random.shuffle(indices)
data = data[indices]
hot_labels = hot_labels[indices]
train_data = data[:-N]
train_cat = hot_labels[:-N]
val_data = data[-N:]
val_cat = hot_labels[-N:]

embedding_dim = 100
model = Sequential([Embedding(max_features + 1, embedding_dim), Dropout(0.25), Conv1D(128, 7, padding='valid', activation='relu', strides=3), GlobalAveragePooling1D(), Dropout(0.25), Dense(128, activation='relu'), Dense(3, activation='softmax')])
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc'])
model.summary()

acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
e = np.arange(len(acc)) + 1

pred_labels = np.argmax(model.predict(val_data), axis=1)
val_labels = np.argmax(val_cat, axis=1)
cr = classification_report(val_labels, pred_labels)

cm = confusion_matrix(val_labels, pred_labels).T
print(cm)