I have a model trained with Word2Vec. It works well. I would like to plot only a list of words which I have entered in a list. I have written the function below (and reused some code found) and get the following error message when a vector is added to arr : 'ValueError: all the input arrays must have same number of dimensions'
def display_wordlist(model, wordlist):
vector_dim = model.vector_size
arr = np.empty((0,vector_dim), dtype='f') #dimension trained by the model
word_labels = [word]
# get words from word list and append vector to 'arr'
for wrd in wordlist:
word_array = model[wrd]
arr = np.append(arr,np.array(word_array), axis=0) #This goes wrong
# Use tsne to reduce to 2 dimensions
tsne = TSNE(perplexity=65,n_components=2, random_state=0)
np.set_printoptions(suppress=True)
Y = tsne.fit_transform(arr)
x_coords = Y[:, 0]
y_coords = Y[:, 1]
# display plot
plt.figure(figsize=(16, 8))
plt.plot(x_coords, y_coords, 'ro')
for label, x, y in zip(word_labels, x_coords, y_coords):
plt.annotate(label, xy=(x, y), xytext=(5, 2), textcoords='offset points')
plt.xlim(x_coords.min()+0.00005, x_coords.max()+0.00005)
plt.ylim(y_coords.min()+0.00005, y_coords.max()+0.00005)
plt.show()
arr
has a shape of (0, vector_dim)
and word_array
has a shape of (vector_dim,)
. That's why you are getting that error.
Simply reshaping word_array does the trick:
word_array = model[wrd].reshape(1, -1)
Why are you passing the word list instead of "querying" the model for it?
wordlist = list(model.wv.vocab)
Thanks. I have now modified my code and it delivers the correct result:
def display_wordlist(model, wordlist):
vectors = [model[word] for word in wordlist if word in model.wv.vocab.keys()]
word_labels = [word for word in wordlist if word in model.wv.vocab.keys()]
word_vec_zip = zip(word_labels, vectors)
# Convert to a dict and then to a DataFrame
word_vec_dict = dict(word_vec_zip)
df = pd.DataFrame.from_dict(word_vec_dict, orient='index')
# Use tsne to reduce to 2 dimensions
tsne = TSNE(perplexity=65,n_components=2, random_state=0)
np.set_printoptions(suppress=True)
Y = tsne.fit_transform(df)
x_coords = Y[:, 0]
y_coords = Y[:, 1]
# display plot
plt.figure(figsize=(16, 8))
plt.plot(x_coords, y_coords, 'ro')
for label, x, y in zip(df.index, x_coords, y_coords):
plt.annotate(label, xy=(x, y), xytext=(5, 2), textcoords='offset points')
plt.xlim(x_coords.min()+0.00005, x_coords.max()+0.00005)
plt.ylim(y_coords.min()+0.00005, y_coords.max()+0.00005)
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.