0% found this document useful (0 votes)
5 views3 pages

Complete Python Assignment Solution

The document outlines a Python assignment focused on data analysis and linear regression modeling using a car price dataset. It includes steps for data loading, handling missing values, checking for duplicates, computing averages, and visualizing relationships through scatter plots. Finally, it describes the process of preparing data for a linear regression model, evaluating its performance, and making predictions based on user input.

Uploaded by

xokamil454
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)
5 views3 pages

Complete Python Assignment Solution

The document outlines a Python assignment focused on data analysis and linear regression modeling using a car price dataset. It includes steps for data loading, handling missing values, checking for duplicates, computing averages, and visualizing relationships through scatter plots. Finally, it describes the process of preparing data for a linear regression model, evaluating its performance, and making predictions based on user input.

Uploaded by

xokamil454
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

Assignment 2

Python Solution
Q(a) Import all necessary Python libraries/packages. Load the dataset into Python and displays
requirement as per the assignment.
import pandas as pd
import numpy as np
import [Link] as plt
from [Link] import files
uploaded = [Link]()
df = pd.read_csv('[Link]')
[Link]
[Link]
Q(b) Check for missing values in the dataset. If any are found, briefly explain how you would handle
them.
[Link]().sum()
Q(c) Determine the required values as per the assignment.
df['carbody'].nunique()
df['fueltype'].mode()[0]
Q(d) Check whether duplicate records exist and remove them if present.
[Link]().sum()
df = df.drop_duplicates()
[Link]().sum()
Q(e) Compute and display required values as per the assignment.
df['price'].mean()
df['horsepower'].mean()
df[['citympg', 'highwaympg']].mean()
Q(f) Plot scatter plots for: • engine size vs price • horsepower vs price Briefly comment on the
relationships observed.
[Link](df['enginesize'], df['price'])
[Link]('Engine Size')
[Link]('Price')
[Link]('Engine Size vs Price')
[Link]()
[Link](df['horsepower'], df['price'])
[Link]('Horsepower')
[Link]('Price')
[Link]('Horsepower vs Price')
[Link]()
Q(g) Create a new column avg_mileage as the average of city and highway mileage.
df['avg_mileage'] = (df['citympg'] + df['highwaympg']) / 2
df[['citympg', 'highwaympg', 'avg_mileage']].head()
Q(h) For price prediction, prepare the dataset (X and y) using only numeric predictors (enginesize,
horsepower, curbweight, carwidth, wheelbase, avg_mileage). Split the dataset into training and testing
sets in a 70:30 ratio. Build a Linear Regression model to predict car price and generate predictions on
the test set.
features = [
'enginesize',
'horsepower',
'curbweight',
'carwidth',
'wheelbase',
'avg_mileage'
]

X = df[features]
y = df['price']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.30, random_state=42
)
from sklearn.linear_model import LinearRegression
model = LinearRegression()
[Link](X_train, y_train)
y_pred = [Link](X_test)
Q(i) Evaluate your developed model using: • Root Mean Squared Error (RMSE) • R² score Briefly
interpret the resuls.
from [Link] import mean_squared_error, r2_score
from [Link] import mean_squared_error, r2_score
import numpy as np
# Mean Squared Error
mse = mean_squared_error(y_test, y_pred)
# Root Mean Squared Error
rmse = [Link](mse)
# R-squared
r2 = r2_score(y_test, y_pred)
rmse, r2
Q(j) Interpret the following coefficients from the regression model in business terms: • enginesize •
avg_mileage
[Link](model.coef_, index=features)
Q(k) Predict car price using your developed model by taking user input.
enginesize = float(input("Enter enginesize: "))
horsepower = float(input("Enter horsepower: "))
curbweight = float(input("Enter curbweight: "))
carwidth = float(input("Enter carwidth: "))
wheelbase = float(input("Enter wheelbase: "))
avg_mileage = float(input("Enter avg_mileage: "))

user_input = [Link](
[[enginesize, horsepower, curbweight, carwidth, wheelbase, avg_mileage]],
columns=features
)
predicted_price = [Link](user_input)
print("Predicted Car Price:", predicted_price[0])
Enter enginesize: 130
Enter horsepower: 110
Enter curbweight: 2800
Enter carwidth: 65
Enter wheelbase: 102
Enter avg_mileage: 30
Answer: Predicted Car Price: 13008.239431901566

You might also like