简体   繁体   English

如何针对 TF.Dataset 的 class label 的 plot 直方图

[英]How to plot histogram against class label for TF.Dataset

I'm loading data from the disk with TF.CsvDataset .我正在使用TF.CsvDataset从磁盘加载数据。 And plotting the data as并将数据绘制为

#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)

And I'm getting this histogram我得到这个直方图

在此处输入图像描述

But I want to plot distribution of 712 features' values against binary class labels.但我想根据二进制 class 标签对 712 个特征值进行 plot 分布。 That is, what is the value of feature 1,2 or 3 when class label is 0.也就是说,当 class label 为 0 时,特征 1,2 或 3 的值是多少。

How to do that with pyplot?如何用 pyplot 做到这一点?

I have read following threads but, nothing helped.我已阅读以下主题,但没有任何帮助。

Plotting histograms against classes in pandas / matplotlib 针对 pandas / matplotlib 中的类绘制直方图

Histogram color by class class 的直方图颜色

How to draw an histogram with multiple categories in python 如何在 python 中绘制具有多个类别的直方图

You can use np.fromiter and get all the labels.您可以使用np.fromiter并获取所有标签。 Then you simply pass the list of labels to plt.hist :然后您只需将标签列表传递给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()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM