I am working with the keras and there is always a from with a lists, so I guess that always everything has to be converted to the numpy array which are very illogical for me. I guess that it is associated with performance? I don't see any other reason? However my problem looks like shown below. I have to convert this part of code:
output_sentence = []
final_output_sentence = []
for key in row['o'].lower():
temp_list = []
if key in dictionary.keys():
temp_list.append(dictionary[key])
output_sentence.append(temp_list)
else:
dictionary[key] = len(dictionary)
temp_list.append(dictionary[key])
output_sentence.append(temp_list)
final_output_sentence.append(output_sentence)
to the code based on numpy arrays. I try in this way:
output_sentence = np.array([], dtype=int)
final_output_sentence = np.array([], dtype=int)
for key in row['o'].lower():
temp_list = np.array([], dtype=int)
if key in dictionary.keys():
temp_list = np.append(temp_list, dictionary[key])
output_sentence = np.append(output_sentence, temp_list)
else:
dictionary[key] = len(dictionary)
temp_list = np.append(temp_list, dictionary[key])
output_sentence = np.append(output_sentence, temp_list)
final_output_sentence = np.append(final_output_sentence, output_sentence)
however instead of this [[[1], [2], [3], [2], [4]]] I get this [1 2 3 2 4]. Any ideas how to solve this?
UPDATE
What do you think about solution shown below? Any tips for performance optimization?
output_sentence = []
for key in row['o'].lower():
temp_list = []
if key in dictionary.keys():
temp_list.append(dictionary[key])
output_sentence.append(temp_list)
else:
dictionary[key] = len(dictionary)
temp_list.append(dictionary[key])
output_sentence.append(temp_list)
final_output_sentence = np.array(output_sentence)
final_output_sentence = final_output_sentence.reshape(1, final_output_sentence.shape[0], 1)
append, and create an array with one call at the end.np.append(and other versions ofnp.concatenate) are slower, and harder to apply correctly.rowanddictionary, and the results from the list version, we might be able to suggest improvements. Sometimes there are ways of replacing list operations with whole-array ones. But replicating the list appends is not one of those. Why are you appending to 2 lists, when you only have one iteration loop (onkey)?temp_list.append(dictionary[key]) output_sentence.append(temp_list)?