【问题标题】:How to plot histogram against class label for TF.Dataset如何针对 TF.Dataset 的类标签绘制直方图
【发布时间】:2021-04-12 02:54:02
【问题描述】:

我正在使用TF.CsvDataset 从磁盘加载数据。并将数据绘制为

#This is the transformation function applied on loaded data before displaying histogram.
def preprocess(*fields):
    print(len(fields))
    features=tf.stack(fields[:-1])
    labels=tf.stack([int(x) for x in fields[-1:]])
    return features,labels  # x, y

for features,label in train_ds.take(1000):
#  print(features[0])
plt.hist(features.numpy().flatten(), bins = 101)

我得到了这个直方图

但我想根据二进制类标签绘制 712 个特征值的分布。即class label为0时,feature 1,2 or 3的取值是多少。

如何用 pyplot 做到这一点?

我已阅读以下主题,但没有任何帮助。

Plotting histograms against classes in pandas / matplotlib

Histogram color by class

How to draw an histogram with multiple categories in python

【问题讨论】:

标签: python tensorflow matplotlib machine-learning histogram


【解决方案1】:

您可以使用np.fromiter 并获取所有标签。然后你只需将标签列表传递给plt.hist

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

train, test = tf.keras.datasets.mnist.load_data()

ds = tf.data.Dataset.from_tensor_slices(train)

vals = np.fromiter(ds.map(lambda x, y: y), float)

plt.hist(vals)
plt.xticks(range(10))
plt.title('Label Frequency')
plt.show()

【讨论】:

猜你喜欢
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 2015-08-26
  • 2017-09-16
  • 1970-01-01
  • 2020-10-21
相关资源
最近更新 更多