I'm trying to implement an example project on DZone (https://dzone.com/articles/cv-r-cvs-retrieval-system-based-on-job-description) and running into a problem. In this case, I've set
dir_pca_we_EWE = 'pickle_model_pca.pkl'
And am executing the following:
def reduce_dimensions_WE(dir_we_EWE, dir_pca_we_EWE):
m1 = KeyedVectors.load_word2vec_format('./wiki.en/GoogleNews.bin', binary=True)
model1 = {}
# normalize vectors
for string in m1.wv.vocab:
model1[string] = m1.wv[string] / np.linalg.norm(m1.wv[string])
# reduce dimensionality
pca = decomposition.PCA(n_components=200)
pca.fit(np.array(list(model1.values())))
model1 = pca.transform(np.array(list(model1.values())))
i = 0
for key, value in model1.items():
model1[key] = model1[i] / np.linalg.norm(model1[i])
i = i + 1
with open(dir_pca_we_EWE, 'wb') as handle:
pickle.dump(model1, handle, protocol=pickle.HIGHEST_PROTOCOL)
return model1
This then produces the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 12, in reduce_dimensions_WE
AttributeError: 'numpy.ndarray' object has no attribute 'items'
As always, all help is greatly appreciated!
You start by initializing model1 = {}
as an empty dict. By using transform
in
model1 = pca.transform(np.array(list(model1.values())))
the variable model1
becomes a numpy.ndarray
, which is the return type of the transform method of the pca. In the line
for key, value in model1.items():
...
you still use model1
as if it is a dict, which it no longer is.