Reputation: 49
def cast_vector(row):
return np.array(list(map(lambda x: x.astype('float32'), row)))
words = pd.DataFrame(word_vectors.vocab.keys())
words.columns = ['words']
words['vectors'] = words.words.apply(lambda x: word_vectors.wv[f'{x}'])
words['vectors_typed'] = words.vectors.apply(cast_vector)
words['cluster'] = words.vectors_typed.apply(lambda x: model.predict([np.array(x)]))
#words.cluster = words.cluster.apply(lambda x: x[0])
Why is there an error although it's float32?
Upvotes: 4
Views: 14037
Reputation: 101
For me it worked to change the kmeans definition to use the word vectors as double. The resulting code is:
from sklearn.cluster import KMeans
word_vectors = Word2Vec.load("../models/word2vec.model").wv
kmeans = KMeans(n_clusters=2, max_iter=1000, random_state=True, n_init=50).fit(X=word_vectors.vectors.astype('double'))
def cast_vector(row):
return np.array(list(map(lambda x: x.astype('double'), row)))
words = pd.DataFrame(word_vectors.vocab.keys())
words.columns = ['words']
words['vectors'] = words.words.apply(lambda x: word_vectors[f'{x}'])
words['vectors_typed'] = words.vectors.apply(cast_vector)
words['cluster'] = words.vectors_typed.apply(lambda x: kmeans.predict([np.array(x)]))
words.cluster = words.cluster.apply(lambda x: x[0])
words['cluster_value'] = [1 if i==0 else -1 for i in words.cluster]
words['closeness_score'] = words.apply(lambda x: 1/(model.transform([x.vectors]).min()), axis=1)
words['sentiment_coeff'] = words.closeness_score * words.cluster_value
words.head(10)
Upvotes: 9