Contents
Data Preparation for Modeling
Models need data in a specific form. This topic shows you how to turn your cleaned data into a format that machine learning algorithms can use, so your model gets the best chance to learn well.
Selecting Useful Columns
Not every column helps a model. Some are identifiers, like customer ID, that carry no useful pattern. Others may be repeated or have too many missing values. Remove columns that do not add value.
Use your business knowledge and EDA findings to choose columns. Ask which pieces of information could really affect the result you want to predict. Keep those and drop the rest.
X = df[["age", "income", "visits"]]
y = df["churned"]Here, X holds the input features and y holds the target. Separating them clearly is the first step in preparing data for supervised learning.
Encoding Categorical Data
Most models only understand numbers, so text categories must be converted. One hot encoding creates a new column of 0s and 1s for each category. It works well for values with no natural order, like city or color.
X = pd.get_dummies(X, columns=["city"], drop_first=True)Label encoding gives each category a number. Use it only when the categories have a real order, such as low, medium, and high. Otherwise, the model may think one category is greater than another.
Be careful with columns that have many unique values. They can create too many new columns. In that case, group rare values into an "Other" category first.
Scaling Numeric Data
Numeric columns often have very different ranges. Age may go from 18 to 70, while income may go up to 100000. Some models are sensitive to this, so scaling helps put all columns on a similar range.
Standardization changes values to have a mean of 0 and a standard deviation of 1. Min-max scaling squeezes values between 0 and 1.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)Not every model needs scaling. Tree-based models usually do not. However, it is a good habit to know when it matters.
Splitting Data into Training and Test Sets
To know if your model works on new data, you must test it on data it has not seen. So you split your dataset into two parts. The training set teaches the model, and the test set checks it.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)A common split is 80 percent for training and 20 percent for testing. The random_state makes the split repeatable, so you get the same result each time.
Never use the test set while building or tuning your model. Keep it separate until the final check. This keeps your evaluation honest.
Data Science Lifecycle Tutorial
Wesline Rinisha








