0
votes

I have reviewed threads for this particular subject, but none of the solutions are applicable to my situation and I need help with this, please. I am trying to prep my data for training using neural language model. My data has already been pre-processed and stored in text file. The sample data is as follows: ["restaurant", "paris", "district", "gunman", "held", "hostage"...] and the array shape is (10848135,), as per array shape attribute. However I get an index error message when I try to slice the data with statement X, y = sequences[:,:-1], sequences[:,-1]

The code is as follows:

def encode_words(self, dataset):
        data = dataset.split('\n')
        tokenizer = Tokenizer()
        tokenizer.fit_on_texts(dataset)
        sequences = tokenizer.texts_to_sequences(dataset)
        vocab_size = len(tokenizer.word_index) + 1
        sequences = array(sequences)
        X, y = sequences[:,:-1], sequences[:,-1]
        y = to_categorical(y, num_classes=vocab_size)
        seq_length = X.shape[1]
        return X, y, vocab_size, seq_length, tokenizer

I understand what the error is, but don't know how to fix this. Please I need assistance to get the code working. Below is the full error message:

Traceback (most recent call last):

   File "/home/asifa/anaconda3/deep_learning_project/processor.py", line 15, in <module>
    X,y,vocab_size,seq_length,tokenizer = emb.encode_words(seq_data)

  File "/home/asifa/anaconda3/deep_learning_project/WordEmbedding.py", line 67, in encode_words
    X, y = sequences[:,:-1], sequences[:,-1]

IndexError: too many indices for array
2
It looks like your array sequences only has a single axis. You'll need to reshape it to have two axes. - David Hoffman
What's the dtype of sequences? - hpaulj
@hpaulj, sequences is an object dtype - Eddy
You need to examine sequences in more detail. 1d object dtype is probably an array of lists or arrays - and those lists vary in size. Until you understand the exact nature of that array, you can't reshape or slice in a meaningful way. - hpaulj
@hpaulj, I've manged to reshape and sliced sequences with (2, 104309) as the output shape - although I'm not sure if the new shape and slice are correct. However this line of code ``` y = to_categorical(y, num_classes=vocab_size) ``` throws the following error message: File "/home/asifa/anaconda3/envs/researchProject/lib/python3.6/site-packages/keras/utils/np_utils.py", line 25, in to_categorical y = np.array(y, dtype='int') ValueError: setting an array element with a sequence. Pls any idea how I can fix this ? - Eddy

2 Answers

0
votes

The array you have is 1D and your are trying to access it as 2D. check why is is 1D in the first place if you expecting a 2D.

0
votes

You have 1-d array and is trying to slice in two dimensions. Thats why you are getting this error.

Your array shape is (10848135,), this means that you have only 1 dimension. If you are expecting to have a second dimension from where you need to extract your y array, you need to review your file because it has 1d array. Therefore, you can't access like this.