Restricted Levenshtein
आपने देखा कि stringdist पैकेज Levenshtein distance का एक वेरिएशन भी implements करता है जिसे Restricted Damerau-Levenshtein distance कहा जाता है, और आप इसे आज़माना चाहते हैं। आप लेसन की ही लॉजिक का पालन करेंगे: इसे एक कस्टम फंक्शन में wrap करके distance मैट्रिक्स को पहले से precompute करेंगे, फिर एक local outlier factor anomaly detector फिट करेंगे। आप परफॉर्मेंस को accuracy_score() से नापेंगे जो accuracy() नाम से उपलब्ध है। आपके पास stringdist, numpy बतौर np, scipy.spatial.distance से pdist() और squareform(), और LocalOutlierFactor बतौर lof की पहुँच भी है। डेटा पहले से एक pandas dataframe के रूप में दो कॉलम label और sequence के साथ preloaded है, और इसमें दो क्लास हैं: IMMUNE SYSTEM और VIRUS.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में मशीन लर्निंग वर्कफ़्लो डिज़ाइन करना
अभ्यास निर्देश
- एक फंक्शन लिखिए जिसका इनपुट
uऔरvहो, जिनमें से प्रत्येक एक string वाला array है, और जो इन दोनों strings परrdlevenshtein()अप्लाई करे. proteinsकीsequenceकॉलम को पहलेnumpyarray में कास्ट करके, फिर.reshape()का उपयोग करके reshape कीजिए.my_rdlevenshtein()का उपयोग करकेsequencesके लिए square distance मैट्रिक्स compute कीजिए, और उस परlofफिट कीजिए.predsऔरproteins['label']को booleans में बदलकर accuracy compute कीजिए, ताकि यह दर्शाएँ कि कोई protein वायरस है या नहीं.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Wrap the RD-Levenshtein metric in a custom function
def my_rdlevenshtein(u, v):
return ____.rdlevenshtein(____, ____)
# Reshape the array into a numpy matrix
sequences = ____(proteins['seq']).____(-1, 1)
# Compute the pairwise distance matrix in square form
M = ____
# Run a LoF algorithm on the precomputed distance matrix
preds = lof(metric=____).____(M)
# Compute the accuracy of the outlier predictions
print(accuracy(____, ____))