使用Iris示例加载csv时,tensorflow的值错误

时间:2018-10-24 14:00:04

标签: python numpy tensorflow jupyter-notebook artificial-intelligence

当尝试在Jupyter上运行简单的get数据序列时,对于系统识别虹膜花tyoes和费舍尔桌子的系统,错误:

  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不能正常工作,如错误所示? 预先感谢。

1 个答案:

答案 0 :(得分:1)

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

target_dtype=np.float32

更新

tensorflow.base有多个load_csv..,您可以尝试base.load_csv_without_headerbase.load_csv

特定的iris张量流示例应用于其自己的数据集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用于存储第一列中的样本数。