0% found this document useful (0 votes)
3 views

Assignment 5

Uploaded by

riteshpc13
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 5

Uploaded by

riteshpc13
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment no 5

write a python program to predict the speed of a 5 year old car

import numpy as np

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error

# Generate synthetic data

np.random.seed(42)

n_samples = 1000

# Features: Age of the car in years, Mileage in thousands of miles

# Speed: Speed of the car in mph

data = {

'Age': np.random.randint(1, 15, n_samples),

'Mileage': np.random.randint(5, 150, n_samples),

'Speed': np.random.randint(30, 120, n_samples) # Target variable

df = pd.DataFrame(data)

# Split the data into training and testing sets

X = df[['Age', 'Mileage']]

y = df['Speed']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,


random_state=42)
# Create and train the model

model = LinearRegression()

model.fit(X_train, y_train)

# Predict on the test set and evaluate

y_pred = model.predict(X_test)

mse = mean_squared_error(y_test, y_pred)

print(f"Mean Squared Error: {mse}")

# Predict the speed of a 5-year-old car with some mileage

age_of_car = 5

mileage_of_car = 50 # Example mileage in thousands of miles

speed_prediction = model.predict([[age_of_car, mileage_of_car]])

print(f"Predicted speed of a {age_of_car}-year-old car with


{mileage_of_car}k miles: {speed_prediction[0]:.2f} mph")

output:-

Mean Squared Error: 744.9109089260654

Predicted speed of a 5-year-old car with 50k miles: 72.68 mph

To predict the speed of a 5-year-old car, you would typically need a


dataset that includes features related to cars (such as age, make, model,
mileage, etc.) and their corresponding speeds. For simplicity, I'll
demonstrate a basic example using synthetic data.

Steps

1. Generate Synthetic Data: Create a dataset with features that


could influence the speed of a car (e.g., age, mileage).

2. Train a Model: Use a simple regression model to train on this


dataset.
3. Predict the Speed: Use the trained model to predict the speed of a
5-year-old car.

Explanation

1. Generate Synthetic Data:

o A dataset with Age of the car, Mileage, and Speed is created.


The Speed is a target variable.

2. Split Data:

o Split the data into training and testing sets using


train_test_split().

3. Create and Train the Model:

o A LinearRegression model is trained using Age and Mileage to


predict Speed.

4. Predict the Speed:

o Use the trained model to predict the speed of a car that is 5


years old with a specific mileage.

This program uses synthetic data for demonstration purposes. In a real-


world scenario, you would use an actual dataset that includes relevant
features and target values. The model's performance and prediction
accuracy would depend heavily on the quality and relevance of the data
used for training.

You might also like