هو لو استخدمات الOne-Hot Encoding في الكود ده بدل OrdinalEnocder عادي لو هيحصل مشكله في قيمه الNULL ؟
ده الكود
def ordinalencoder_data(data):# Select the categorical columns (object type) from the dataframe
categorical_cols = data.select_dtypes(include=['object']).columns
# Create a mask to identify missing values (NaN) in the categorical columns
mask = data[categorical_cols].isna()# Fill missing values with the string 'missing' to ensure no NaN values before encoding
temp_data = data[categorical_cols].fillna('missing')# Initialize the OrdinalEncoder, which will convert categorical values to numerical labels
oe =OrdinalEncoder()# Fit the encoder to the data and transform the categorical columns into numerical labels
encoded_data = oe.fit_transform(temp_data)# Convert the encoded data to a DataFrame, keeping the original column names and specifying the dtype as 'Int64'
encoded_series = pd.DataFrame(encoded_data, columns=categorical_cols, dtype='Int64')# Restore the missing values (NaN) in the original positions, using pd.NA to indicate missing values
encoded_series[mask]= pd.NA
# Update the original dataframe with the encoded columns while preserving the missing value positions
data[categorical_cols]= encoded_series
return data
السؤال
Ali Ahmed55
السلام عليكم
هو لو استخدمات الOne-Hot Encoding في الكود ده بدل OrdinalEnocder عادي لو هيحصل مشكله في قيمه الNULL ؟
ده الكود
3 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.