Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
from sklearn.model_selection import train_test_split | |
from sklearn.preprocessing import StandardScaler, OneHotEncoder | |
from sklearn.compose import ColumnTransformer | |
from sklearn.pipeline import Pipeline | |
from sklearn.ensemble import RandomForestRegressor | |
from sklearn.metrics import mean_squared_error | |
# Load and prepare the dataset | |
url = "https://raw.githubusercontent.com/manishkr1754/CarDekho_Used_Car_Price_Prediction/main/notebooks/data/cardekho_dataset.csv" | |
df = pd.read_csv(url) | |
# Data preparation | |
df = df.drop(columns=['Unnamed: 0']) # Drop irrelevant column | |
X = df.drop(columns=['selling_price']) | |
y = df['selling_price'] | |
# Define feature types | |
num_features = ['vehicle_age', 'km_driven', 'mileage', 'engine', 'max_power', 'seats'] | |
cat_features = ['car_name', 'brand', 'model', 'seller_type', 'fuel_type', 'transmission_type'] | |
# Preprocessing pipeline | |
numeric_transformer = StandardScaler() | |
onehot_transformer = OneHotEncoder(handle_unknown='ignore') | |
preprocessor = ColumnTransformer( | |
transformers=[ | |
('num', numeric_transformer, num_features), | |
('cat', onehot_transformer, cat_features) | |
]) | |
# Combine preprocessing with model | |
model = Pipeline(steps=[ | |
('preprocessor', preprocessor), | |
('regressor', RandomForestRegressor(n_estimators=100, random_state=42)) | |
]) | |
# Split the data and train the model | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
model.fit(X_train, y_train) | |
# Evaluate the model (optional, you can remove this if not needed) | |
y_pred = model.predict(X_test) | |
mse = mean_squared_error(y_test, y_pred) | |
# Streamlit app | |
st.title('Used Car Price Prediction') | |
# Input fields | |
st.sidebar.header('Enter Car Details') | |
year = st.sidebar.number_input('Year of Manufacture', min_value=1990, max_value=2023, value=2015) | |
km_driven = st.sidebar.number_input('Kilometers Driven', min_value=0, max_value=300000, value=50000) | |
vehicle_age = st.sidebar.number_input('Vehicle Age (years)', min_value=0, max_value=30, value=5) | |
mileage = st.sidebar.number_input('Mileage (km/l)', min_value=0.0, max_value=50.0, value=15.0) | |
engine = st.sidebar.number_input('Engine Capacity (cc)', min_value=0, max_value=5000, value=1500) | |
max_power = st.sidebar.number_input('Maximum Power (bhp)', min_value=0, max_value=500, value=100) | |
seats = st.sidebar.number_input('Number of Seats', min_value=2, max_value=7, value=5) | |
seller_type = st.sidebar.selectbox('Seller Type', ['Dealer', 'Individual']) | |
transmission_type = st.sidebar.selectbox('Transmission Type', ['Manual', 'Automatic']) | |
fuel_type = st.sidebar.selectbox('Fuel Type', ['Petrol', 'Diesel', 'CNG', 'LPG']) | |
car_name = st.sidebar.text_input('Car Name') | |
brand = st.sidebar.text_input('Brand') | |
model_name = st.sidebar.text_input('Model') | |
# Button to trigger the prediction | |
if st.sidebar.button('Predict Price'): | |
# Create input dataframe | |
input_data = pd.DataFrame({ | |
'vehicle_age': [vehicle_age], | |
'km_driven': [km_driven], | |
'mileage': [mileage], | |
'engine': [engine], | |
'max_power': [max_power], | |
'seats': [seats], | |
'car_name': [car_name], | |
'brand': [brand], | |
'model': [model_name], | |
'seller_type': [seller_type], | |
'fuel_type': [fuel_type], | |
'transmission_type': [transmission_type] | |
}) | |
# Predict the price | |
predicted_price = model.predict(input_data) | |
st.write(f'The predicted selling price for the car is: ₹ {predicted_price[0]:,.2f}') | |