Categorical कॉलम का ऑर्डिनल एन्कोडिंग
Categorical वैल्यूज़ को इम्प्यूट करना, numerical वैल्यूज़ की तुलना में कुछ अतिरिक्त स्टेप्स माँगता है. सबसे पहले इन्हें numerical वैल्यूज़ में बदलना पड़ता है, क्योंकि strings पर statistical operations नहीं किए जा सकते.
आप user profile डेटासेट का उपयोग करेंगे जिसमें एक रेस्टोरेंट द्वारा रिकॉर्ड की गई कस्टमर प्रेफ़रेंसेज़ और चॉइसेज़ हैं. इसमें केवल categorical फीचर्स हैं. इस अभ्यास में, आप sklearn के OrdinalEncoder का उपयोग करके categorical कॉलम 'ambience' को numerical में बदलेंगे. DataFrame आपके लिए users के रूप में लोड किया गया है. फ़ंक्शन OrdinalEncoder() भी लोड किया गया है.
users DataFrame के head() और tail() आपके लिए प्रिंट कर दिए गए हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Missing Data से निपटना
अभ्यास निर्देश
- ऑर्डिनल एन्कोडर ऑब्जेक्ट बनाएँ और उसे
ambience_ord_encको असाइन करें. usersमें'ambience'कॉलम की non-missing वैल्यूज़ चुनें.ambience_not_nullको आकार(-1, 1)में reshape करें.ambienceकी non-missing वैल्यूज़ को उसके encoded वैल्यूज़ से बदल दें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Set col_name to 'ambience'
col_name = 'ambience'
# Create Ordinal encoder
ambience_ord_enc = ___
# Select non-null values of ambience column in users
ambience = users[col_name]
ambience_not_null = ___
# Reshape ambience_not_null to shape (-1, 1)
reshaped_vals = ___
# Select the non-null values for the column col_name in users and store the encoded values
encoded_vals = ambience_ord_enc.fit_transform(reshaped_vals)
users.loc[___, col_name] = np.squeeze(encoded_vals)