When i print this in the module without printing to file, it prints all values. But when I print it to the designated file, it only prints the first value.
#Create the model base
from sklearn.feature_selection import chi2
import numpy as np
N = 2
for UNSPSC, category_id in sorted(category_to_id.items()):
features_chi2 = chi2(features, labels == category_id)
indices = np.argsort(features_chi2[0])
feature_names = np.array(tfidf.get_feature_names())[indices]
unigrams = [v for v in feature_names if len(v.split(' ')) == 1]
bigrams = [v for v in feature_names if len(v.split(' ')) == 2]
with open('results.csv', 'w') as f:
print("# '{}':".format(UNSPSC), file=f)
print(" . Most correlated unigrams:\n. {}".format('\n. '.join(unigrams[-N:])), file=f)
print(" . Most correlated bigrams:\n. {}".format('\n. '.join(bigrams[-N:])), file=f)
How can i adjust this so that all values get printed onto the file?