I am a newbie on python. I am trying to check the results of a test and train and I have to compare my predictions with the actual test results (data_train). Data_train is a dictionary as shown in the image below . The prediction is an array like this
The code aims to counts the consistent classifications between prediction and test results.
consistent=0
inconsistent=0
for i in np.linspace(1,len_test,len_test):
if data_train['class'][i] == predictions[i]:
consistent=consistent+1
else:
inconsistent=inconsistent+1
I have this error, what does it means? I don't know how to catch error like this one in python
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\anaconda4\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
~\anaconda4\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
~\anaconda4\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 1
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10076/1764309407.py in <module>
1 for i in np.linspace(1,len_test,len_test):
----> 2 a=data_train['class'][i]
3 b=predictions[i]
4 if a == b:
5 consistent=consistent+1
~\anaconda4\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
940
941 elif key_is_scalar:
--> 942 return self._get_value(key)
943
944 if is_hashable(key):
~\anaconda4\lib\site-packages\pandas\core\series.py in _get_value(self, label, takeable)
1049
1050 # Similar to Index.get_value, but we do not fall back to positional
-> 1051 loc = self.index.get_loc(label)
1052 return self.index._get_values_for_loc(self, loc, label)
1053
~\anaconda4\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:
KeyError: 1.0
I'm not entirely sure of the code's context, but it seems you're using NumPy. As I understand it, referencing an array element can only be done using an integer (or slices, ellipses etc.), but not strings. You appear to be attempting to reference using the string 'class' and I don't understand why. My suggestion would be to remove the ['class'] reference and simply use the integer reference, such as: a=data_train[i]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.