我该如何处理巨大的矩阵?

时间:2016-01-16 07:53:50

标签: python matrix machine-learning

我正在通过有监督的学习进行主题检测。但是,我的矩阵大小非常大(202180 x 15000),我无法将它们放入我想要的模型中。大多数矩阵由零组成。只有逻辑回归才有效。有没有办法让我可以继续使用相同的矩阵,但是让他们能够使用我想要的模型?我可以用不同的方式创建矩阵吗?

这是我的代码:

import numpy as np
import subprocess
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import LogisticRegression

from sklearn import metrics

def run(command):
    output = subprocess.check_output(command, shell=True)
    return output

加载词汇

 f = open('/Users/win/Documents/wholedata/RightVo.txt','r')
    vocab_temp = f.read().split()
    f.close()
    col = len(vocab_temp)
    print("Training column size:")
    print(col)

创建列车矩阵

row = run('cat '+'/Users/win/Documents/wholedata/X_tr.txt'+" | wc -l").split()[0]
print("Training row size:")
print(row)
matrix_tmp = np.zeros((int(row),col), dtype=np.int64)
print("Train Matrix size:")
print(matrix_tmp.size)

label_tmp = np.zeros((int(row)), dtype=np.int64)
f = open('/Users/win/Documents/wholedata/X_tr.txt','r')
count = 0
for line in f:
    line_tmp = line.split()
    #print(line_tmp)
    for word in line_tmp[0:]:
        if word not in vocab_temp:
            continue
        matrix_tmp[count][vocab_temp.index(word)] = 1
    count = count + 1
f.close()
print("Train matrix is:\n ")
print(matrix_tmp)
print(label_tmp)
print("Train Label size:")
print(len(label_tmp))

f = open('/Users/win/Documents/wholedata/RightVo.txt','r')
vocab_tmp = f.read().split()
f.close()
col = len(vocab_tmp)
print("Test column size:")
print(col)

制作测试矩阵

row = run('cat '+'/Users/win/Documents/wholedata/X_te.txt'+" | wc -l").split()[0]
print("Test row size:")
print(row)
matrix_tmp_test = np.zeros((int(row),col), dtype=np.int64)
print("Test matrix size:")
print(matrix_tmp_test.size)

label_tmp_test = np.zeros((int(row)), dtype=np.int64)

f = open('/Users/win/Documents/wholedata/X_te.txt','r')
count = 0
for line in f:
    line_tmp = line.split()
    #print(line_tmp)
    for word in line_tmp[0:]:
        if word not in vocab_tmp:
            continue
        matrix_tmp_test[count][vocab_tmp.index(word)] = 1
    count = count + 1
f.close()
print("Test Matrix is: \n")
print(matrix_tmp_test)
print(label_tmp_test)

print("Test Label Size:")
print(len(label_tmp_test))

xtrain=[]
with open("/Users/win/Documents/wholedata/Y_te.txt") as filer:
    for line in filer:
        xtrain.append(line.strip().split())
xtrain= np.ravel(xtrain)
label_tmp_test=xtrain

ytrain=[]
with open("/Users/win/Documents/wholedata/Y_tr.txt") as filer:
    for line in filer:
        ytrain.append(line.strip().split())
ytrain = np.ravel(ytrain)
label_tmp=ytrain

加载监督模型

model = LogisticRegression()
model = model.fit(matrix_tmp, label_tmp)
#print(model)
print("Entered 1")
y_train_pred = model.predict(matrix_tmp_test)
print("Entered 2")
print(metrics.accuracy_score(label_tmp_test, y_train_pred))

1 个答案:

答案 0 :(得分:5)

您可以使用名为稀疏矩阵的scipy包中提供的特定数据结构:http://docs.scipy.org/doc/scipy/reference/sparse.html

根据definition

  

稀疏矩阵只是一个具有大量零值的矩阵。相反,许多或大多数条目非零的矩阵被认为是密集的。什么构成稀疏矩阵没有严格的规则,所以我们要说如果利用稀疏矩阵有一些好处,矩阵是稀疏的。另外,存在各种稀疏矩阵格式,其被设计为利用不同的稀疏模式(稀疏矩阵中的非零值的结构)以及用于访问和操纵矩阵条目的不同方法。