python - 使用 iris 示例加载 csv 时 tensorflow 的值错误

标签 python numpy tensorflow jupyter-notebook artificial-intelligence

当尝试在 Jupyter 上运行一个简单的获取数据序列时,为了让系统识别鸢尾花类型,错误是 teough fisher 的表,错误:

  ValueError                                Traceback (most recent call last)
 <ipython-input-12-269564554b65> in <module>
 10 training_set = base.load_csv_with_header(filename=IRIS_TRAINING,
 11                                      features_dtype=np.float32,
 ---> 12                                      target_dtype=np.float32)
 13 test_set = base.load_csv_with_header(filename=IRIS_TEST,
 14                                  features_dtype=np.float32,

 /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py in load_csv_with_header(filename, target_dtype, features_dtype, target_column)
 46     data_file = csv.reader(csv_file)
 47     header = next(data_file)
 ---> 48     n_samples = int(header[0])
 49     n_features = int(header[1])
 50     data = np.zeros((n_samples, n_features), dtype=features_dtype)

ValueError: invalid literal for int() with base 10: '5.1'

正在显示。该错误表明它无法使用 int() 函数,尽管代码中根本没有 int。这是代码:

import tensorflow as tf
import numpy as np
from tensorflow.contrib.learn.python.learn.datasets import base

# Data files
IRIS_TRAINING = "iris_training.csv"
IRIS_TEST = "iris_test.csv"

# Load datasets.
training_set = base.load_csv_with_header(filename=IRIS_TRAINING,
                                     features_dtype=np.float32,
                                     target_dtype=np.float32)
test_set = base.load_csv_with_header(filename=IRIS_TEST,
                                 features_dtype=np.float32,
                                 target_dtype=np.float32)

print(training_set.data)

print(training_set.target)

为什么 target_dytype=np.int 不起作用,如错误所示? 提前致谢。

最佳答案

答案就在那里,您无法将小数转换为整数。尝试改用 numpy 数据类型 float32

target_dtype=np.float32

更新

tensorflow.base 有几个 load_csv..,你可以试试 base.load_csv_without_header 或者 base.load_csv >.

具体的iris tensorflow示例应用到自己的数据集上,here第一列标题存储导致以下错误的示例数:

 46     data_file = csv.reader(csv_file)
 47     header = next(data_file)
 ---> 48     n_samples = int(header[0])
 49     n_features = int(header[1])

header 返回 csv 文件的第一行,n_samples 用于存储第一列的样本数。

关于python - 使用 iris 示例加载 csv 时 tensorflow 的值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52970934/

相关文章:

Python:tkinter 上的 Z-index

python - 如何找到中轴的关节和端点

python - 数据类型。 Python 中 S1 和 S2 的区别

python - 返回 Keras 中 RNN 中跨时间步长的所有状态

python - 在 Google App Engine 上运行 Alembic 迁移

Python mysql 连接器 LIKE 针对 unicode 值

python - 余弦相似度产生 'nan' 值 pt.II

python - 将 csv 文件中的列加载到 spaCy

python - tf.app.flags 的用法或 API

python - 创建随机索引为零的张量