python - 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).
解决方案
我建议您可以尝试从 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
推荐阅读
- sql - Group by 同时丢弃查询中的旧数据
- delphi - 将 ASCII 文件转换为二进制文件 - 高效的方法
- javascript - 在javascript中为不同打开的网页运行单独的计时器
- spring -
- mysql - 如何在 Ceph 上运行 MySQL?
- vue.js - 在 Vue.js 中按下 Enter 按钮时如何调用函数
- python - TypeError: run() missing 1 required positional argument: 'self' (Space Invaders Bullet issue)
- python - TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是运行 python manage.py migrate 时的“datetime.datetime”
- python - 从芦苇传感器棋盘生成 FEN 字符串,只从鱼引擎获得下一个最佳移动
- android - 在 Android Gradle Build 3.1.2 中禁用清单合并