gpt4 book ai didi

python - 用于文档分类的 scipy/sklearn 稀疏矩阵分解

转载 作者:太空宇宙 更新时间:2023-11-04 10:31:27 25 4
gpt4 key购买 nike

我正在尝试对大型语料库(400 万文档)进行文档分类,并在使用标准 scikit-learn 方法时不断遇到内存错误。在清理/提取我的数据后,我有一个非常稀疏的矩阵,大约有 100 万个单词。我的第一个想法是使用 sklearn.decomposition.TruncatedSVD,但由于内存错误,我无法使用足够大的 k 执行 .fit() 操作(我能做的最大只占数据方差的 25% ).我尝试遵循 sklearn 分类 here , 但在进行 KNN 分类时内存仍然不足。 我想手动进行核外矩阵变换以将 PCA/SVD 应用于矩阵以降低维数,但需要一种方法来首先计算特征向量。我希望使用scipy.sparse.linalg.eigs 有没有一种方法可以计算特征向量矩阵以完成下面显示的代码?

from sklearn.feature_extraction.text import TfidfVectorizer
import scipy.sparse as sp
import numpy as np
import cPickle as pkl
from sklearn.neighbors import KNeighborsClassifier

def pickleLoader(pklFile):
try:
while True:
yield pkl.load(pklFile)
except EOFError:
pass

#sample docs
docs = ['orange green','purple green','green chair apple fruit','raspberry pie banana yellow','green raspberry hat ball','test row green apple']
classes = [1,0,1,0,0,1]
#first k eigenvectors to keep
k = 3

#returns sparse matrix
tfidf = TfidfVectorizer()
tfs = tfidf.fit_transform(docs)

#write sparse matrix to file
pkl.dump(tfs, open('pickleTest.p', 'wb'))



#NEEDED - THIS LINE THAT CALCULATES top k eigenvectors
del tfs

x = np.empty([len(docs),k])

#iterate over sparse matrix
with open('D:\\GitHub\\Avitro-Classification\\pickleTest.p') as f:
rowCounter = 0
for dataRow in pickleLoader(f):
colCounter = 0
for col in k:
x[rowCounter, col] = np.sum(dataRow * eingenvectors[:,col])
f.close()

clf = KNeighborsClassifier(n_neighbors=10)
clf.fit(x, k_class)

如有任何帮助或指导,我们将不胜感激!如果有更好的方法来做到这一点,我很乐意尝试不同的方法,但我想在这个大型稀疏数据集上尝试 KNN,最好使用一些降维(这在我运行的小型测试数据集上表现非常好 -我不想因为愚蠢的内存限制而失去我的表现!)

编辑:这是我第一次尝试运行的代码,它引导我走上了执行我自己的核外稀疏 PCA 实现的道路。任何修复此内存错误的帮助都会使这更容易!

from sklearn.decomposition import TruncatedSVD
import pickle

dataFolder = 'D:\\GitHub\\project\\'

# in the form of a list: [word sample test word, big sample test word test, green apple test word]
descWords = pickle.load(open(dataFolder +'descriptionWords.p'))

vectorizer = TfidfVectorizer()
X_words = vectorizer.fit_transform(descWords)

print np.shape(X_words)

del descWords
del vectorizer

svd = TruncatedSVD(algorithm='randomized', n_components=50000, random_state=42)
output = svd.fit_transform(X_words)

输出:

(3995803, 923633)
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<ipython-input-27-c0db86bd3830> in <module>()
16
17 svd = TruncatedSVD(algorithm='randomized', n_components=50000, random_state=42)
---> 18 output = svd.fit_transform(X_words)

C:\Python27\lib\site-packages\sklearn\decomposition\truncated_svd.pyc in fit_transform(self, X, y)
173 U, Sigma, VT = randomized_svd(X, self.n_components,
174 n_iter=self.n_iter,
--> 175 random_state=random_state)
176 else:
177 raise ValueError("unknown algorithm %r" % self.algorithm)

C:\Python27\lib\site-packages\sklearn\utils\extmath.pyc in randomized_svd(M, n_components, n_oversamples, n_iter, transpose, flip_sign, random_state, n_iterations)
297 M = M.T
298
--> 299 Q = randomized_range_finder(M, n_random, n_iter, random_state)
300
301 # project M to the (k + p) dimensional space using the basis vectors

C:\Python27\lib\site-packages\sklearn\utils\extmath.pyc in randomized_range_finder(A, size, n_iter, random_state)
212
213 # generating random gaussian vectors r with shape: (A.shape[1], size)
--> 214 R = random_state.normal(size=(A.shape[1], size))
215
216 # sampling the range of A using by linear projection of r

C:\Python27\lib\site-packages\numpy\random\mtrand.pyd in mtrand.RandomState.normal (numpy\random\mtrand\mtrand.c:9968)()

C:\Python27\lib\site-packages\numpy\random\mtrand.pyd in mtrand.cont2_array_sc (numpy\random\mtrand\mtrand.c:2370)()

MemoryError:

最佳答案

scikit-learn 0.15.2 中未实现稀疏数据的核外 SVD 或 PCA。您可能想尝试 gensim相反。

编辑:我忘记在我的第一个回复中指定“关于稀疏数据”。

关于python - 用于文档分类的 scipy/sklearn 稀疏矩阵分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26249367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com