Keras分类器错误“ ValueError:无法将NumPy数组转换为张量不受支持的对象类型float”

问题描述

我正在尝试建立一个分类器。当前,我正要生成对象,但是我一直遇到错误

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).

我不确定为什么会收到此错误或如何解决错误。我对这一切还很陌生,因此任何提示解决方案都将是很棒的。谢谢。

import tensorflow as tf
from keras.models import Sequential
import pandas as pd
from keras.layers import Dense
import seaborn as sns
import matplotlib as plt
from keras.utils import to_categorical

from sklearn.model_selection import train_test_split
from sklearn import preprocessing

dataframe = pd.read_csv('file.csv')

val_dataframe = dataframe.sample(frac=0.2,random_state=1337)
train_dataframe = dataframe.drop(val_dataframe.index)

print(
    "Using %d samples for training and %d for validation"
    % (len(train_dataframe),len(val_dataframe))
)

def dataframe_to_dataset(dataframe):
    dataframe = dataframe.copy()
    labels = dataframe.pop("output")
    ds = tf.data.Dataset.from_tensor_slices((dict(dataframe),labels))
    ds = ds.shuffle(buffer_size=len(dataframe))
    return ds


train_ds = dataframe_to_dataset(train_dataframe)
val_ds = dataframe_to_dataset(val_dataframe)

CSV文件中的数据示例:

         0    1    2        3          4       5    6     7
0     Name  TRY  LOC   OUTPUT     TYPE_A   SIGNAL  A-B  SPOT
1    inc 1    2   20   TYPE-1    TORPEDO   ULTRA    A   -21
2    inc 2    3   16   TYPE-2    TORPEDO     ILH    B   -14
3    inc 3    2   20  BLACK47    TORPEDO    LION    A    49
4    inc 4    3   12   TYPE-2  CENTRALPA    LION    A    25
5    inc 5    3   10   TYPE-2      THREE    LION    A   -21
6    inc 6    2   20   TYPE-2        ATF    LION    A   -48
7    inc 7    4    2  NIVEA-1        ATF    LION    B   -23
8    inc 8    3   16  NIVEA-1        ATF    LION    B    18
9    inc 9    3   18  BLENDER  CENTRALPA    LION    B    48
10   inc 10   4   20    DELCO        ATF    LION    B   -26
11   inc 11   3   20    VE248        ATF    LION    B    44
12   inc 12   1   20   SILVER  CENTRALPA    LION    B   -35
13   inc 13   2   20  CALVIN3     SEVENX    LION    B   -20
14   inc 14   3   14  DECK-BT  CENTRALPA    LION    B   -38
15   inc 15   4    4  10-LEVI    BERWYEN     OWL    B   -29
16   inc 16   4   14   TYPE-2        ATF     NOV    B   -31
17   inc 17   4   10     NYNY    TORPEDO     NOV    B    21
18   inc 18   2   20  NIVEA-1  CENTRALPA     NOV    B    45
19   inc 19   3   27   FMRA97    TORPEDO     NOV    B   -26
20   inc 20   4   18   SILVER        ATF     NOV    B   -46

解决方法

您应将所有列转换为float或int数据类型。您可以首先使用这种预处理,

max.poll.records

您还真的要使用dataframe.TYPE_A,mapping_index = pd.Series(dataframe.TYPE_A).factorize() 列作为功能吗?