# class AddObservedValuesIndicator(SimpleTransformation): # """ # Replaces missing values in a numpy array (NaNs) with a dummy value and adds # an "observed"-indicator that is ``1`` when values are observed and ``0`` # when values are missing. # # # Parameters # ---------- # target_field # Field for which missing values will be replaced # output_field # Field name to use for the indicator # dummy_value # Value to use for replacing missing values. # convert_nans # If set to true (default) missing values will be replaced. Otherwise # they will not be replaced. In any case the indicator is included in the # result. # """ # # @validated() # def __init__( # self, # target_field: str, # output_field: str, # dummy_value: int = 0, # convert_nans: bool = True, # dtype: DType = np.float32, # ) -> None: # self.dummy_value = dummy_value # self.target_field = target_field # self.output_field = output_field # self.convert_nans = convert_nans # self.dtype = dtype # # def transform(self, data: DataEntry) -> DataEntry: # value = data[self.target_field] # nan_indices = np.where(np.isnan(value)) # nan_entries = np.isnan(value) # # if self.convert_nans: # value[nan_indices] = self.dummy_value # # data[self.target_field] = value # # Invert bool array so that missing values are zeros and store as float # data[self.output_field] = np.invert(nan_entries).astype(self.dtype) # return data #知识点: #1.AddObservedValuesIndicator功能:检查观测值是否缺失,若用一个变量记录是否缺失; #2.掌握np.isnan,np.where,np.invert的用法; #3.DataEntry提示数据类型,无实际具体消耗。 from gluonts.transform import AddObservedValuesIndicator import numpy as np if __name__ == '__main__': data={'target':np.array([1,2,3,np.nan])} aovi=AddObservedValuesIndicator('target','output' print(aovi.transform(data)) #{'target': array([1., 2., 3., 0.]), 'output': array([1., 1., 1., 0.], dtype=float32)}
理解gluonts的AddObservedValuesIndicator的用法
最新推荐文章于 2022-10-12 10:26:15 发布