我创建了一个大小为16 ^ 4和16 ^ 8的csr_matrix。但是我需要更新csr_matrix中的值,那么如何更新稀疏矩阵中的值。
我也尝试过twoByte.toarray()[i] += 1
,twoByte.toarray()[0][i] += 1
和twoByte[0].toarray()[i] += 1
,但是它不起作用。下面是代码段。
feature_matrix_two = csr_matrix((len(files),16**4),dtype=int)
feature_matrix_four = csr_matrix((len(files),16**6),dtype=int)
k=0
byte_feature_file=open('bigramresult.csv','w+')
for file in files:
byte_feature_file.write(file+",")
if(file.endswith("txt")):
with open('byteFiles/'+file,"r") as byte_code:
twoByte = csr_matrix((1,16**4),dtype = int)
fourByte = csr_matrix((1,16**8),dtype = int)
for row in byte_code:
codes = row.rstrip().split(" ")
codes_2g = codes[:-1]
codes_4g = codes[:-2]
for i in range(len(codes_2g)):
codes_2g[i] += codes[i+1]
for i in range(len(codes_4g)):
codes_4g[i] += codes[i+1]+codes[i+2]
twoByteCode = []
for i in codes_2g:
if '??' not in i:
twoByteCode += [int(i,16)]
fourByteCode = []
for i in codes_4g:
if '??' not in i:
fourByteCode += [int(i,16)]
for i in twoByteCode:
twoByte[i] += 1
for i in fourByteCode:
fourByte[i] += 1
byte_code.close()
feature_matrix_two[k] = twoByte
feature_matrix_four[k] = fourByte
for i in feature_matrix_two[k]:
byte_feature_file.write(str(i)+",")
for i in feature_matrix_four[k]:
byte_feature_file.write(str(i)+",")
byte_feature_file.write("\n")
k+=1
答案 0 :(得分:0)
从代码中,我认为您不需要稀疏矩阵,可以使用dict对象,例如:
from collections import defaultdict
twoByte = defaultdict(int)
fourByte = defaultdict(int)