4

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=??)

Here is my problem, how do I setup the feature_columns for a numpy matrix?

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]
1

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

This isn't working, here's an error I'm getting: "Exception has occurred: AttributeError module 'tensorflow_estimator.python.estimator.api._v2.estimator' has no attribute 'inputs'"
@aokelly This is because you are using tensorflow 2.x. This question and my answer are based on tensorflow 1.8.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.