【问题标题】:create a input dataset for multiple types of data in a single csv在单个 csv 中为多种类型的数据创建输入数据集
【发布时间】:2021-02-07 22:19:49
【问题描述】:

我想为输入到 TF 创建一个输入数据集,但被卡住了。我有一个各种输入类型(浮点和字符串)的 csv 文件。我想为 tensorflow 创建一个训练文件。有什么方法可以使用 pandas 或 TF 函数来输入这两种类型的数据

`df = pd.read_csv("C:\TRAININGDATA\DATABASE.csv")
Labeldata = pd.DataFrame(df, columns= ['ASSEMBLY_NAME','x','x','x','x','x','x','x','x','x','x','x'])
#REMOVE DOWN IF YOU WANT ALL DATA IN CSV
#print (Labeldata)
# PRINT CSV DATA FOR THE SPECIFIC ASSEMBLY
ASSEMBLYNAME = 'scene0011_00_vh_clean_2'
labels = df.loc[df['ASSEMBLY_NAME'] == ASSEMBLYNAME]
##### Importing into tensorflow
#df['labels'] = df['ASSET NAME'].astype(float)
dataset = tf.data.Dataset.from_tensor_slices((xygrgb , labels))
print(dataset)

` 这是一个数据样本

如您所见,第二列和最后一列是字符串,其他是浮点值。我想输入这种类型的数据作为输入数据集。非常感谢任何帮助。

编辑*** 对于以下代码

dtypes = {'ASSEMBLY_NAME': np.int64, 'ASSETID': 'float'}

df = pd.read_csv("C:\TRAININGDATA\DATABASE.csv",dtype = dtypes)
print(df.dtypes)

我收到TypeError: Cannot cast array data from dtype('O') to dtype('int64') according to the rule 'safe'的错误

对于下面的代码

df = pd.read_csv("C:\TRAININGDATA\DATABASE.csv")
df['ASSEMBLY_NAME'] = df['ASSEMBLY_NAME'].astype(float)
print(df.dtypes)

我收到ValueError: could not convert string to float: 'scene0002_00_vh_clean_2'的错误

以下代码

df = pd.read_csv("C:\TRAININGDATA\DATABASE.csv")
    df['ASSEMBLY_NAME'] = df['ASSEMBLY_NAME'].astype('string')
    print(df.dtypes)

它们被转换为字符串,但浮点值没有被转换为张量

 ASSEMBLY_NAME     string
AssetID           string
Asset Name        string
OffsetX          float32
OffsetY          float32
OffsetZ          float32
Matrix a1        float32
Matrix b1        float32
Matrix c1        float32
Matrix a2        float32
Matrix b2        float32
Matrix c2        float32
Matrix a3        float32
Matrix b3        float32
Matrix c3        float32
KeyPARAM          string
dtype: object

我收到了错误

ValueError: Dimensions 7 and 10 are not compatible

【问题讨论】:

    标签: python pandas dataframe tensorflow


    【解决方案1】:

    pd.read_csv 将隐式推断每列的dtypes

    现在,如果您出于任何原因希望明确指定列的类型,可以通过提供 dtype 参数来实现:

    dtypes = {'colName': np.int64, 'anotherColName': 'float'}
    df = pd.read_csv('C:\TRAININGDATA\DATABASE.csv', dtype=dtypes)
    

    请注意,如果无法使用指定的 dtype 解析列,这将导致错误。


    或者,您也可以对数据帧进行后处理:

    df['anotherColName'] = df['anotherColName'].astype(float)
    

    如果您想检查数据框的dtypes

    df = pd.read_csv("C:\TRAININGDATA\DATABASE.csv")
    print(df.dtypes)
    

    【讨论】:

    • 它不工作。当我们尝试将 NAN 转换为浮点数或字符串时,它会显示错误。字符串的 dtypes 被推断为对象。“ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).”,这是它显示的错误@Giorgos Myrianthous
    • @rocky 那么请编辑您的问题并分享您尝试做的事情以及完整的错误跟踪
    • 嘿,我编辑了这个问题。我错过了什么@Giorgos Myrianthous
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2019-08-23
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多