Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

python - How to convert a tf.data.dataset for other ml models

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

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

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.

can you assist?

question from:https://stackoverflow.com/questions/65915295/how-to-convert-a-tf-data-dataset-for-other-ml-models

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...