Tensorflow:如何为 numpy 矩阵输入创建 feature_columns [英] Tensorflow: how to create feature_columns for numpy matrix input
问题描述
我使用的是 tensorflow 1.8.0,python 3.6.5.数据为虹膜数据集.代码如下:
I'm using tensorflow 1.8.0, python 3.6.5. The data is iris data set. Here is the code:
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?
Here is my problem, how do I setup the feature_columns for a numpy matrix?
如果我将 X 和 y 转换为 pandas.DataFrame
,我可以对 feature_columns 使用以下代码,并且它可以在 DNNClassifier
模型中使用.
If I covert the X and y to pandas.DataFrame
, I can use the following code for the feature_columns, and it works in the DNNClassifier
model.
features = X.columns
feature_columns = [tf.feature_column.numeric_column(key=key) for key in features]
推荐答案
您可以将 numpy ndarray 包装在字典中并将其作为输入 x
传递给 numpy_input_fn
方法和然后使用该字典中的键来定义您的 feature_column
.还要注意的是,由于您的X_train
中的每个数据都有 4 个维度,因此您需要在定义 tf.feature_column.numeric_column
时指定 shape
参数.完整代码如下:
You can wrap your numpy ndarray in a dictionary and pass it to numpy_input_fn
method as input x
and then use the key in that dictionary to define your feature_column
. Also note that because each data in your X_train
has 4 dimensions, you need to specify the shape
parameter when defining tf.feature_column.numeric_column
. Here is the completed code:
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)
这篇关于Tensorflow:如何为 numpy 矩阵输入创建 feature_columns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!