sbgonenc96's picture
hot-fix
a6a7b53 verified
#!/usr/bin/env python
# coding: utf-8
# # Car prediction model training and serving with streamlit
#import libraries
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.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler,OneHotEncoder
#Load data
df=pd.read_csv('cars.csv')
X=df.drop('Price',axis=1)
y=df[['Price']]
X_train,X_test,y_train,y_test=train_test_split(X,y,
test_size=0.2,
random_state=42)
preproccer=ColumnTransformer(transformers=[('num',StandardScaler(),
['Mileage','Cylinder','Liter','Doors']),
('cat',OneHotEncoder(),['Make','Model','Trim','Type'])])
model=LinearRegression()
pipe=Pipeline(steps=[('preprocessor',preproccer),
('model',model)])
## Train with all data
pipe.fit(X,y)
#y_pred=pipe.predict(X_test)
#mean_squared_error(y_test,y_pred)**0.5,r2_score(y_test,y_pred)
import streamlit as st
def price(make,model,trim,mileage,car_type,cylinder,liter,doors,cruise,sound,leather):
input_data=pd.DataFrame({
'Make':[make],
'Model':[model],
'Trim':[trim],
'Mileage':[mileage],
'Type':[car_type],
'Car_type':[car_type],
'Cylinder':[cylinder],
'Liter':[liter],
'Doors':[doors],
'Cruise':[cruise],
'Sound':[sound],
'Leather':[leather]
})
prediction=pipe.predict(input_data)[0]
return prediction
st.title("Car Price Prediction :racing_car: \n by @sbgonenc")
st.write("Select Car Specs")
make=st.selectbox("Brand",df['Make'].unique())
model=st.selectbox("Model",df[df['Make']==make]['Model'].unique())
trim=st.selectbox("Trim",df[(df['Make']==make) & (df['Model']==model)]['Trim'].unique())
mileage=st.number_input("Milage",1 ,60000)
car_type=st.selectbox("Type",df[(df['Make']==make) & (df['Model']==model) & (df['Trim']==trim )]['Type'].unique())
cylinder=st.selectbox("Cylinders",df['Cylinder'].unique())
liter=st.number_input("Liter",1,6)
doors=st.selectbox("Doors",df['Doors'].unique())
cruise=st.radio("Cruise",[True,False])
sound=st.radio("Sound System",[True,False])
leather=st.radio("Leather",[True,False])
if st.button("Prediction"):
pred=price(make,model,trim,mileage,car_type,cylinder,liter,doors,cruise,sound,leather)
st.write("Predicted Price :oncoming_automobile: $",round(pred[0],2))