0

So I am trying to make a machine learning model for recognizing hand signs. I am pretty new to machine learning and numpy. I am facing the following error

pixel0  pixel1  pixel2  pixel3  ...  pixel9212  pixel9213  pixel9214  

pixel9215
0     255     255     255     255  ...        255        255        255        255
0     255     255     255     255  ...        255        255        255        255
0     255     255     255     255  ...        255        255        255        255
0     255     255     255     255  ...        255        255        255        255
0     255     255     255     255  ...        255        255        255        255

[5 rows x 9216 columns]
Traceback (most recent call last):
  File "classification.py", line 12, in <module>
    y = np.array(train.pop('label'))
  File "/home/bing/.local/lib/python2.7/site-packages/pandas/core/generic.py", line 809, in pop
    result = self[item]
  File "/home/bing/.local/lib/python2.7/site-packages/pandas/core/frame.py", line 2927, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/home/bing/.local/lib/python2.7/site-packages/pandas/core/indexes/base.py", line 2659, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'label'

This is my classification.py

 import pandas as pd
 import numpy as np

 train = pd.read_csv("train60.csv")
 print(train.head())
 y = np.array(train.pop('label'))  # this is the error

I have posted the full trackback message Help would be appreciated.

3
  • Yes that is the error (Line 12). What is the output of line 11 ... print(train.head())? Will you paste it? Commented Jul 28, 2019 at 19:00
  • @Amit Sure I am pasting the whole output.. Commented Jul 28, 2019 at 19:11
  • For the next time, keep in mind that your posted code should be minimal, and code that comes after the error is unnecessary and just creates clutter (see edited code). Commented Jul 28, 2019 at 19:45

2 Answers 2

1

Most likely your train df does not have a column called 'label'. You can check for it by a simple addition to your script.

if 'label' in train.columns:
    print("label column is present")
else:
    print("label column is not here. Popping 'label' will produce KeyError")

Also do you want to remove 'label' from the train df when you were using pop function? I am saying this because df.pop('MyColumnName') will return the 'MyColumnName' and drop it from the df. May be this is exactly what you want but I thought you should know. Hope this helped.

Sign up to request clarification or add additional context in comments.

Comments

0

Taking a look at the documentation: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pop.html

Calling pop on a pandas DataFrame raises a KeyError if the column name you passed in was not found. This appears to be what's going on, but I can't tell without looking at your exact DataFrame. Double check to verify that the DataFrame train contains a column named "label."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.