whatlies.transformers.AddRandom

This transformer adds random embeddings to the embeddingset.

Parameters

Name Type Description Default
n the number of random vectors to add 1

Usage:

from whatlies.language import SpacyLanguage
from whatlies.transformers import AddRandom

words = ["prince", "princess", "nurse", "doctor", "banker", "man", "woman",
         "cousin", "neice", "king", "queen", "dude", "guy", "gal", "fire",
         "dog", "cat", "mouse", "red", "blue", "green", "yellow", "water",
         "person", "family", "brother", "sister"]

lang = SpacyLanguage("en_core_web_md")
emb = lang[words]

emb.transform(AddRandom(3)).plot_interactive_matrix('rand_0', 'rand_1', 'rand_2')

fit(self, embset)

Show source code in transformers/_addrandom.py
38
39
40
    def fit(self, embset):
        self.is_fitted = True
        return self

Fit the transformer on the given EmbeddingSet instance (if neccessary). This method should set the is_fitted flag to True.

Parameters

Name Type Description Default
embset an EmbeddingSet instance used for fitting the transformer. required

transform(self, embset)

Show source code in transformers/_addrandom.py
42
43
44
45
46
47
48
49
50
51
52
    def transform(self, embset):
        names, X = embset.to_names_X()
        np.random.seed(self.seed)
        orig_dict = embset.embeddings.copy()
        new_dict = {
            f"rand_{k}": Embedding(
                f"rand_{k}", np.random.normal(0, self.sigma, X.shape[1])
            )
            for k in range(self.n)
        }
        return EmbeddingSet({**orig_dict, **new_dict})

Transform the given EmbeddingSet instance.

Parameters

Name Type Description Default
embset an EmbeddingSet instance to be transformed. required