# Create a copy of the data to avoid modifying the original dataset
data_encoded = data_train.copy()# Iterate over columns with categorical (textual) datafor col in data_encoded.select_dtypes(include=['object']).columns:# Create a mask to identify missing values in the column
mask = data_encoded[col].isna()# Identify missing values# Temporarily replace missing values with the string 'missing'
temp_data = data_encoded[col].fillna('missing')# Replace missing values with 'missing'# Initialize a LabelEncoder object to transform categorical data
le =LabelEncoder()# Create a new LabelEncoder object for each column
encoded_data = le.fit_transform(temp_data)# Apply label encoding# Convert encoded data to float type to allow NaN values
encoded_data = encoded_data.astype(float)# Convert values to float to allow NaN# Restore the missing values back to their original positions
encoded_data[mask]= np.nan # Restore missing values to their original positions
encoded_data = encoded_data.astype(int)# Update the column with the encoded values
data_encoded[col]= encoded_data # Update the column with encoded values
السؤال
Ali Ahmed55
السلام عليكم
انا هنا عاوز الاعمد تكون int مش float ؟
ده الكود
3 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.