CSV 文件导入 SkFlow [英] CSV File Into SkFlow

查看:39
本文介绍了CSV 文件导入 SkFlow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Tensorflow.据我了解,SkFlow 是一个......

I'm just starting out with Tensorflow. As I understand it, SkFlow is a...

TensorFlow 的简化界面

Simplified interface for TensorFlow

对我来说简单就是好的.

And for me simple is good.

TensorFlow 的 Github 有一些有用的使用 SkFlow 中包含的 Iris 数据集的入门示例.这是来自第一个示例,线性分类器.

TensorFlow's Github has some useful starter examples using the Iris dataset included in SkFlow. This is from the first example, the Linear Classifier.

iris = datasets.load_iris()
feature_columns = learn.infer_real_valued_columns_from_input(iris.data)

这个 iris 对象的类型为 并且是一个类似字典的结构,包含两个列表和数据和目标.

This iris object has the type <class 'sklearn.datasets.base.Bunch'> and is a dict like structure containing two lists and the data and the targets.

此链接展示了如何从 CSV(或至少是 URL)加载数据.在页面顶部,它显示了如何通过上述方法加载,然后通过 URL 加载,就像这样

This link shows how to load data from a CSV (or at least a URL). At the top of the page it shows how to load via the method above, and then via the URL, like so

# Load the Pima Indians diabetes dataset from CSV URL
import numpy as np
import urllib
# URL REMOVED - SO DOES NOT LIKE SHORTENED URL
# URL for the Pima Indians Diabetes dataset
raw_data = urllib.urlopen(url)
# load the CSV file as a numpy matrix
dataset = np.loadtxt(raw_data, delimiter=",")
print(dataset.shape)
# separate the data from the target attributes
X = dataset[:,0:7]
y = dataset[:,8]

我知道 X 是数据,而 y 是目标.但这不是 github 示例或指南的第一个示例中的数据结构.

I get that X is the data, and y is the target. But that's not the structure of the data in the github example, or in the first example of the guide.

我的意思是将 CSV 数据转换为单个对象吗

Am I meant to turn the CSV data into a single object as in

    iris = datasets.load_iris()

或者我是否使用 Xy 输出?如果是这样,我该如何使用 Github 上的线性分类器示例来做到这一点

Or do I work with the X and y outputs? And if so, how do I do that with the Linear Classifier example on Github

推荐答案

我正在研究同一个教程.我使用 scikit learn 的 cross_validation 方法将 scikit Bunch 对象分解为训练/测试拆分.然后只需在classifier.fit 和classifier.evaluate 方法中使用它们.

I was working on the same tutorial. I used scikit learn's cross_validation method to break the scikit Bunch object into train/test splits. Then just use those in the classifier.fit and classifier.evaluate methods.

from sklearn import cross_validation
import tensorflow as tf
import numpy as np
from sklearn import datasets

# load from scikit learn
iris = datasets.load_iris()
# break into train/test splits
x_train, x_test, y_train, y_test = cross_validation.train_test_split(
             iris.data, iris.target, test_size=0.2, random_state=42)

# commented out the previous loading code
'''
# Data sets
IRIS_TRAINING = "iris_training.csv"
IRIS_TEST = "iris_test.csv"
# Load datasets.
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
    filename=IRIS_TRAINING,
    target_dtype=np.int,
    features_dtype=np.float32)
test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
    filename=IRIS_TEST,
    target_dtype=np.int,
    features_dtype=np.float32)
'''
# Specify that all features have real-value data
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]

# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
                                        hidden_units=[10, 20, 10],
                                        n_classes=3,
                                        model_dir="./tmp/iris_model")

# Fit model. Add your train data here
classifier.fit(x=x_train,y=y_train,steps=2000)

# Evaluate accuracy. Add your test data here
accuracy_score = classifier.evaluate(x=x_test,y=y_test)["accuracy"]
print('Accuracy: {0:f}'.format(accuracy_score))

# Classify two new flower samples.
new_samples = np.array(
    [[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float)
y = list(classifier.predict(new_samples, as_iterable=True))
print('Predictions: {}'.format(str(y)))

这篇关于CSV 文件导入 SkFlow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆