如何针对TF.Dataset的类标签绘制直方图 [英] How to plot histogram against class label for TF.Dataset

查看:55
本文介绍了如何针对TF.Dataset的类标签绘制直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

但是我想针对二进制类标签绘制712个要素的值分布.也就是说,当类标签为0时,功能1,2或3的值是什么.

如何使用pyplot做到这一点?

我已阅读以下主题,但无济于事.

I'm loading data from the disk with 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. That is, what is the value of feature 1,2 or 3 when class label is 0.

How to do that with pyplot?

I have read following threads but, nothing helped.

Plotting histograms against classes in pandas / matplotlib

Histogram color by class

How to draw an histogram with multiple categories in python

解决方案

You can use np.fromiter and get all the labels. Then you simply pass the list of labels to 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()

这篇关于如何针对TF.Dataset的类标签绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆