使用numpy python数组打印类输出?

时间:2021-02-27 21:45:52

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 个解决方案

#1


1  

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

您可以在最后添加print语句,但仍需要创建属性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'

#1


1  

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

您可以在最后添加print语句,但仍需要创建属性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'