I wrote a function to get cosine similarity but I am having a problem when I have to output my results to a text file. The index variable inside the for look returns all the integer values I needed but in my text file, it stores only one index. Could someone please help?
from scipy.spatial.distance import cosine
def key_consine_similarity(tupple):
return tupple[1]
def get_computed_similarities(vectors, predicted_vectors, reverse=False):
data_size = len(df3)
cosine_similarities = []
for i in range(data_size):
cosine_sim_val = (1 - cosine(vectors[i], predicted_vectors[i]))
cosine_similarities.append((i, cosine_sim_val))
return sorted(cosine_similarities, key=key_consine_similarity, reverse=reverse)
def display_all_n(sorted_cosine_similarities, n=7735):
for i in range(n):
file = open('file.txt', 'w')
index, consine_sim_val = sorted_cosine_similarities[i]
file.write(str(index))
print (index)
file.close()
print('Printing all score')
sorted_cosine_similarities = get_computed_similarities(vectors=doc2vec_vectors, predicted_vectors=predicted_vectors)
display_all_n(sorted_cosine_similarities=sorted_cosine_similarities)
output
Printing all score
3234
2342
3344
5656
file.txt
3234
When you open a file in write mode and write to it all previous content gets deleted.
What is happening to you is that you are opening and closing the file each time you want to write so each write deletes the previous one.
Either open the file only once:
def display_all_n(sorted_cosine_similarities, n=7735):
file = open('file.txt', 'w')
for i in range(n):
index, consine_sim_val = sorted_cosine_similarities[i]
file.write(str(index) + '\n')
print (index)
file.close()
or open the file in append mode:
def display_all_n(sorted_cosine_similarities, n=7735):
for i in range(n):
file = open('file.txt', 'a')
index, consine_sim_val = sorted_cosine_similarities[i]
file.write(str(index) + '\n')
print (index)
file.close()