2
我的任務是用svm做文本分類,用單詞n-gram作爲特徵。我的代碼是:如何用TF-IDF構造單詞n-gram的訓練矢量
word_dic = ngram.wordNgrams(text, n)
freq_term_vector = [word_dic[gram] if gram in word_dic else 0 for gram in global_vector]
X.append(freq_term_vector)
它運作良好。然而,當我試圖TF-IDF,代碼如下:
freq_term_vector = [word_dic[gram] if gram in word_dic else 0 for gram in global_vector]
tfidf = TfidfTransformer(norm="l2")
tfidf.fit(freq_term_vector)
X.append(tfidf.transform(freq_term_vector).toarray())
訓練部分都可以做,但是當程序運行到預測的一部分,它說
clf.predict(X_test)
File "/usr/lib/python2.7/dist-packages/sklearn/linear_model/base.py", line 223, in predict
scores = self.decision_function(X)
File "/usr/lib/python2.7/dist-packages/sklearn/linear_model/base.py", line 207, in decision_function
dense_output=True) + self.intercept_
File "/usr/lib/python2.7/dist-packages/sklearn/utils/extmath.py", line 83, in safe_sparse_dot
return np.dot(a, b)
ValueError: shapes (1100,1,38) and (1,11) not aligned: 38 (dim 2) != 1 (dim 0)
的訓練方法和預測方法是一樣的。我如何解決這個對齊問題?任何人都可以幫我檢查我的代碼或給我一些想法?
我看到的,真正的問題在於append方法。我嘗試了擴展方法,效果很好。謝謝! – allenwang 2014-10-21 03:23:05