Saya memiliki kelas berikut:

class TfidfEmbeddingVectorizer(object):
    def __init__(self, word2vec):
        self.word2vec = word2vec
        self.word2weight = None
        self.dim = len(word2vec[next(iter(w2v))])

    def fit(self, X, y):
        tfidf = TfidfVectorizer(analyzer=lambda x: x)
        tfidf.fit(X)
        # if a word was never seen - it must be at least as infrequent
        # as any of the known words - so the default idf is the max of
        # known idf's
        max_idf = max(tfidf.idf_)
        self.word2weight = defaultdict(
            lambda: max_idf,
            [(w, tfidf.idf_[i]) for w, i in tfidf.vocabulary_.items()])

        return self

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

Tetapi ketika saya instantiate saya mendapatkan kesalahan berikut:

File "<ipython-input-70-dcde03597dd3>", line 23, in <listcomp>
for w in words if w in self.word2vec] or
TypeError: 'NoneType' object is not subscriptable
0
Devesh 5 Mei 2018, 17:22

1 menjawab

Jawaban Terbaik

Nah, ketika Anda menerima kesalahan TypeError: 'NoneType' object is not subscriptable, Anda mencoba melakukan sesuatu seperti None[0].

Dan masalah Anda ada di sini:

self.word2weight = None

Dan kemudian Anda mencoba mengaksesnya di sini:

np.mean([self.word2vec[w] * self.word2weight[w]

Mungkin Anda perlu memanggil fungsi fit terlebih dahulu yang menulis dalam word2weight

2
Fer Nando 5 Mei 2018, 15:08