This project is a car price prediction tool built with Python and Streamlit. The model leverages machine learning techniques to predict the price of a car based on various input features like model year, mileage, engine type, and more.
This project is a car price prediction tool built with Python and Streamlit. The model leverages machine learning techniques to predict the price of a car based on various input features like model year, mileage, engine type, and more.
- Predicts car prices based on user-provided details
- Interactive UI built with Streamlit for easy usability
- Data preprocessing, transformation, and model loading handled in the backend
project-folder/
│
├── main.py # Main Streamlit application
├── car_price_model.joblib # Trained machine learning model
├── requirements.txt # Python dependencies
└── README.md # Project documentation
git clone https://github.com/yourusername/car-price-prediction.git
cd car-price-predictionpip install -r requirements.txtstreamlit run main.pyOpen the app in your browser by going to http://localhost:8501.
- Enter details like car model, year, mileage, transmission type, and other relevant information.
- Click the "Predict Price" button to get an estimated price.
The model was trained using various features, including:
- Model year
- Mileage
- Transmission type
- Fuel type
- Engine capacity and more...
The model was saved using joblib and can be reloaded for predictions.
This project uses the following libraries:
- Streamlit - For building the interactive web application
- Scikit-learn - For machine learning model training and pipeline management
- XGBoost - For the prediction model
- Pandas - For data manipulation and preprocessing
Contributions are welcome! Please fork the repository and submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.
-
Replace username in
git clonewith your GitHub username. -
If you don't have the
requirements.txtfile, you can create it by running the following command:pip freeze > requirements.txt
import streamlit as st
import pandas as pd
import joblibstreamlit: For creating an interactive UI.pandas: For data manipulation.joblib: For loading the saved model.
st.markdown("""
<h1 style='text-align: right;'>مدل پیشبینی قیمت خودرو</h1>
""", unsafe_allow_html=True)- Displays the app title in Persian.
st.markdown("""
<style>
.css-1dq8tca {
text-align: right;
direction: rtl;
}
.stSelectbox, .stNumberInput, .stButton > button {
text-align: right;
direction: rtl;
float: right;
}
</style>
""", unsafe_allow_html=True)- Sets up right-aligned UI elements for better Persian language support.
train_data = pd.read_csv('train.csv')
model = joblib.load('car_price_model.joblib.gz')
preprocessor = joblib.load("car_price_model_preprocessor.joblib")train_data: Loads the dataset used for training.model: Loads the pre-trained model.preprocessor: Loads the preprocessor used to transform input data.
column_dictionaries = {}
for col in train_data.select_dtypes(include='object').columns:
unique_values = train_data[col].unique()
value_to_number = {value: idx for idx, value in enumerate(unique_values)}
column_dictionaries[col] = value_to_number- Creates mappings for categorical values to convert text into numerical values.
title = st.selectbox("نام خودرو", list(column_dictionaries['title'].keys()))
year = st.number_input("سال ساخت", min_value=1350, max_value=1403, value=1390)
mileage = st.number_input("کارکرد (به کیلومتر)", min_value=0.0, value=110000.0, step=1000.0)- Collects user input for the car details, including title, year, and mileage.
input_data = pd.DataFrame({
"title": [column_dictionaries['title'][title]],
"year": [year],
"mileage": [mileage],
"transmission": [column_dictionaries['transmission'][transmission]],
"fuel": [column_dictionaries['fuel'][fuel]],
"body_color": [column_dictionaries['body_color'][body_color]],
"inside_color": [column_dictionaries['inside_color'][inside_color]],
"body_status": [column_dictionaries['body_status'][body_status]],
"body_type": [column_dictionaries['body_type'][body_type]],
"volume": [volume],
"engine": [column_dictionaries['engine'][engine]],
"acceleration": [acceleration]
})- Converts user inputs into a structured DataFrame, replacing categorical values with numerical representations.
if st.button("پیشبینی قیمت"):
predicted_price = model.predict(input_data)
st.markdown(f"""
<div style='text-align: right;'>
<span style='font-weight: bold; font-size: 18px;'>قیمت پیشبینی شده: </span>
<span style='color: purple; font-size: 24px; font-weight: bold;'>{predicted_price[0]:,.0f}</span>
<span style='font-weight: bold; font-size: 18px;'>تومان</span>
</div>
""", unsafe_allow_html=True)- When the "Predict Price" button is clicked, the model predicts the price and displays it in Persian format.
