<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Playing with word2vec
Fabio A. González, Universidad Nacional de Colombia
Google News dataset
Internal Google data set with one billion words
692k vocabulary. Words with frequency below 5 were discarded.
300-dimensions representation was obtained by training a skip-gram model.
Model available at https
Step1:  Continuous representation of words
Step2:  Semantically related words have similar representations
Step3:  Vector representation similarity = semantic similarity
Step4:  Word vector space encodes linguistic regularities
<img width= 600 src="linguistic regularities.jpg">
Solving analogies
man is to woman as king is to ??
Relationships are encoded by word vector differences
Step5:  Calculating comparatives from adjectives
Step6:  Comparative vector
Step7:  Compositionality
Step8:  Spanish WikiNews dataset
Public data set with 94 million words
213k vocabulary (converted to lowercase, accents removed)
300-dimensions representation was obtained by training a cbow model
<ASSISTANT_TASK:>
Python Code:
## Loading the model with `gensim`
# import wrod2vec model from gensim
from gensim.models.word2vec import Word2Vec
# load Google News pre-trained network
model = Word2Vec.load_word2vec_format('GNvectors.bin', binary=True)
Explanation: Playing with word2vec
Fabio A. González, Universidad Nacional de Colombia
Google News dataset
Internal Google data set with one billion words
692k vocabulary. Words with frequency below 5 were discarded.
300-dimensions representation was obtained by training a skip-gram model.
Model available at https://drive.google.com/file/d/0B7XkCwpI5KDYNlNUTTlSS21pQmM/edit?usp=sharing
End of explanation
pp(model['table'])
Explanation: Continuous representation of words
End of explanation
plt.plot(model['car'][:50], label = 'car')
plt.plot(model['vehicle'][:50], label = 'vehicle')
plt.legend()
Explanation: Semantically related words have similar representations
End of explanation
pp(model.most_similar(positive=['car']))
Explanation: Vector representation similarity = semantic similarity
End of explanation
result = model.most_similar(negative=['man'], 
                            positive=['woman', 'king'])
pp(result)
Explanation: Word vector space encodes linguistic regularities
<img width= 600 src="linguistic regularities.jpg">
Solving analogies
man is to woman as king is to ??
Relationships are encoded by word vector differences: 
$$ f(\textrm{"woman"}) - f(\textrm{"man"}) = f(\textrm{"??"}) - f(\textrm{"king"})$$
We can add the relationship encoding vector to a the vector of king: $$ f(\textrm{"king"}) + (f(\textrm{"woman"}) - f(\textrm{"man"})) = f(\textrm{"??"}) $$
$$ f(\textrm{"king"}) + (f(\textrm{"woman"}) - f(\textrm{"man"})) = f(\textrm{"??"}) $$
End of explanation
def plot_data(orig_data, labels):
    pca = PCA(n_components=2)
    data = pca.fit_transform(orig_data)
    plt.plot(data[:,0], data[:,1], '.')
    for i in range(len(data)):
        plt.annotate(labels[i], xy = data[i])
    for i in range(len(data)/2):
        plt.annotate("",
                xy=data[i],
                xytext=data[i+len(data)/2],
                arrowprops=dict(arrowstyle="->",
                                connectionstyle="arc3")
                )
def analogy(worda, wordb, wordc):
    result = model.most_similar(negative=[worda], 
                                positive=[wordb, wordc])
    return result[0][0]
adjectives = ['big', 'small', 'large', 'wide', 'strong']
comparatives = [analogy('good', 'better', adjective) for adjective in adjectives]
pp(zip(adjectives,comparatives))
Explanation: Calculating comparatives from adjectives
End of explanation
labels = comparatives + adjectives
data = [model[w] for w in labels]
plot_data(data, labels)
Explanation: Comparative vector
End of explanation
 pp(model.most_similar(positive=['Colombia', 'currency']))
Explanation: Compositionality
End of explanation
model_es = Word2Vec.load_word2vec_format('eswikinews.bin', binary=True)
 pp(model_es.most_similar(positive=['yo_soy_betty']))
Explanation: Spanish WikiNews dataset
Public data set with 94 million words
213k vocabulary (converted to lowercase, accents removed)
300-dimensions representation was obtained by training a cbow model
End of explanation
<END_TASK>