2014-10-13 9 views
5

मैं दस्तावेज़ क्लस्टरिंग के लिए अजगर scikit-learn उपयोग कर रहा हूँ और मैं एक विरल मैट्रिक्स एक dict वस्तु में संग्रहीत है:परिवर्तित अजगर विरल मैट्रिक्स dict विरल मैट्रिक्स SciPy को

उदाहरण के लिए:

doc_term_dict = { ('d1','t1'): 12,    \ 
        ('d2','t3'): 10,    \ 
        ('d3','t2'): 5    \ 
        }       # from mysql data table 
<type 'dict'> 

मैं चाहता हूँ क्लस्टरिंग करने के लिए scikit-learn का उपयोग करने के लिए जहां इनपुट मैट्रिक्स प्रकार scipy.sparse.csr.csr_matrix

उदाहरण:

(0, 2164) 0.245793088885 
(0, 2076) 0.205702177467 
(0, 2037) 0.193810934784 
(0, 2005) 0.14547028437 
(0, 1953) 0.153720023365 
... 
<class 'scipy.sparse.csr.csr_matrix'> 

मैं एक तरह से इस सीएसआर-मैट्रिक्स को dict कन्वर्ट करने के लिए नहीं मिल सकता है (मैं scipy इस्तेमाल नहीं किया है।)

+0

धन्यवाद @ दत्तमान ने मेरा प्रश्न संपादित किया – chent

उत्तर

5

सुंदर सरल। सबसे पहले शब्दकोश को पढ़ें और कुंजी को उचित पंक्ति और कॉलम में कनवर्ट करें। Scipy sparse matrices के लिए COO-rdinate format का समर्थन करता है (और इस उद्देश्य के लिए अनुशंसा करता है)।

पास यह data, row, और column, जहां A[row[k], column[k] = data[k] (सभी कश्मीर के लिए) मैट्रिक्स परिभाषित करता है। फिर Scipy सीएसआर में रूपांतरण करते हैं।

कृपया जांचें कि मेरे पास जिस तरह से आप चाहते हैं, पंक्तियों और स्तंभ हैं, मैं उन्हें स्थानांतरित कर सकता हूं। मैंने यह भी माना कि इनपुट 1-अनुक्रमित होगा।

प्रिंट नीचे दी गई मेरी कोड:

(0, 0)  12 
(1, 2)  10 
(2, 1)  5 

कोड:

#!/usr/bin/env python3 
#http://stackoverflow.com/questions/26335059/converting-python-sparse-matrix-dict-to-scipy-sparse-matrix 

from scipy.sparse import csr_matrix, coo_matrix 

def convert(term_dict): 
    ''' Convert a dictionary with elements of form ('d1', 't1'): 12 to a CSR type matrix. 
    The element ('d1', 't1'): 12 becomes entry (0, 0) = 12. 
    * Conversion from 1-indexed to 0-indexed. 
    * d is row 
    * t is column. 
    ''' 
    # Create the appropriate format for the COO format. 
    data = [] 
    row = [] 
    col = [] 
    for k, v in term_dict.items(): 
     r = int(k[0][1:]) 
     c = int(k[1][1:]) 
     data.append(v) 
     row.append(r-1) 
     col.append(c-1) 
    # Create the COO-matrix 
    coo = coo_matrix((data,(row,col))) 
    # Let Scipy convert COO to CSR format and return 
    return csr_matrix(coo) 

if __name__=='__main__': 
    doc_term_dict = { ('d1','t1'): 12,    \ 
       ('d2','t3'): 10,    \ 
       ('d3','t2'): 5    \ 
       } 
    print(convert(doc_term_dict)) 
2

हम कर सकते हैं @ Unapiedra की (उत्कृष्ट) का जवाब थोड़ा और विरल:

from scipy.sparse import csr_matrix 
def _dict_to_csr(term_dict): 
    term_dict_v = list(term_dict.itervalues()) 
    term_dict_k = list(term_dict.iterkeys()) 
    shape = list(repeat(np.asarray(term_dict_k).max() + 1,2)) 
    csr = csr_matrix((term_dict_v, zip(*term_dict_k)), shape = shape) 
    return csr 
0

@carsonc के समान , लेकिन पाइथन 3.X:

from scipy.sparse import csr_matrix 
def _dict_to_csr(term_dict): 
    term_dict_v = term_dict.values() 
    term_dict_k = term_dict.keys() 
    term_dict_k_zip = zip(*term_dict_k) 
    term_dict_k_zip_list = list(term_dict_k_zip) 

    shape = (len(term_dict_k_zip_list[0]), len(term_dict_k_zip_list[1])) 
    csr = csr_matrix((list(term_dict_v), list(map(list, zip(*term_dict_k)))), shape = shape) 
    return csr 
संबंधित मुद्दे