comvertind pandas数据框时,无法将NumPy数组转换为张量

时间:2020-09-13 17:42:13

标签: python pandas numpy tensorflow

我正在尝试将pandas数据框转换为tf数据集,但我经常遇到此问题:

Traceback (most recent call last):
  File "/home/arch_poppin/dev/AI/reviews/rev.py", line 36, in <module>
    dataset = tf.data.Dataset.from_tensor_slices((df.values, label.values))
  File "/usr/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 689, in from_tensor_slices
    return TensorSliceDataset(tensors)
  File "/usr/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 3066, in __init__
    element = structure.normalize_element(element)
  File "/usr/lib/python3.8/site-packages/tensorflow/python/data/util/structure.py", line 129, in normalize_element
    ops.convert_to_tensor(t, name="component_%d" % i, dtype=dtype))
  File "/usr/lib/python3.8/site-packages/tensorflow/python/profiler/trace.py", line 163, in wrapped
    return func(*args, **kwargs)
  File "/usr/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1535, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/usr/lib/python3.8/site-packages/tensorflow/python/framework/tensor_conversion_registry.py", line 52, in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)
  File "/usr/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 264, in constant
    return _constant_impl(value, dtype, shape, name, verify_shape=False,
  File "/usr/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 276, in _constant_impl
    return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
  File "/usr/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 301, in _constant_eager_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "/usr/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 98, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).

这是我的代码,如下所示:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import io
import tensorflow as tf
from tensorflow import keras
import pandas as pd
import numpy as np


# In[0] open file
df = pd.read_csv(r'PATH')
df = df.sample(frac=1).reset_index(drop=True)


# In[1] make comma separated integers into list

objectColumnList = list(df.select_dtypes(include=['object']).columns)

for column in objectColumnList:
    colArr = []
    for row in df[column]:
        arr = np.asarray(row.split(',')).astype(np.float32)
        colArr.append(arr)
    df[column] = colArr

# In[2] mnake datatset

label = df.pop('MYLABELS')

dataset = tf.data.Dataset.from_tensor_slices((df.values, label.values))

如果要重现该错误,请现在指向我正在使用的csv文件的链接: https://mega.nz/file/uOwiwK5K#FVG7K0glMh2mGa53UDWQiG6iKgNFn5972Kdjb-gmAV4由于隐私原因,我必须删除列名

0 个答案:

没有答案