title: Keras Text Classification Custom Dataset from csv
body: <p>I'm trying to build a Keras model to classify different articles into topics. Each article only has one topic. I have a custom csv file with the following structure:</p>
  
  <pre><code>"topic1","article1"
  "topic2","article2"
  </code></pre>
  
  <p>I'm trying to train my model for this data set, but unfortunately I get an error because the data from the csv was not yet processed to be a vector. </p>
  
  <p>This is my code:</p>
  
  <pre><code>from __future__ import print_function
  import csv
  import numpy as np
  import keras
  import os
  from keras.models import Sequential
  from keras.layers import Dense, Dropout, Activation
  from keras.preprocessing.text import Tokenizer
  
  max_words = 1000
  batch_size = 32
  epochs = 5
  model_file_name = 'model.h5'
  
  
  def load_data(word_max, test_split):
      xs = []
      labels = []
      counter = 0
      with open('data.csv', 'r') as f:
          reader = csv.reader(f)
          for line in reader:
              if counter &gt; word_max:
                  break
              xs.append(line[1])
              labels.append(line[0])
              counter += 1
      idx = int(len(xs) * (1 - test_split))
      train_x, train_y = np.array(xs[:idx]), np.array(labels[:idx])
      test_x, test_y = np.array(xs[idx:]), np.array(labels[idx:])
      return (train_x, train_y), (test_x, test_y)
  
  
  print('Loading data...')
  (x_train, y_train), (x_test, y_test) = load_data(max_words, 0.3)
  
  print(len(x_train), 'train sequences')
  print(len(x_test), 'test sequences')
  
  num_classes = np.max(y_train) + 1
  print(num_classes, 'classes')
  
  print('Vectorizing sequence data...')
  tokenizer = Tokenizer(num_words=max_words)
  x_train = tokenizer.sequences_to_matrix(x_train, mode='binary')
  x_test = tokenizer.sequences_to_matrix(x_test, mode='binary')
  print('x_train shape:', x_train.shape)
  print('x_test shape:', x_test.shape)
  
  print('Convert class vector to binary class matrix '
        '(for use with categorical_crossentropy)')
  y_train = keras.utils.to_categorical(y_train, num_classes)
  y_test = keras.utils.to_categorical(y_test, num_classes)
  print('y_train shape:', y_train.shape)
  print('y_test shape:', y_test.shape)
  
  if os.path.isfile(model_file_name):
      model = keras.models.load_model(model_file_name)
  else:
      print('Building model...')
      model = Sequential()
      model.add(Dense(512, input_shape=(max_words,)))
      model.add(Activation('relu'))
      model.add(Dropout(0.35))
      model.add(Dense(num_classes))
      model.add(Activation('softmax'))
  
      model.compile(loss='categorical_crossentropy',
                    optimizer='adam',
                    metrics=['accuracy'])
  
      history = model.fit(x_train, y_train,
                          batch_size=batch_size,
                          epochs=epochs,
                          verbose=1,
                          validation_split=0.1)
      score = model.evaluate(x_test, y_test,
                             batch_size=batch_size, verbose=1)
      print('Test score:', score[0])
      print('Test accuracy:', score[1])
      model.save(model_file_name)
  </code></pre>
  
  <p>How can I load my data correctly to train my model? And how would I predict a topic for a given text with <code>model.predict</code>?</p>
  
  <p>EDIT:
  I've got the training of the model working by changing the process of loading the training data like:</p>
  
  <pre><code> print('Loading data...')
  (x_train, y_train), (x_test, y_test) = load_data(max_words, 0.3)
  
  tokenizer = Tokenizer(num_words=max_words)
  tokenizer.fit_on_texts(x_train)
  x_train = tokenizer.texts_to_sequences(x_train)
  tokenizer.fit_on_texts(y_train)
  y_train = tokenizer.texts_to_sequences(y_train)
  tokenizer.fit_on_texts(x_test)
  x_test = tokenizer.texts_to_sequences(x_test)
  tokenizer.fit_on_texts(y_test)
  y_test = tokenizer.texts_to_sequences(y_test)
  
  print(len(x_train), 'train sequences')
  print(len(x_test), 'test sequences')
  </code></pre>
  
  <p>So, how can I use this and predict a label from a given article?:</p>
  
  <pre><code>model = keras.models.load_model(model_file_name)
  to_predict = np.array(['The sun is shining.'])
  # Predict label from example article
  label = model.predict(...)
  print(label)
  </code></pre>