我在试着做一个分类器。目前,我正在生成对象,但我一直收到一个错误:
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发布于 2020-11-12 01:38:32
您应该将所有列转换为float或int数据类型。你可以先使用这种预处理,
dataframe.TYPE_A, mapping_index = pd.Series(dataframe.TYPE_A).factorize()另外,你真的想使用name列作为一个特性吗?
https://stackoverflow.com/questions/64791272
复制相似问题