0% found this document useful (0 votes)
8 views5 pages

Car Price Prediction with ML Workflow

The document outlines a machine learning workflow for predicting car prices, detailing the steps from problem formulation to model deployment using linear regression. It emphasizes data preprocessing, exploratory data analysis, and model evaluation, ultimately leading to the creation of a Streamlit app and PowerBI dashboard for user interaction and business insights. Key insights include the importance of feature selection and handling missing values to ensure accurate predictions.

Uploaded by

Shainaz Tazyeen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Car Price Prediction with ML Workflow

The document outlines a machine learning workflow for predicting car prices, detailing the steps from problem formulation to model deployment using linear regression. It emphasizes data preprocessing, exploratory data analysis, and model evaluation, ultimately leading to the creation of a Streamlit app and PowerBI dashboard for user interaction and business insights. Key insights include the importance of feature selection and handling missing values to ensure accurate predictions.

Uploaded by

Shainaz Tazyeen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

21/12/2025, 13:12 youlearn-summary

Machine Learning Workflow for Predicting Car Prices


The standard process for building and deploying machine learning models starts with
formulating the problem (define what to solve: here, identify variables affecting car prices and
predict MSRP—Manufacturer's Suggested Retail Price), gathering raw data, preprocessing
(cleaning, handling missing values, aggregating, visualizing), splitting data, selecting and
training a model, evaluating performance, hyperparameter tuning if needed, storing the model,
and deploying via apps or dashboards. s s
This workflow ensures reproducible results, as
followed in this car price prediction project using linear regression. s
Building on this, the
project divides into development phase (data prep and modeling) and deployment phase
(Streamlit app for user inputs/predictions and PowerBI dashboard for business insights on
predictions vs. actuals). s s

💡 Key Insight: Deployment makes models actionable—the Streamlit app lets users input
features like 500 horsepower to get instant predictions by loading the stored model, while
PowerBI visualizes predictions on full data to spot over/underpricing and key influencers. s

Loading Raw Car Data


Raw data is a CSV scraped from the web (link in description for BeautifulSoup scraping to
CSV/SQL), loaded into a pandas DataFrame c_data with 1,610 rows and columns: make (e.g.,
Aston Martin, Audi), model, year, trim, MSRP (target y-variable), invoice price, used/new price,
body size/style, cylinders, engine aspiration, drivetrain, transmission, horsepower (text + RPM),
torque (text + RPM), highway fuel economy. ...
All non-MSRP columns serve as input features
(X). s
Think of it like raw ingredients—loaded via pd.read_csv() for inspection. s
This step
matters because understanding the dataset's structure reveals potential issues like mixed data
types early. s

[Link] 1/5
21/12/2025, 13:12 youlearn-summary

Initial Data Exploration: Unique Values and Missing Data Check


To grasp categorical data, loop through columns: count unique values; if <12, print them (e.g.,
make: 7 uniques—Aston Martin, Audi, BMW, Bentley, Ford, Mercedes, Nissan; body size: 3;
body style: 12; cylinders: 10; etc.). s s Numeric columns (e.g., MSRP) have many uniques. s

Check missing values with [Link]().sum() : high in invoice price, cylinders, horsepower/torque
(fewer), highway fuel economy. s Significance: This identifies high-missing columns for
dropping (invoice price, cylinders, highway fuel economy—too many gaps relative to 1,610
rows) vs. imputable ones, preventing model failure from NaNs. s s

Handling Specific Missing Values: Horsepower and Torque


Horsepower/torque are text (e.g., "500 RPM"); extract numeric part: c_data['horsepower_no'] =

c_data['horsepower'].str[:3].astype(float) (first 3 chars). s View missing horsepower rows


( df[df['horsepower_no'].isnull()] ): all Ford cars (5 rows). s
Impute with make-specific mean:
ford_hp_mean = c_data[c_data['make']=='Ford']['horsepower_no'].mean();

c_data['horsepower_no'].fillna(ford_hp_mean, inplace=True) . s Repeat for original text column. s

For torque: same extraction to torque_no ; missing across makes (Audi, BMW, Ford)—use overall
mean: overall_torque_mean = c_data['torque_no'].mean();

c_data['torque_no'].fillna(overall_torque_mean, inplace=True) . s s Why group-specific?


Averages per make avoid biasing (e.g., Ford-specific for horsepower). s Post-clean: no NaNs in
these. s

Cleaning Data Types for Numeric Columns


Many "numeric" columns are object dtype due to $, commas (e.g., MSRP: "$100,000"). s
Clean:
df['MSRP'] = df['MSRP'].[Link]('$','').[Link](',','').astype(float) (same for used/new
price). s Result: float dtypes, usable for math/models; head() shows clean numbers. s

Connection: This builds on missing value handling—clean types enable


aggregation/visualization. s

Exploratory Data Analysis: Visualizing Relationships


Use [Link](df) on full dataset (small, <20 features): MSRP shows linear trends with
horsepower_no (rising price with HP) and torque_no—prime candidates. s s Deeper: plot
these vs. categoricals like engine aspiration (electric motor, naturally aspirated, supercharged,

[Link] 2/5
21/12/2025, 13:12 youlearn-summary

turbocharged, twin turbo, twin charged): twin turbo cars pricier. s s Use make as hue: Bentley
outliers explained by make (keep them—model can use make feature). s

Bar plots for all categoricals (list: make, body_size, etc.) vs. MSRP sum (not avg—mind count
imbalances): Bentley/Aston Martin highest; convertibles/coupes pricier; twin turbo, AWD,
automatic expensive. s s

Numeric distributions (loop: histogram + mean vline): MSRP skewed right (Bentley outliers,
keepable); used/new price similar; horsepower/torque near-normal. ... Box plots confirm
outliers (explained, no action). s

Categorical breakdowns: box + swarm plots of MSRP by group (e.g., by make: Bentley highest,
no outliers within; others have explainable outliers like high-HP variants). s s Why visualize?
Uncovers linear/categorical predictors, justifies keeping outliers (modelable via make/engine).
s

Visualization Type Purpose Key Findings for MSRP

Pairplot Numeric pairwise Strong linear: horsepower_no (0.7 corr later),


scatters torque_no

Numeric Hist + Mean Distributions Skewed right (outliers OK)

Box/Swarm by Group distributions Bentley high; outliers explainable (e.g., big


Categorical engines)

Bars by Categorical Sum MSRP Bentley top; twin turbo/AWD pricier s

...

Preparing Final Dataset for Modeling


Drop non-informative: index, model (150+ uniques, high compute), year (only 2023-24), trim
(model-specific), used/new price (correlated to MSRP), text horsepower/torque. ... One-hot
encode categoricals: pd.get_dummies(df, columns=categoricals) creates binary columns (e.g.,
make_Aston Martin: True/False). s s
Result: fully numeric DataFrame ready for models. s

Connection to EDA: Drops based on uniques/visuals; encoding enables ML input. s

[Link] 3/5
21/12/2025, 13:12 youlearn-summary

Computing Correlations and Feature Importances


Pearson corr heatmap on numerics ( df[numerics].corr(method='pearson') ): MSRP high with
horsepower_no (0.7), moderate-high torque_no. s
Confirms pairplot. s

Feature importances: Split X (all except MSRP), y=MSRP; fit RandomForestClassifier (proxy for
regression importances); extract model.feature_importances_ , sort top (horsepower_no #1,
make_Ford #2, torque_no #3, engine_aspiration_turbocharged #4; bottom: 0 info gain). ...

Significance: Guides understanding (HP/torque/make drive price); drop 0-importance later. s _

python

# Example: Feature importance extraction


from [Link] import RandomForestClassifier
X = final_df.drop('MSRP', axis=1); y = final_df['MSRP']
model = RandomForestClassifier(); [Link](X, y)
importances = [Link]({'feature': [Link], 'importance':
model.feature_importances_}).sort_values('importance', ascending=False)

s s

Train-Test Split: Hold-Out Validation


train_test_split(X, y, test_size=0.2) : 80% train (1,288 rows), 20% test (322 unseen rows). s

Prevents overfitting—model trains on train, evaluates on test. s

Training and Evaluating Linear Regression Model


Linear regression finds best-fit line minimizing residuals: y = a + bx (a=intercept,
b=coefficients; iterates features for multi-var). s From sklearn: LinearRegression().fit(X_train,

y_train) ; predict train/test/all data. s

Metrics:

R² (explains variance): train 0.89, test higher (excellent, near 1). s

RMSE (avg error $): train ~17k, test ~16k (lower better). s

MAE similar. s Coefficients/intercept printed. s Why good? Test > train suggests no overfitting;
errors reasonable for car prices. s

[Link] 4/5
21/12/2025, 13:12 youlearn-summary

python

from sklearn.linear_model import LinearRegression


model = LinearRegression()
[Link](X_train, y_train)
y_pred_train = [Link](X_train)
y_pred_test = [Link](X_test)
# Metrics: r2_score, mean_squared_error**(0.5), mean_absolute_error

s s

Storing Model and Results for Deployment


[Link](model, open('[Link]', 'wb')) —saves for Streamlit load. s
Export rounded top
importances (27 features, drop 0s) to Excel. s Add predictions to full DF
( final_df['predictions'] = [Link](final_df.drop('MSRP', axis=1)) ), export CSV/Excel for
PowerBI (business views: variables, over/underpricing). s
Connection: Links dev to deployment
—pickled model called in app; data w/preds for dashboard. s s

Deployment Overview
Streamlit app: User inputs (e.g., horsepower=500, make, etc.) → loads model → predicts price.
s s
PowerBI dashboard: Static predictions on full data (no inputs)—shows key vars, pricing
insights. s Detailed build/deploy in separate videos. s Why separate phases? Dev focuses
training; deployment operationalizes for users/business. s

⚠️ Warning: Visuals use MSRP sum (not avg)—biased by counts (e.g., more cheap cars
lower sum); interpret cautiously. s s

[Link] 5/5

You might also like