我在python中的代码用于多标签分类;使用TF-IDF矢量器处理一堆推文。我只是把代码的相应部分放在下面。我的vocab是14182个单词的词典,train_array.shape是(683814182(。train_labels.shape也是(6838,11(:
#Vectorizing
vector_maker = TfidfVectorizer(stop_words= set(stopwords.words('english')), vocabulary= vocab) #Vectorizer
train_array = vector_maker.fit_transform(train_tweets).toarray() #Making vector for train tweets
test_array = vector_maker.fit_transform(test_tweets).toarray() #Making vector for test tweets
clf = tree.DecisionTreeClassifier()
clf.fit(train_array,train_labels)
predicted= clf.predict(test_array)
print("Accuracy = ",accuracy_score(test_gold_labels,predicted))
代码运行良好。现在我想使用Word2Vec作为矢量器。我把代码改成:
#Vectorizing
vector_maker = Word2Vec(vocab, size=50, window=5, min_count=1, workers=8) #Vectorizer
train_array = vector_maker.train(train_tweets, total_examples=vector_maker.corpus_count, epochs=15) #Making vector for train tweets
test_array = vector_maker.train(test_tweets, total_examples=vector_maker.corpus_count, epochs=15) #Making vector for test tweets
clf = tree.DecisionTreeClassifier()
clf.fit(train_array,train_labels)
predicted= clf.predict(test_array)
print("Accuracy = ",accuracy_score(test_gold_labels,predicted))
然后我得到这个错误:
ValueError Traceback (most recent call last)
<ipython-input-8-3977a56bf1df> in <module>
71 #clf = RandomForestClassifier()
72 clf = tree.DecisionTreeClassifier()
---> 73 clf.fit(train_array,train_labels)
74 predicted= clf.predict(test_array)
75 print("Accuracy = ",accuracy_score(test_gold_labels,predicted))
~Anaconda3libsite-packagessklearntreetree.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
814 sample_weight=sample_weight,
815 check_input=check_input,
--> 816 X_idx_sorted=X_idx_sorted)
817 return self
818
~Anaconda3libsite-packagessklearntreetree.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
128 random_state = check_random_state(self.random_state)
129 if check_input:
--> 130 X = check_array(X, dtype=DTYPE, accept_sparse="csc")
131 y = check_array(y, ensure_2d=False, dtype=None)
132 if issparse(X):
~Anaconda3libsite-packagessklearnutilsvalidation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
519 "Reshape your data either using array.reshape(-1, 1) if "
520 "your data has a single feature or array.reshape(1, -1) "
--> 521 "if it contains a single sample.".format(array))
522
523 # in the future np.flexible dtypes will be handled like object dtypes
ValueError: Expected 2D array, got 1D array instead:
array=[1249397. 9119055.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
然后我发现trainarray不是一个数组。我的意思是,我发现要获得Word2Vec的训练向量,你应该使用vector_maker.wv.vectors。但首先我尝试了一下向量维度:
print(vector_maker.wv.vectors.shape)
但我得到了(30,50(。我不应该得到(6838,50(吗?还是怎样实际上,我对Word2Vec的工作原理不太了解。我读了很多书,但没读那么多。你们能告诉我应该怎么做才能使用创建的向量进行分类吗?
不要将.fit()
和TfidfVectorizer
重新转换为测试数据:它会更改单词indexes&权重以匹配测试数据。相反,对训练数据进行拟合,然后对测试数据使用相同的训练数据拟合模型,以反映出你只根据在没有训练数据的情况下学到的知识来分析测试数据,并且具有兼容的
您的代码没有正确使用gensim Word2Vec
类:
-
什么是"vocab"?(
Word2Vec
类要么需要一个已经标记化的文本的可迭代训练语料库,要么什么都不需要,这样你就可以手动执行后面的步骤。它不需要任何被描述为vocab
的东西,而且你还没有显示vocab
是什么,如果你在Word2Vec()
实例化中提供了一个语料库,那么你就不会在该模型上调用train()
。( -
。
train()
不返回与每个文本或每个单词相对应的数组:只是一些关于训练的摘要数字。你必须稍后向模型询问你需要的每个学习单词向量。而且,单词向量并不是多单词文本的摘要(除非你把它们平均在一起(。 -
目前还不清楚你是否已经按照
Word2Vec
的要求,将文本预先标记成单词列表。如果您传递了原始字符串,而不是单词标记,那么模型将为每个字符学习无意义向量。如果你的文本中只有30个唯一的字符,这将解释(30, 50)
的model.wv.vectors.shape
:你已经创建了30个单词向量,每个向量有50个维度。
您的代码还远远不能正常工作,因此在尝试将其嵌入到更大的scikit-learn
培训管道中之前,最好先完成一些功能性的Word2Vec
文档教程,以了解正确的使用方法。例如,请参阅gensim:先前版本的OK介绍笔记本
https://github.com/RaRe-Technologies/gensim/blob/ff107d6c5cb50d9ab99999cb898ff0aceb192592/docs/notebooks/word2vec.ipynb
#Vectorizing
vector_maker = TfidfVectorizer(stop_words= set(stopwords.words('english')), vocabulary= vocab) #Vectorizer
train_array = vector_maker.fit_transform(train_tweets).toarray() #Making vector for train tweets
test_array = vector_maker.transform(test_tweets).toarray()
clf = tree.DecisionTreeClassifier()
clf.fit(train_array,train_labels)
predicted= clf.predict(test_array)
print("Accuracy = ",accuracy_score(test_gold_labels,predicted))