Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: 'int' object is not subscriptable #14

Open
ruimmpires opened this issue Mar 13, 2019 · 1 comment
Open

TypeError: 'int' object is not subscriptable #14

ruimmpires opened this issue Mar 13, 2019 · 1 comment

Comments

@ruimmpires
Copy link

the code works fine!
However I am a newbie and tried to make editing the training data a bit more easy so I've created a file training_data.data as:
Green,3,Apple
Yellow,3,Apple
Red,1,Grape
Red,1,Grape
Yellow,3,Lemon
and then import this with:
import pandas as pd
training_data = pd.read_csv('training_data.data', header=-1)

and now I gut the error TypeError: 'int' object is not subscriptable.

@ruimmpires
Copy link
Author

also tried to force the dimension to be integer, but now get many weird errors:
training_data = pd.read_csv('training_data.data', dtype={"diameter": np.int32})
header = training_data.columns

The training_data.data is now:
"color","diameter","label"
'Green',3,'Apple'
'Yellow',3,Apple'
'Red',1,'Grape'
'Red',1,'Grape'
'Yellow',3,'Lemon'

Traceback (most recent call last):
File "/home/rpires/.local/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2656, in get_loc
return self._engine.get_loc(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: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "decision_tree_withCSVdata.py", line 410, in
my_tree = build_tree(training_data)
File "decision_tree_withCSVdata.py", line 318, in build_tree
gain, question = find_best_split(rows)
File "decision_tree_withCSVdata.py", line 247, in find_best_split
n_features = len(rows[0]) - 1 # number of columns
File "/home/rpires/.local/lib/python3.7/site-packages/pandas/core/frame.py", line 2927, in getitem
indexer = self.columns.get_loc(key)
File "/home/rpires/.local/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2658, 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: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant