Demo entry 6780240

Predicting Income with the Census Income Dataset

   

Submitted by anonymous on Dec 24, 2018 at 02:07
Language: Python 3. Code size: 1.7 kB.

age = tf.feature_column.numeric_column('age')
#education_num, capital_gain, capital_loss, hours_per_week are also numerical

education = tf.feature_column.categorical_column_with_vocabulary_list(
    'education', ['Bachelors', 'HS-grad', '11th', 'Masters', '9th', 'Some-college',
                  # ...
        ])
# marital_status, relationship, workclass are also categorical

# hashing
occupation = tf.feature_column.categorical_column_with_hash_bucket(
    'occupation', hash_bucket_size=_HASH_BUCKET_SIZE)

# transformations
age_buckets = tf.feature_column.bucketized_column(
    age, boundaries=[18, 25, 30, 35, 40, 45, 50, 55, 60, 65])

# Wide columns and deep columns.
base_columns = [education, marital_status, relationship, workclass, occupation, age_buckets]

crossed_columns = [
    tf.feature_column.crossed_column(
        ['education', 'occupation'], hash_bucket_size=_HASH_BUCKET_SIZE),
    # ...
]

wide_columns = base_columns + crossed_columns

deep_columns = [
    age, education_num, capital_gain, capital_loss, hours_per_week,
    tf.feature_column.indicator_column(workclass),
    # ...
    tf.feature_column.embedding_column(occupation, dimension=8),
]

if model_type == 'wide':
    model = tf.estimator.LinearClassifier(feature_columns=wide_columns)
# ...
else:
    model = tf.estimator.DNNLinearCombinedClassifier(linear_feature_columns=wide_columns,
                                                     dnn_feature_columns=deep_columns,
                                                     dnn_hidden_units=hidden_units)

model.train(input_fn=input_fn_train, steps=200)
model.evaluate(input_fn=input_fn_test)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).