श्रेणीबद्ध मानों का इम्प्यूटेशन

Python में Missing Data से निपटना

Suraj Donthi

Deep Learning & Computer Vision Consultant

श्रेणीबद्ध मानों की जटिलता

  • ज्यादातर श्रेणीबद्ध मान strings होते हैं
  • strings पर ऑपरेशन नहीं किए जा सकते
  • इसलिए strings को numeric में बदलकर/एन्कोड कर इम्प्यूट करें
Python में Missing Data से निपटना

कन्वर्ज़न तकनीकें

One-hot encoder
Color Color_Red Color_Green Color_Blue
Red 1 0 0
Green 0 1 0
Blue 0 0 1
Red 1 0 0
Blue 0 0 1
Blue 0 0 1
Ordinal encoder
Color Value
Red 0
Green 1
Blue 2
Red 0
Blue 2
Blue 2
Python में Missing Data से निपटना

इम्प्यूटेशन तकनीकें

  • सबसे अधिक बार आने वाली category से भरें
  • KNN जैसे स्टैटिस्टिकल मॉडल से इम्प्यूट करें
Python में Missing Data से निपटना

यूज़र प्रोफ़ाइल डेटा

users = pd.read_csv('userprofile.csv')
users.head()
    smoker    drink_level    dress_preference ambience    hijos        activity     budget
0    False    abstemious        informal      family    independent    student      medium
1    False    abstemious        informal      family    independent    student      low
2    False    social drinker    formal        family    independent    student      low
3    False    abstemious        informal      family    independent    professional medium
4    False    abstemious     no preference    family    independent    student      medium
Python में Missing Data से निपटना

Ordinal एन्कोडिंग

from sklearn.preprocessing import OrdinalEncoder

# Create Ordinal Encoder ambience_ord_enc = OrdinalEncoder() # Select non-null values in ambience ambience = users['ambience'] ambience_not_null = ambience[ambience.notnull()] reshaped_vals = ambience_not_null.values.reshape(-1, 1)
# Encode the non-null values of ambience encoded_vals = ambience_ord_enc.fit_transform(reshaped_vals)
# Replace the ambience column with ordinal values users.loc[ambience.notnull(), 'ambience'] = np.squeeze(encoded_vals)
Python में Missing Data से निपटना

Ordinal एन्कोडिंग

# Create dictionary for Ordinal encoders
ordinal_enc_dict = {}

# Loop over columns to encode
for col_name in users:
    # Create ordinal encoder for the column
    ordinal_enc_dict[col_name] = OrdinalEncoder()
    col = users[col_name]

    # Select the non-null values in the column
    col_not_null = col[col.notnull()]
    reshaped_vals = col_not_null.values.reshape(-1, 1)

    # Encode the non-null values of the column
    encoded_vals = ordinal_enc_dict[col_name].fit_transform(reshaped_vals)

    # Replace the values in the column with ordinal values
    users.loc[col.notnull(), col_name] = np.squeeze(encoded_vals)
Python में Missing Data से निपटना

KNN से इम्प्यूट करना

users_KNN_imputed = users.copy(deep=True)

# Create KNN imputer KNN_imputer = KNN()
users_KNN_imputed.iloc[:, :] = np.round(KNN_imputer.fit_transform(users))
for col_name in users_KNN_imputed: # Reshape the values to 2-dimensions to # avoid errors while storing in the DataFrame reshaped = users_KNN_imputed[col_name].values.reshape(-1, 1) users_KNN_imputed[col_name] = \ ordinal_enc_dict[col_name].inverse_transform(reshaped)
Python में Missing Data से निपटना

सारांश

श्रेणीबद्ध मानों का इम्प्यूट करने के चरण

  • non-missing श्रेणीबद्ध कॉलम्स को ordinal मानों में बदलें
  • ordinal DataFrame में missing मान इम्प्यूट करें
  • ordinal से वापस श्रेणीबद्ध मानों में बदलें
Python में Missing Data से निपटना

अभ्यास करते हैं!

Python में Missing Data से निपटना

Preparing Video For Download...