【问题标题】:ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type Timestamp)ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型时间戳)
【发布时间】:2020-09-15 05:37:33
【问题描述】:

有一些类似的 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).

【问题讨论】:

    标签: python pandas numpy tensorflow


    【解决方案1】:

    我建议您可以尝试从 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

    【讨论】:

    • index 和index_col 是两个不同的东西吗?因为我在使用pandas加载数据时没有使用index_col
    • @AidanL.、indexindex_col 不同,因为index_col 用于创建行标签,而DataFrame.index 用于在创建 DataFrame 后检索这些值。 Pandas 可以方便地访问 DataFrame 中的数据,这就是为什么索引不像列表列表那样不是第一列的原因。我认为这是一个足够有意义的答案:)
    • 我尝试使用你给我的代码,但现在我得到一个不同的错误:KeyError: "[Timestamp('2020-05-27 00:00:00') Timestamp('2020-05-26 00:00:00')\n Timestamp('2020-05-25 00:00:00') ... Timestamp('2014-12-03 00:00:00')\n Timestamp('2014-12-02 00:00:00') Timestamp('2014-12-01 00:00:00')] not found in axis
    猜你喜欢
    • 2020-07-05
    • 2020-09-09
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    相关资源
    最近更新 更多