Skip to content

Instantly share code, notes, and snippets.

@jlewi
Last active June 10, 2016 17:14
Show Gist options
  • Save jlewi/5691269636e50015b9167b43babcc55d to your computer and use it in GitHub Desktop.
Save jlewi/5691269636e50015b9167b43babcc55d to your computer and use it in GitHub Desktop.
Example converting a row of data to Example JSON
import json
from google.protobuf import json_format
from tensorflow.core.example import example_pb2
def convert_row_to_json(row):
e = example_pb2.Example()
e.features.feature['id'].bytes_list.value.append(str(row[id_column_index]))
e.features.feature["target"].int64_list.value.append(row[target_column_index])
# Add features to predict on.
e.features.feature[feature_1_name].float_list.value.append(row[feature_1_index])
#...other features ...
j = json_format.MessageToJson(e)
# Remove newlines.
flat = json.dumps(json.loads(j))
return flat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment