python - 如何修复此错误“无法将 numpy 数组转换为张量”
问题描述
我正在 tensorflow 上制作一个保险费预测器(给出单个输出,它是一个浮点数),获取年龄、性别等数据(总共 6 个值)。这是代码
import tensorflow as tf
import numpy as np
from tensorflow import keras
import pandas as pd
a=0
file=pd.read_csv(r"""C:\Users\lavni\OneDrive\Desktop\proj.csv""",sep=',',index_col=False)
Data = file[['age', 'sex', 'bmi', 'children', 'smoker', 'region']].to_numpy()
Charges=file[['charges']].to_numpy()
model = keras.Sequential()
model.add(keras.layers.Dense(units=6, input_shape=(6,)))
model.compile(optimizer='sgd',loss='mean_squared_error')
model.fit(Data,Charges)
当我运行代码时,我收到以下错误:
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\ML.py", line 14, in <module>
model.fit(Data,Charges)
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1063, in fit
steps_per_execution=self._steps_per_execution)
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1117, in __init__
model=model)
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\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\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1021, in _process_tensorlike
inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\util\nest.py", line 635, in map_structure
structure[0], [func(*x) for x in entries],
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\util\nest.py", line 635, in <listcomp>
structure[0], [func(*x) for x in entries],
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1016, in _convert_numpy_and_scipy
return ops.convert_to_tensor(x, dtype=dtype)
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\ops.py", line 1499, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\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\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\constant_op.py", line 264, in constant
allow_broadcast=True)
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\constant_op.py", line 275, in _constant_impl
return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\constant_op.py", line 300, in _constant_eager_impl
t = convert_to_eager_tensor(value, ctx, dtype)
File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\constant_op.py", line 98, 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 int).
任何帮助表示赞赏,并提前感谢您。
解决方案
Charges = np.array([file.loc[:,'charges']])
这可能会解决您的问题。首先,使用方法过滤数据框loc
。之后,将其转换为 numpy 数组。
推荐阅读
- node.js - 无法在终端上使用 psql 命令访问在 docker 容器内运行的 postgres
- java - Java ClassCastException 使用时?延伸
- symfony - Symfony 自定义表单字段类型 FQCN 存储在数据库字段中
- arrays - React Native 对象子映射在父映射中
- django - 使用 django 身份验证 password_reset_confirm 错误
- python - 用户可选择的函数及其参数
- jprofiler - 未经许可在商业上使用 JProfile 代理文件 (libjprofilerti.so) 是否合法?
- sql - 使用 SCOPE_IDENTITY 导致另一个查询
- python - 如何解释这个
- reactjs - 如何使用 react-leaflet containerPointToLatLng?