Feature Engineering and Feature Selection
Good features often matter more than the choice of algorithm. This topic shows you how to create better features from raw data and how to select the ones that truly help your model.
Creating New Features
Feature engineering means building new columns that make patterns easier for a model to see. You can combine columns, extract parts of values, or create counts and ratios.
df["price_per_unit"] = df["total_price"] / df["quantity"]
df["order_month"] = df["order_date"].dt.month
df["is_weekend"] = df["order_date"].dt.dayofweek >= 5Date columns are a rich source of features. You can pull out the year, month, weekday, and hour. You can also calculate the time between two events, such as days since the last purchase.
Use domain knowledge to guide you. Ask what real-world factors might affect your target and try to capture them. Each feature should have a reason to exist.
Encoding and Scaling Techniques
Beyond basic one hot encoding, there are other options for categories. Target encoding replaces each category with the average target value for that category. It works well for columns with many unique values, but it can cause leakage if not done carefully.
Frequency encoding replaces each category with how often it appears. It is simple and keeps the number of columns small.
For scaling, standardization and min-max scaling are the most common. Use a robust scaler when your data has many outliers, since it relies on the median and IQR rather than the mean.
Filter, Wrapper, and Embedded Selection Methods
Feature selection means choosing the most useful features and dropping the rest. Fewer features make models faster, simpler, and often more accurate. There are three main groups of methods.
Filter methods score each feature using statistics, such as correlation or chi-square, before any model is trained. They are fast and simple. Wrapper methods, like Recursive Feature Elimination, train a model many times with different feature sets to find the best group.
Embedded methods select features during model training. Lasso regression and tree-based models are examples. They give a good balance of speed and quality.
from sklearn.feature_selection import SelectKBest, f_classif
selector = SelectKBest(f_classif, k=5)
X_new = selector.fit_transform(X_train, y_train)Reducing Features with PCA
Principal Component Analysis, or PCA, reduces the number of features by combining them into a smaller set of new ones called components. Each component captures as much of the data's variation as possible.
PCA is useful when you have many correlated columns. It can speed up training and reduce noise. It is also used to visualize high-dimensional data in two or three dimensions.
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)The downside is that components are hard to explain, because they mix many original features. Use PCA when performance matters more than interpretability, and always scale the data first.
Data Science Lifecycle Tutorial
Wesline Rinisha








