Tensorflow ValueError:无法将NumPy数组转换为Tensor(不支持的对象类型Timestamp)

u1ehiz5o  于 2023-06-29  发布在  其他
关注(0)|答案(1)|浏览(147)

我的代码使用所有megasena结果的csv文件。

data['Data'] = pd.to_datetime(data['Data'], format='%d/%m/%Y')
features = data[['Data', 'Conc']]
labels = data[['NR1', 'NR2', 'NR3', 'NR4', 'NR5', 'NR6']]

# convert date column to numerical features
features['Day'] = features['Data'].dt.day 
features['Month'] = features['Data'].dt.month 
features['Year'] = features['Data'].dt.year  

# normalize the features (year should have more weightage than day and month)
features['Day'] = features['Day'] / 31.0
features['Month'] = features['Month'] / 12.0
features['Year'] = (features['Year'] - 2000) / 20.0

# split the data into training and testing sets
train_features = features[features['Conc'] <= 2601].values
train_labels = labels[labels.index <= 2601].values
test_features = features[features['Conc'] > 2601].values

# define the model architecture
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(64, input_shape=[5], activation='relu'),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(6, activation='softmax')
])

这个错误发生在训练中,我已经尝试通过将日,月和年的类型改为float,np.float32,tf.float32来解决它,但没有一个解决了这个问题。

# train the model
model.fit(train_features, train_labels, epochs=100)
avkwfej4

avkwfej41#

找到的解决方案是将日期转换为浮点数并删除日期列

features['DataF'] = data['Data'].apply(lambda x: x.timestamp())
features['Day'] = features['Data'].dt.day
features['Month'] = features['Data'].dt.month
features['Year'] = features['Data'].dt.year
features.drop(['Data'], axis=1, inplace=True)

相关问题