0

i have this class which build the classification features, by averaging the word vectors for all vectors in the text

class MeanEmbeddingVectorizer(object):
    def __init__(self, word2vec):
        self.word2vec = word2vec
        self.dim = len(word2vec.itervalues().next())

    def fit(self, X, y):
        return self

    def transform(self, X):
        return np.array([
            np.mean([self.word2vec[w] for w in words if w in self.word2vec]
                    or [np.zeros(self.dim)], axis=0)
            for words in X
        ])

so my question how to print the output of this class to screen or save it to a file Thank you

1 Answer 1

1

You could add a print statement at the end but you still need to create the attribute word2vec

import numpy as np

class MeanEmbeddingVectorizer(object):
    def __init__(self, word2vec):
        self.word2vec = word2vec
        self.dim = len(word2vec.itervalues().next())

    def fit(self, X, y):
        return self

    def transform(self, X):
        return np.array([
            np.mean([self.word2vec[w] for w in words if w in self.word2vec]
                    or [np.zeros(self.dim)], axis=0)
            for words in X
        ])
print(MeanEmbeddingVectorizer.fit("X", "Y", "Z"))

will give you the output

X
None

but if you run

print(MeanEmbeddingVectorizer.transform("X", "Y"))

you get

AttributeError: 'str' object has no attribute 'word2vec'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.