首页 > 解决方案 > ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型时间戳)

问题描述

有一些类似的 ValueErrors 问题,但没有关于 Timestamp 的问题。

我试图在“n”天后预测比特币的价格。

我将“日期”列转换为Timestamp. 在拟合模型时,我需要将张量转换为 Numpy 数组。但我不能;发生此错误。有办法解决吗?

(另外,如果我有任何其他错误,或者代码中有可以改进的地方,请告诉我。)

我也觉得我错过了什么。(我确实删掉了预处理部分,稍后会这样做,我也可以对此提出一些建议。)

代码:

import tensorflow as tf
import pandas as pd
import numpy as np

df = pd.read_csv('btcdata.csv', header=0, parse_dates=[0])
print(df)
target = df.pop('Close')
df = df.values

print()


model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(50, activation='relu', input_shape=(4,)))
model.add(tf.keras.layers.Lambda(
    lambda x: tf.expand_dims(model.output, axis=-1)))
model.add(tf.keras.layers.LSTM(100, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='relu'))
model.summary()
model.compile(optimizer='adam', loss='mean_absolute_error',
              metrics=['accuracy'])

model.fit(df, target, epochs=10)

错误:

Traceback (most recent call last):
  File "time-series-test.py", line 23, in <module>
    model.fit(df, target, epochs=10)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 815, in fit
    model=self)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1112, in __init__
    model=model)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 265, in __init__
    x, y, sample_weights = _process_tensorlike((x, y, sample_weights))
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1013, in _process_tensorlike
    inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\util\nest.py", line 617, in map_structure
    structure[0], [func(*x) for x in entries],
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\util\nest.py", line 617, in <listcomp>
    structure[0], [func(*x) for x in entries],
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1008, in _convert_numpy_and_scipy
    return ops.convert_to_tensor(x, dtype=dtype)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1341, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\tensor_conversion_registry.py", line 52, in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py", line 262, in constant
    allow_broadcast=True)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py", line 270, in _constant_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "C:\Users\User\anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py", line 96, 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 Timestamp).

标签: pythonpandasnumpytensorflow

解决方案


我建议您可以尝试从 DataFrame 中删除索引,因为我假设它包含时间戳。

df.reset_index(drop=True, inplace=True) # Replaces the index with integers

学分:https ://kite.com/python/answers/how-to-drop-the-index-column-of-a-pandas-dataframe-in-python


推荐阅读