Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a TF Transform preprocessing function that scales a numeric feature.
TensorFlow
def preprocessing_fn(inputs): import tensorflow_transform as tft x = inputs['feature'] x_scaled = tft.[1](x) return {'feature_scaled': x_scaled}
Attempts:
3 left
💡 Hint
Common Mistakes
Using bucketize instead of scaling for numeric features.
Using scale_to_0_1 which scales between 0 and 1, not zero mean and unit variance.
✗ Incorrect
The function tft.scale_to_z_score scales numeric features to have zero mean and unit variance, which is a common normalization step.
2fill in blank
mediumComplete the code to apply TF Transform's vocabulary function to a categorical feature.
TensorFlow
def preprocessing_fn(inputs): import tensorflow_transform as tft x = inputs['category'] x_vocab = tft.[1](x) return {'category_vocab': x_vocab}
Attempts:
3 left
💡 Hint
Common Mistakes
Using scaling functions on categorical data.
Using bucketize which is for numeric data binning.
✗ Incorrect
The function tft.compute_and_apply_vocabulary creates a vocabulary from the categorical feature and maps the input to integer indices.
3fill in blank
hardFix the error in the TF Transform preprocessing function that tries to bucketize a numeric feature.
TensorFlow
def preprocessing_fn(inputs): import tensorflow_transform as tft x = inputs['age'] x_buckets = tft.[1](x, num_buckets=5) return {'age_buckets': x_buckets}
Attempts:
3 left
💡 Hint
Common Mistakes
Using scaling functions instead of bucketizing.
Using vocabulary functions on numeric data.
✗ Incorrect
The function tft.bucketize splits numeric data into buckets. The code needs 'bucketize' to create age buckets.
4fill in blank
hardFill both blanks to create a TF Transform preprocessing function that normalizes a numeric feature and buckets another feature.
TensorFlow
def preprocessing_fn(inputs): import tensorflow_transform as tft x_num = inputs['income'] x_cat = inputs['education'] income_norm = tft.[1](x_num) education_buckets = tft.[2](x_cat, num_buckets=4) return {'income_norm': income_norm, 'education_buckets': education_buckets}
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing vocabulary functions with numeric bucketing.
Using scale_to_0_1 instead of scale_to_z_score for normalization.
✗ Incorrect
Income is normalized with scale_to_z_score, and education is bucketized into 4 buckets.
5fill in blank
hardFill all three blanks to create a TF Transform preprocessing function that applies vocabulary to a categorical feature, normalizes a numeric feature, and buckets another numeric feature.
TensorFlow
def preprocessing_fn(inputs): import tensorflow_transform as tft cat_feat = inputs['city'] num_feat1 = inputs['temperature'] num_feat2 = inputs['humidity'] city_vocab = tft.[1](cat_feat) temp_norm = tft.[2](num_feat1) humidity_buckets = tft.[3](num_feat2, num_buckets=3) return {'city_vocab': city_vocab, 'temp_norm': temp_norm, 'humidity_buckets': humidity_buckets}
Attempts:
3 left
💡 Hint
Common Mistakes
Using bucketize on categorical features.
Using scale_to_0_1 instead of scale_to_z_score for normalization.
✗ Incorrect
Vocabulary is applied to city, temperature is normalized with scale_to_z_score, and humidity is bucketized into 3 buckets.
