Sep-17-2022, 03:36 PM
(This post was last modified: Sep-17-2022, 03:36 PM by Led_Zeppelin.
Edit Reason: fixed error tags
)
The following Python code generates an error:
q = image_data_org.loc[image_data_org.loc['machine_status']==0]['time_period'] p = image_data_org.loc[image_data_org.loc['machine_status']==1]['time_period'][:q.shape[0]] pq = np.sum(p * np.log(p/q)) qp = np.sum(q * np.log(q/p)) print('KL(P || Q) : %. pq)%.3f' % pq) print('KL(Q || P) : %. pq)%.3f' % qp)The error is:
Error:---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Input In [12], in <cell line: 1>()
----> 1 q = image_data_org.loc[image_data_org.loc['machine_status']==0]['time_period']
2 p = image_data_org.loc[image_data_org.loc['machine_status']==1]['time_period'][:q.shape[0]]
4 pq = np.sum(p * np.log(p/q))
File ~\miniconda3\envs\pump-failure-pred\lib\site-packages\pandas\core\indexing.py:967, in _LocationIndexer.__getitem__(self, key)
964 axis = self.axis or 0
966 maybe_callable = com.apply_if_callable(key, self.obj)
--> 967 return self._getitem_axis(maybe_callable, axis=axis)
File ~\miniconda3\envs\pump-failure-pred\lib\site-packages\pandas\core\indexing.py:1202, in _LocIndexer._getitem_axis(self, key, axis)
1200 # fall thru to straight lookup
1201 self._validate_key(key, axis)
-> 1202 return self._get_label(key, axis=axis)
File ~\miniconda3\envs\pump-failure-pred\lib\site-packages\pandas\core\indexing.py:1153, in _LocIndexer._get_label(self, label, axis)
1151 def _get_label(self, label, axis: int):
1152 # GH#5667 this will fail if the label is not present in the axis.
-> 1153 return self.obj.xs(label, axis=axis)
File ~\miniconda3\envs\pump-failure-pred\lib\site-packages\pandas\core\generic.py:3864, in NDFrame.xs(self, key, axis, level, drop_level)
3862 new_index = index[loc]
3863 else:
-> 3864 loc = index.get_loc(key)
3866 if isinstance(loc, np.ndarray):
3867 if loc.dtype == np.bool_:
File ~\miniconda3\envs\pump-failure-pred\lib\site-packages\pandas\core\indexes\range.py:389, in RangeIndex.get_loc(self, key, method, tolerance)
387 raise KeyError(key) from err
388 self._check_indexing_error(key)
--> 389 raise KeyError(key)
390 return super().get_loc(key, method=method, tolerance=tolerance)
KeyError: 'machine_status'