Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding Eror
#1
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'

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
Coding Eror - by Led_Zeppelin - Sep-17-2022, 03:36 PM
RE: Coding Eror - by deanhystad - Sep-17-2022, 04:23 PM
RE: Coding Eror - by Led_Zeppelin - Sep-18-2022, 10:03 PM
RE: Coding Eror - by deanhystad - Sep-19-2022, 03:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  invalid syntax eror Larry 3 4,176 Feb-18-2018, 05:56 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020