【问题标题】:How to extract data from mapdataset tensorflow in python如何在python中从mapdataset tensorflow中提取数据
【发布时间】:2023-01-07 03:10:02
【问题描述】:

我尝试通过将数据集划分为时间窗口来进行数据准备。用户定义函数是通过以下代码创建的。

class WindowGenerator_with_nan():
    def __init__(self, input_width, label_width, shift, x_iter,
           train_df=cluster_concat_train_df, val_df=cluster_concat_val_df, 
           test_df=cluster_concat_test_df,
           label_columns=None):
     # Store the raw data.
     self.train_df = cluster_concat_train_df[x_iter]
     self.val_df = cluster_concat_val_df[x_iter]
     self.test_df = cluster_concat_test_df[x_iter]

     # Work out the label column indices.
     self.label_columns = label_columns
     if label_columns is not None:
     self.label_columns_indices = {name: i for i, name in
                                enumerate(label_columns)}
     self.column_indices = {name: i for i, name in
                       enumerate(train_df[x_iter].columns)}

     # Work out the window parameters.
     self.input_width = input_width
     self.label_width = label_width
     self.shift = shift

     self.total_window_size = input_width + shift

     self.input_slice = slice(0, input_width)
     self.input_indices = np.arange(self.total_window_size)[self.input_slice]

     self.label_start = self.total_window_size - self.label_width
     self.labels_slice = slice(self.label_start, None)
     self.label_indices = np.arange(self.total_window_size)[self.labels_slice]

 def __repr__(self):
    return '\n'.join([
    f'Total window size: {self.total_window_size}',
    f'Input indices: {self.input_indices}',
    f'Label indices: {self.label_indices}',
    f'Label column name(s): {self.label_columns}'])

在我的例子中,重复的“i”代表簇号。但是,在窗口生成器之后包含一些“NaN”值作为“mapdataset”类型

在我做之后:`

wide_window_with_nan =[WindowGenerator(input_width=96, label_width=1, shift=1, label_columns = ['Labels'], x_iter = i) for i in range(len(df_without_impulate_before_RNN))]

这行代码的结果:print(wide_window_with_nan[0].train)

<MapDataset element_spec=(TensorSpec(shape=(None, 96, 112), dtype=tf.float32, name=None), TensorSpec(shape=(None, 1, 1), dtype=tf.float32, name=None))>

我的问题是如何从地图数据中删除所有包含“NaN”值的窗口,因为我必须使用此数据集作为预测模型的输入。预测模型不允许输入 NaN 值。

附言我使用 google colab pro 作为我的 IDE。因此,我必须在执行期间关注计算资源,例如 RAM 空间。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    您可以使用 tf.data.Dataset 中的 filter 方法删除 NaN 值

    y = tf.data.Dataset.from_tensor_slices([np.nan, 0, 1, 2, 3])
    filter_nan = lambda y: not tf.math.is_nan(y)
    ds = y.filter(filter_nan)
    list(ds.as_numpy_iterator())
    >>>[0.0, 1.0, 2.0, 3.0]
    

    谢谢你。

    【讨论】:

      猜你喜欢
      • 2019-10-07
      • 2021-04-28
      • 1970-01-01
      • 2020-07-31
      • 2014-11-07
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多