साझा मेंबरशिप खोजें: Transposition
जैसा कि आप ने देखा होगा, जब आप किसी ग्राफ़ को sparse matrix representation में बदलते हैं, तो उसका मेटाडेटा खो जाता है। अब आप सीखेंगे कि वह मेटाडेटा वापस कैसे जोड़ें ताकि साझा मेंबरशिप के बारे में और जानकारी मिल सके।
पिछले अभ्यास में आपने जो user_matrix निकाला था, वह आपके वर्कस्पेस में प्रीलोड किया गया है।
यहाँ np.where() फंक्शन काम आएगा। यह ऐसे काम करता है: मान लीजिए कोई array है, a = [1, 5, 9, 5]. अगर आप वे इंडेक्स लेना चाहें जहाँ वैल्यू 5 है, तो आप लिख सकते हैं idxs = np.where(a == 5)। यह आपको एक ट्यूपल में array लौटाता है, (array([1, 3]),)। इन इंडेक्सों तक पहुँचने के लिए, आपको ट्यूपल में [0] से इंडेक्स करना होगा, यानी idxs[0]।
यह अभ्यास पाठ्यक्रम का हिस्सा है
इंटरमीडिएट Network Analysis in Python
अभ्यास निर्देश
- उन लोगों के नाम निकालें जो सबसे ज़्यादा क्लबों के मेंबर थे।
- इसके लिए, पहले
user_matrixपर.diagonal()मेथड सेdiagनिकालें। - फिर
np.where()का उपयोग करके वे इंडेक्स चुनें जहाँdiagdiag.max()के बराबर हो। यह एक ट्यूपल लौटाता है: सुनिश्चित करें कि आप[0]से ट्यूपल को इंडेक्स करके सही इंडेक्स लेते हैं। indicesपर इटरेट करें और दिए गएprint()फंक्शन सेpeople_nodesके प्रत्येक इंडेक्सiको प्रिंट करें।
- इसके लिए, पहले
- डायगोनल को शून्य पर सेट करें और मैट्रिक्स को "coordinate matrix format" में कनवर्ट करें। यह कोड उत्तर में दिया गया है।
- उन यूज़र्स के जोड़े खोजें जिन्होंने सबसे ज़्यादा क्लब साथ शेयर किए।
np.where()का उपयोग करके वे इंडेक्स लें जहाँusers_coo.datausers_coo.data.max()के बराबर हो।indices2पर इटरेट करें और प्रत्येक इंडेक्सidxके लिएpeople_nodeकेusers_coo.rowऔरusers_coo.colसे वैल्यूज़ प्रिंट करें।
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
import numpy as np
# Find out the names of people who were members of the most number of clubs
diag = ____
indices = np.where(____ == ____)[0]
print('Number of clubs: {0}'.format(diag.max()))
print('People with the most number of memberships:')
for i in indices:
print('- {0}'.format(____))
# Set the diagonal to zero and convert it to a coordinate matrix format
user_matrix.setdiag(0)
users_coo = user_matrix.tocoo()
# Find pairs of users who shared membership in the most number of clubs
indices2 = np.where(____ == ____)[0]
print('People with most number of shared memberships:')
for idx in indices2:
print('- {0}, {1}'.format(people_nodes[____.____[____]], people_nodes[____.____[____]]))