في الكود التالي، قام بتشفير القيم التصنيفية وملء القيم المفقودة بشكل يدوي دون استخدام مكتبة scikit-learn:
 
	def preprocess_data(df): 
	    """ 
	    Performs transformations on df and returns transformed df. 
	    """ 
	    df["saleYear"] = df.saledate.dt.year 
	    df["saleMonth"] = df.saledate.dt.month 
	    df["saleDay"] = df.saledate.dt.day 
	    df["saleDayOfWeek"] = df.saledate.dt.dayofweek 
	    df["saleDayOfYear"] = df.saledate.dt.dayofyear 
	     
	    df.drop("saledate", axis=1, inplace=True) 
	     
	    # Fill the numeric rows with median 
	    for label, content in df.items(): 
	        if pd.api.types.is_numeric_dtype(content): 
	            if pd.isnull(content).sum(): 
	                # Add a binary column which tells us if the data was missing or not 
	                df[label+"_is_missing"] = pd.isnull(content) 
	                # Fill missing numeric values with median 
	                df[label] = content.fillna(content.median()) 
	     
	        # Fill categorical missing data and turn categories into numbers 
	        if not pd.api.types.is_numeric_dtype(content): 
	            df[label+"_is_missing"] = pd.isnull(content) 
	            # Add +1 to category codes to avoid -1 for missing categories 
	            df[label] = pd.Categorical(content).codes+1 
	     
	    return df 
	 
 
	متى يجب عليّ استخدام هذه الطريقة اليدوية لتشفير القيم وملء القيم المفقودة؟ ومتى يكون من الأفضل استخدام مكتبة scikit-learn، مثل استخدام دالة SimpleImputer لملء القيم المفقودة و OneHotEncoder لتشفير القيم التصنيفية؟
 
	أيضًا، متى يجب تقسيم البيانات قبل القيام بعمليات المعالجة المسبقة (مثل التشفير وملء القيم المفقودة)؟ ومتى يكون من المناسب معالجة البيانات كاملة كما في المثال أعلاه؟
 
	مثال باستخدام scikit-learn:
 
	from sklearn.model_selection import train_test_split 
	from sklearn.impute import SimpleImputer 
	from sklearn.preprocessing import OneHotEncoder 
	from sklearn.compose import ColumnTransformer 
	from sklearn.pipeline import Pipeline
 
	# Split data 
	X_train, X_test, y_train, y_test = train_test_split(df.drop("target", axis=1), df["target"], test_size=0.2)
 
	# Define preprocessing pipeline 
	numeric_features = ['age', 'income'] 
	categorical_features = ['gender']
 
	numeric_transformer = SimpleImputer(strategy='median') 
	categorical_transformer = Pipeline(steps=[ 
	    ('imputer', SimpleImputer(strategy='most_frequent')), 
	    ('onehot', OneHotEncoder(handle_unknown='ignore')) 
	])
 
	# Combine both transformers 
	preprocessor = ColumnTransformer( 
	    transformers=[ 
	        ('num', numeric_transformer, numeric_features), 
	        ('cat', categorical_transformer, categorical_features) 
	    ])
 
	# Apply the transformations on training and testing sets 
	X_train_transformed = preprocessor.fit_transform(X_train) 
	X_test_transformed = preprocessor.transform(X_test)