0% found this document useful (0 votes)
4 views2 pages

Preprocessing Code

This document outlines a data preprocessing workflow for a house prices dataset using Python. It includes steps for loading the dataset, checking and handling missing values, encoding categorical variables, separating features and target, and performing a train-test split. Additionally, it suggests feature scaling for improved model performance.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Preprocessing Code

This document outlines a data preprocessing workflow for a house prices dataset using Python. It includes steps for loading the dataset, checking and handling missing values, encoding categorical variables, separating features and target, and performing a train-test split. Additionally, it suggests feature scaling for improved model performance.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# 1.

Import Libraries

import pandas as pd

import numpy as np

# 2. Load Dataset

df = pd.read_csv("house_prices_500_realistic.csv")

print("Original Dataset Shape:", [Link])

print([Link]())

# 3. Check Missing Values

print("\nMissing Values Count:")

print([Link]().sum())

# 4. Handle Missing Values

# Numerical columns → Fill with Mean

num_cols = ['Size_sqft', 'Bedrooms', 'Property_Age', 'Distance_City_km', 'Price_Lakhs']

df[num_cols] = df[num_cols].fillna(df[num_cols].mean())

# Categorical columns → Fill with Mode (most frequent value)

cat_cols = ['Location', 'Furnished', 'Parking']

for col in cat_cols:

df[col] = df[col].fillna(df[col].mode()[0])

# 5. Encode Categorical Variables (One-Hot Encoding)

df_encoded = pd.get_dummies(df, drop_first=True)

# 6. Feature and Target Separation

X = df_encoded.drop('Price_Lakhs', axis=1)
y = df_encoded['Price_Lakhs']

# 7. Train-Test Split

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

# 8. Feature Scaling (Optional but Recommended)

from [Link] import StandardScaler

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)

X_test = [Link](X_test)

# Final Check

print("\nPreprocessing Completed!")

print("Training Shape:", X_train.shape)

print("Testing Shape:", X_test.shape)

You might also like