Print (TRAIN_SET.TDM) Print (Type (TRAIN_SET.TDM))
The output gets:
(0, 3200) 0.264940780338(0,1682) 0.356545827856(0,3875) 0.404535449364(0,2638) 0.375094236628(0,2643) 0.420086333071(0,558) 0.332314202381(0,2383) 0.215711023304(0,3233) 0.304884643652(0,3848) 0.26822694041 (1, 1682) 0.0679433740085 (1, 3586) 0.186001809282 (1, 1748) 0.224453998729 (1, 4369) 0.217962362491 (1, 4102) 0.321977101868 (1, 3717) 0.11571865147 (1, 1849) 0.23976007391 (1, 3019) 0.105831301914 (1, 2731) 0.133236987271 (1, 2284) 0.158959982269 (1, 1129) 0.224453998729 (1, 4004) 0.14716429302 (1, 1113) 0.224453998729 (1, 1239) 0.23282317344 (1, 4439) 0.17621324335 (1, 4075) 0.111234138548 : : (3297, 4296) 0.189022497666 (3297, 1273) 0.173257613112 (3297, 611) 0.189022497666 (3297, 1639) 0.201945480138 (3297, 1401) 0.196076146399 (3297, 800) 0.193531186809 (3297, 4442) 0.213804760507 (3298, 2383) 0.115351969953 (3298, 3848) 0.143434978411 (3298, 3480) 0.166989458436 (3298, 767) 0.208015125433 (3298, 3836) 0.115469714921 (3298, 3877) 0.132381892057 (3298, 4387) 0.302243669544 (3298, 2967) 0.182430066726 (3298, 4184) 0.170734583655 (3298, 3878) 0.131142324027 (3298, 3381) 0.202336034891 (3298, 3959) 0.299487688552 (3298, 1392) 0.257499357524 (3298, 3039) 0.266066529253 (3298, 3599) 0.27026191686 (3298, 4289) 0.302243669544 (3298, 484) 0.36124988755 (3298, 2037) 0.36124988755<class 'Scipy.sparse.csr.csr_matrix'>
describe this variable TRAIN_SET.TDM is a Scipy.sparse.csr.csr_matrix, similar to the sparse matrix, the output is the matrix of 0 of the row and column coordinates and values, and now we want to pick out the highest value in each row of the K term.
First we know a convenient function for sparse matrices:
# outputs the row and column coordinates of a non-0 element nonzero=Train_set.tdm.nonzero () #Nonzero is a tuple print(type ( Nonzero)) print(nonzero[0] )print(nonzero[1]) Print(nonzero[1][0])
The output is:
<class'tuple'>[ 0 0 3298 3298 3298][3200 1682 3875 ..., 4289 484 2037]3200
In fact, TRAIN_SET.TDM is my text mining TF-IDF after the weight matrix,
I'm going to pick out the 76 most weighted words in the Daily Record, from the big to the small, the dictionary number of these words to Excel
I use a class to store two of these non-0 lattice messages: One is the word weight information, one is the dictionary number of the word,
Lis to save a record of the ownership of 0 words of information, gather is the collection of all Lis, the code is as follows:
classobj:def __init__(self): Self.key=0 Self.weight=0.0k=0#K is used to record if a record is over.lis=[] Gather=[] P=-1#p is used to count, each walk over the loop +1 forIinchNONZERO[0]:#I do not necessarily +1 per cycle, it is nonzero "0" in the number, do not understand can be seen before the output of the nonzero "0"P=p+1Print(i)ifk==i:a=obj () a.key=NONZERO[1][P]#The dictionary number of the word is that it belongs to the first column.A.weight=train_set.tdm[i,nonzero[1][p]] Lis.append (a)Else: Lis.sort (Key=LambdaObj:obj.weight, Reverse=true)#To sort a class object within a linked list #print (LIS)gather.append (LIS) whileK <i:k=k+1Lis=[] A=obj () a.key=nonzero[1][p] A.weight=train_set.tdm[i,nonzero[1][p]] Lis.append (a) gather.append (LIS)
And the end is output to Excel.
Myexcel =XLWT. Workbook () sheet= Myexcel.add_sheet ('sheet') #SI,SJ indicates the output to the first few rowsSi=-1SJ=-1 forIinchGather:si=si+1 forJinchI:SJ=sj+1Sheet.write (Si,sj,str (j.key)) whilesj<=76: SJ=sj+1Sheet.write (SI,SJ,'-1')#If you don't have that many phrases, use-1 instead .Sj=-1Myexcel.save ("Attribute76.xls")
is as follows:
Python sparse matrix Gets the value of the maximum K entry for each column, sorting the class object within the list (Scipy.sparse.csr.csr_matrix)