如何为其他 ml 模型转换 tf.data.dataset [英] How to convert a tf.data.dataset for other ml models

查看:33
本文介绍了如何为其他 ml 模型转换 tf.data.dataset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经解决了 tf.data.dataset 的两个问题:

I've tackled 2 issues with tf.data.dataset:

我想知道是否可以将 tf.data.dataset 转换为 Pandas 数据框?

I wonder if it's possible to convert a tf.data.dataset into pandas dataframe?

另外我想知道我是否可以将 tf.data.dataset 与其他模型(tf 神经网络除外)一起使用,例如我可以使用 tf.data.dataset 来训练 xgboost、LGBMClassifier、RandomForest 分类器等.

Also i wonder if i can use a tf.data.dataset with other models (except tf neural network) so for example i can take a tf.data.dataset for training xgboost, LGBMClassifier, RandomForest classifier etc.

你能帮忙吗?

推荐答案

您可以使用 tfds.as_dataframe:

You can use tfds.as_dataframe:

import tensorflow_datasets as tfds
import pandas as pd

iris_dataset, info = tfds.load('iris', with_info=True, split='train')

df = tfds.as_dataframe(iris_dataset, info)

df[['feature1','feature2', 'feature3', 'feature4']] = \
    pd.DataFrame(df['features'].tolist(), index= df.index)

df = df.drop('features', axis=1)

df.head()

   label  feature1  feature2  feature3  feature4
0      0       5.1       3.4       1.5       0.2
1      2       7.7       3.0       6.1       2.3
2      1       5.7       2.8       4.5       1.3
3      2       6.8       3.2       5.9       2.3
4      0       5.2       3.4       1.4       0.2

或者更一般地说,只需将数据集转换为 NumPy 数组即可与其他库一起使用.

Or more generally, just turn the dataset into a NumPy array to use it with another library.

这篇关于如何为其他 ml 模型转换 tf.data.dataset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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