如何在Tensorflow中为numpy矩阵输入创建feature_columns?

问题描述 投票:0回答:1

我使用的是tensorflow 1.8.0,python 3.6.5。 数据是虹膜数据集。这是代码:

import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import tensorflow as tf

X = iris['data']
y = iris['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

input_train=tf.estimator.inputs.numpy_input_fn(x=X_train,
            y=y_train, num_epochs=100, shuffle=False)
classifier_model = tf.estimator.DNNClassifier(hidden_units=[10, 
                  20, 10], n_classes=3, feature_columns=??)

这是我的问题,如何为 numpy 矩阵设置 feature_columns?

如果我将 X 和 y 转换为

pandas.DataFrame
,我可以对 feature_columns 使用以下代码,并且它可以在
DNNClassifier
模型中工作。

features = X.columns
feature_columns = [tf.feature_column.numeric_column(key=key) for key in features]
python pandas numpy tensorflow
1个回答
1
投票

您可以将 numpy ndarray 包装在字典中并将其作为输入传递给

numpy_input_fn
方法作为输入
x
,然后使用该字典中的键来定义您的
feature_column
。另请注意,由于
X_train
中的每个数据都有 4 个维度,因此在定义
shape
时需要指定
tf.feature_column.numeric_column
参数。这是完整的代码:

import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import tensorflow as tf

iris = load_iris()

X = iris['data']
y = iris['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

input_train = tf.estimator.inputs.numpy_input_fn(
                  x = {'x': X_train},
                  y = y_train,
                  num_epochs = 100,
                  shuffle = False)

feature_columns = [tf.feature_column.numeric_column(key='x', shape=(X_train.shape[1],))]

classifier_model = tf.estimator.DNNClassifier(
                       hidden_units=[10, 20, 10],
                       n_classes=3,
                       feature_columns=feature_columns)
© www.soinside.com 2019 - 2024. All rights reserved.