#๐Ÿ”’ KeyCode Error with Predictive Algorithm

26 messages ยท Page 1 of 1 (latest)

potent coveBOT
#

@keen dragon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

coarse kelp
#

this kaggle's titanic?
I remember their features having capitalized characters, make sure to match that

keen dragon
#

well I have y set to a column in my dataset, the goal is to use a predictive algorithm (kmeans) and compare it to a dataset that has the correct answers, if the predictive algorithm is correct, it gets assigned a 1, if not then it stays at 0

keen dragon
#

but the kaggle website is missing a lot of information, ill try capitalizing

coarse kelp
#

check the df.head() df.columns, etc to see the column names
or even just read the .csv yourself, it's a very human readable format

keen dragon
#

well it runs fine but when i add that loop at the end it says error

#

and my csv is no caps

coarse kelp
keen dragon
#

sorry program im using only shows small sliver of error message at a time

#

bottom one is probably more useful

#

it just doesnt like the prediction line

coarse kelp
#

I think it's here

prediction = kmeans.predict(X)
pred_df = pd.DataFrame({'actual': y, 'prediction': prediction})
print(pred_df)
for i in range(len(y+1)):
    if prediction[i] == y[i]:  # <--
        correct += 1
```you can't access a `df` using index like that, you use `.iloc`
#
>>> import pandas as pd, seaborn as sns
>>> df = sns.load_dataset('iris')  # just some mock dataset
>>> df[0]
Traceback (most recent call last):
  File "C:\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc
    return self._engine.get_loc(casted_key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc
  File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\\_libs\\hashtable_class_helper.pxi", line 7081, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\\_libs\\hashtable_class_helper.pxi", line 7089, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python311\Lib\site-packages\pandas\core\frame.py", line 4090, in __getitem__
    indexer = self.columns.get_loc(key)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc
    raise KeyError(key) from err
KeyError: 0
>>> df.iloc[0]
sepal_length       5.1
sepal_width        3.5
petal_length       1.4
petal_width        0.2
species         setosa
Name: 0, dtype: object
>>>
keen dragon
#

so would i change the y to ship.iloc?

coarse kelp
keen dragon
#

gotcha

#

ive never used python before so this really hurts my brain

#

and theres no recent resources for this

coarse kelp
coarse kelp
#

here's the indexing section of the user guide, on the part about indexing by position

keen dragon
#

gotcha, thanks homie

potent coveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ KeyCode Error with Predictive Algorithm