|
import pandas as pd |
|
from sklearn.model_selection import train_test_split |
|
from sklearn.linear_model import LinearRegression |
|
from sklearn.metrics import r2_score,mean_squared_error |
|
from sklearn.compose import ColumnTransformer |
|
from sklearn.preprocessing import OneHotEncoder, StandardScaler |
|
from sklearn.pipeline import Pipeline |
|
import streamlit as st |
|
|
|
data = pd.read_excel("cars.xls") |
|
|
|
X = data.drop("Price", axis=1) |
|
y = data["Price"] |
|
|
|
|
|
preprocess = ColumnTransformer( |
|
transformers=[("num", StandardScaler(), ["Mileage", "Cylinder", "Liter", "Doors"]), |
|
("cat", OneHotEncoder(), ["Make", "Model", "Trim", "Type"])]) |
|
|
|
LR = LinearRegression() |
|
|
|
|
|
pipe = Pipeline(steps= [("preprocessor", preprocess), ("model", LR)]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pipe.fit(X, y) |
|
|
|
|
|
def price(mileage, make, model, trim, type, cylinder, liter, doors, cruise, sound, leather): |
|
input_data= pd.DataFrame({"Mileage": [mileage], |
|
"Make": [make], |
|
"Model": [model], |
|
"Trim": [trim], |
|
"Type": [type], |
|
"Cylinder": [cylinder], |
|
"Liter": [liter], |
|
"Doors": [doors], |
|
"Cruise": [cruise], |
|
"Sound": [sound], |
|
"Leather": [leather]}) |
|
prediction = pipe.predict(input_data)[0] |
|
return prediction |
|
|
|
st.title("Used Car Price Prediction :red_car:") |
|
st.write("Please select the car features.") |
|
mileage = st.number_input("Mileage", 100, 200000) |
|
make = st.selectbox("Brand", data["Make"].unique()) |
|
model = st.selectbox("Model", data[data["Make"] == make]["Model"].unique()) |
|
trim = st.selectbox("Trim", data[(data["Make"] == make) & (data["Model"] == model)]["Trim"].unique()) |
|
car_type = st.selectbox("Type", data[(data["Make"] == make) & (data["Model"] == model) & (data["Trim"] == trim)]["Type"].unique()) |
|
cylinder = st.selectbox("Cylinder", data["Cylinder"].unique()) |
|
liter = st.number_input("Fuel Volume", 1, 10) |
|
doors = st.selectbox("Doors", data['Doors'].unique()) |
|
cruise = st.radio("Speed Constant", [True, False]) |
|
sound = st.radio("Sound System", [True, False]) |
|
leather = st.radio("Leather", [True, False]) |
|
if st.button("Prediction"): |
|
pred = price(mileage, make, model, trim, car_type, cylinder, liter, doors, cruise, sound, leather) |
|
st.write("Price ($):", round(pred, 2)) |
|
|