You are on page 1of 1

import re

import numpy

label_map = [] # the map that holds the link between the label and the
one_hot_encoded label
file_info = [] # holds all your file locations and the label of the
file
#read all the lines. A line should look like this: mycatimage.png,1
with open('labels.txt') as f:
rows = [re.split(",", line.rstrip("\n")) for line in f]

for row in rows:


file_info.append(row)

label_column = [line[1] for line in rows] # line[1] is based on your


'data/cats/cat (1) copy.png,1' example where the label is the second
index

unique_labels = list(set(label_column)) # set gives unique values


# now the onehot encoding of the labels which basically means
everything 0 except 1
for label in unique_labels:
output_values = np.zeros(len(unique_labels), dtype=np.int)
output_values [unique_labels.index(label)] = 1
label_map.append({'name': label , 'value': output_values })

# Write the found labels to the label file if you want for later use.
We will use the label_map variable for now
with open("mylabelfile.txt", 'w+') as lf:
for label in label_map:
lf.write(label['name'] + ',' + ','.join(str(x) for x in
label['value']) + "\n") # writes --> Christina,0,0,1,0\n

You might also like