python - KeyError: 'C85' when mapping a string list to numeric list

Viewed 117

I'm new with python and trying to write RNN program using the Kaggle Titanic data. https://www.kaggle.com/c/titanic/data

I keep getting an uninformative error keyError: 'C85' when trying to execute the loop -

for each in feature_sets:
    print(each)
    feature_sets_ints.append([vocab_to_int[word] for word in each.split()])

(encode the words in the data set)

and the print output is (with the error)

3 Braund, Mr. Owen Harris male 22.0 A/5 21171 7.25 nan S
3 Braund, Mr. Owen Harris male 22.0 A/5 21171 7.25 nan S
1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38.0 PC 17599 71.2833 C85 C
Traceback (most recent call last):
  File "C:\Users\kfire\Desktop\NLP & Deep Leaning\CNN\RNN.py", line 53, in <module>
    feature_sets_ints.append([vocab_to_int[word] for word in each.split()])
  File "C:\Users\kfire\Desktop\NLP & Deep Leaning\CNN\RNN.py", line 53, in <listcomp>
    feature_sets_ints.append([vocab_to_int[word] for word in each.split()])
KeyError: 'C85'

Process finished with exit code 1

EDIT: here is a sample of my dataset:

data set sample

as you can see, there are nan values in the data set but I don't think its what causing the problem.

any ideas of how to fix it? or at least can someone please explain me the error?

below I only added the code that works (the last line of code I've brought is the one that throws an error) and a screen shoot of the error message. and the error messasge

import numpy as np
import tensorflow as tf
import pandas as pd
from collections import Counter


feature_sets_train = pd.read_csv('train.csv')
# TODO: Use both datasets to make the embeddings (vocab_to_int map)
feature_sets_test = pd.read_csv('test.csv')
feature_sets_train_tests = pd.concat([feature_sets_train, feature_sets_test])
feature_sets = feature_sets_train

passengers = [' '.join(map(str,passenger[[2,3,4,5,8,9,10,11]])) for passenger in feature_sets.values]
passengers_test = [' '.join(map(str,passenger[[1,2,3,4,7,8,9,10]])) for passenger in feature_sets_test.values]

survived = [passenger[1] for passenger in feature_sets.values]
feature_sets = passengers
feature_sets_test = passengers_test
labels =  survived

passengers = [' '.join(map(str,passenger[[0,1,2,3,4,5,7,8,9,11]])) for passenger in feature_sets_train_tests.values]

all_text = ' '.join(passengers)
words = all_text.split()

counts = Counter(words)
vocab = sorted(counts, key=counts.get, reverse=True)
vocab_to_int = {word: ii for ii, word in enumerate(vocab, 1)}

feature_sets_ints = []
feature_sets_ints_test = []
print(feature_sets[0])
for each in feature_sets:
    feature_sets_ints.append([vocab_to_int[word] for word in each.split()])
1 Answers

Add print for debugging purposes:

for each in feature_sets:
    print (each)
    feature_sets_ints.append([vocab_to_int[word] for word in each.split()]

This will show you the feature_set that it gets stuck on.

Related