Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions solutions/Prediction of the prices of Electric Cars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
def load_data():
"""
Load a predefined electric car dataset.

Returns:
pd.DataFrame: loaded dataset as a Pandas DataFrame.
"""
import pandas as pd
from io import StringIO

try:
# Embedded electric car dataset
data = """Make,Model,Year,BatteryCapacity_kWh,Range_km,Price
Tesla,Model 3,2021,75,450,50000
Nissan,Leaf,2019,40,240,30000
Chevrolet,Bolt EV,2020,66,380,37000
Hyundai,Kona Electric,2021,64,415,40000
Volkswagen,ID.4,2022,77,520,45000
BMW,i3,2018,33,200,29000
"""
return pd.read_csv(StringIO(data))
except Exception as e:
print(f"Error loading data: {e}")
return pd.DataFrame()

def preprocess_data(data):
"""
Preprocess the electric car dataset by handling missing values and encoding categorical variables.
"""
import pandas as pd

try:
data = data.dropna()
# One-hot encode 'Make' and 'Model'
data = pd.get_dummies(data, columns=['Make', 'Model'], drop_first=True)
return data
except Exception as e:
print(f"Error preprocessing data: {e}")
return pd.DataFrame()
def analyze_data(data):
"""
Perform exploratory data analysis on the dataset.
"""
import matplotlib.pyplot as plt
import seaborn as sns

try:
print("Dataset Summary:")
print(data.describe())

sns.pairplot(data[['Year', 'BatteryCapacity_kWh', 'Range_km', 'Price']])
plt.show()
except Exception as e:
print(f"Error in data analysis: {e}")
def train_model(data):
"""
Train a predictive model using the dataset.

Returns:
tuple: Trained model, test features, test labels
"""
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler

try:
features = data.drop(columns='Price')
target = data['Price']

X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)

# Scale the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

model = LinearRegression()
model.fit(X_train, y_train)

return model, X_test, y_test, scaler
except Exception as e:
print(f"Error in model training: {e}")
return None, None, None, None
def evaluate_model(model, X_test, y_test):
"""
Evaluate the trained model using Mean Squared Error and R-squared metrics.
"""
from sklearn.metrics import mean_squared_error, r2_score

try:
y_pred = model.predict(X_test)

mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"Mean Squared Error: {mse}")
print(f"R-squared: {r2}")
except Exception as e:
print(f"Error in model evaluation: {e}")
def visualize_results(model, X_test, y_test):
"""
Visualize the actual vs predicted prices and feature importance.
"""
import matplotlib.pyplot as plt
import numpy as np

try:
# Actual vs Predicted Prices
y_pred = model.predict(X_test)
plt.figure(figsize=(10, 6))
plt.scatter(y_test, y_pred, alpha=0.6, color='blue')
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], '--r', linewidth=2)
plt.xlabel('Actual Prices')
plt.ylabel('Predicted Prices')
plt.title('Actual vs Predicted Prices')
plt.show()

# Feature Coefficients
coefficients = model.coef_
feature_names = ['Year', 'BatteryCapacity_kWh', 'Range_km'] + list(X_test.columns[3:])
plt.figure(figsize=(8, 4))
plt.bar(feature_names, coefficients, color='skyblue')
plt.title('Feature Coefficients')
plt.xticks(rotation=45)
plt.ylabel('Coefficient Value')
plt.show()
except Exception as e:
print(f"Error in visualization: {e}")
def save_model(model, scaler, filename="electric_car_price_model.pkl"):
"""
Save the trained model and scaler to a file.
"""
import joblib

try:
joblib.dump({'model': model, 'scaler': scaler}, filename)
print(f"Model saved as {filename}")
except Exception as e:
print(f"Error saving model: {e}")
if name == "main":
data = load_data()
if not data.empty:
data = preprocess_data(data)
analyze_data(data)
model, X_test, y_test, scaler = train_model(data)
if model:
evaluate_model(model, X_test, y_test)
visualize_results(model, X_test, y_test)
save_model(model, scaler)
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import pandas as pd
from electric_car_prices_analysis import (
load_data,
preprocess_data,
train_model,
evaluate_model,
save_model,
)

def test_load_data():
"""
Test the load_data function to ensure it returns a DataFrame with correct columns.
"""
data = load_data()
assert isinstance(data, pd.DataFrame), "load_data() should return a DataFrame"
expected_columns = ["Make", "Model", "Year", "BatteryCapacity_kWh", "Range_km", "Price"]
assert list(data.columns) == expected_columns, f"Expected columns: {expected_columns}, but got {list(data.columns)}"
print("test_load_data passed!")

def test_preprocess_data():
"""
Test the preprocess_data function to ensure it cleans the data and encodes categorical variables.
"""
data = load_data()
processed_data = preprocess_data(data)
assert not processed_data.isnull().any().any(), "preprocess_data() should remove missing values"
assert "Make_Tesla" in processed_data.columns, "preprocess_data() should encode categorical variables"
print("test_preprocess_data passed!")

def test_train_model():
"""
Test the train_model function to ensure the model trains correctly and splits the data properly.
"""
data = load_data()
processed_data = preprocess_data(data)
model, X_test, y_test, scaler = train_model(processed_data)
assert hasattr(model, "predict"), "train_model() should return a trained model"
assert len(X_test) > 0, "X_test should not be empty"
assert len(y_test) > 0, "y_test should not be empty"
print("test_train_model passed!")

def test_evaluate_model():
"""
Test the evaluate_model function to ensure it calculates metrics without errors.
"""
data = load_data()
processed_data = preprocess_data(data)
model, X_test, y_test, _ = train_model(processed_data)
try:
evaluate_model(model, X_test, y_test)
print("test_evaluate_model passed!")
except Exception as e:
assert False, f"evaluate_model() raised an exception: {e}"

def test_save_model():
"""
Test the save_model function to ensure the model is saved correctly.
"""
data = load_data()
processed_data = preprocess_data(data)
model, _, _, scaler = train_model(processed_data)
save_model(model, scaler, "test_model.pkl")
try:
import joblib
saved_data = joblib.load("test_model.pkl")
assert "model" in saved_data and "scaler" in saved_data, "save_model() should save both the model and scaler"
print("test_save_model passed!")
except Exception as e:
assert False, f"save_model() failed to save or load the model: {e}"

if _name_ == "_main_":
test_load_data()
test_preprocess_data()
test_train_model()
test_evaluate_model()
test_save_model()
print("All tests passed!")