KAARIN's picture
Update app.py
08c5345 verified
#!/usr/bin/env python
# coding: utf-8
# In[1]:
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.preprocessing import OneHotEncoder,StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
# In[2]:
df=pd.read_excel('cars.xls')
# In[3]:
df.head()
# In[4]:
X=df.drop('Price',axis=1)
y=df['Price']
# In[5]:
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=.2,random_state=42)
# In[6]:
preprocessor=ColumnTransformer(transformers=[('num',StandardScaler(),['Mileage','Cylinder','Liter','Doors']),
('cat',OneHotEncoder(),['Make','Model','Trim','Type'])]
)
# In[7]:
my_model=LinearRegression()
pipe=Pipeline(steps=[('preprocessor',preprocessor),('model',my_model)])
pipe.fit(X_train,y_train)
y_pred=pipe.predict(X_test)
mean_squared_error(y_test,y_pred)**.5, r2_score(y_test,y_pred)
# In[8]:
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],
'Cylinder':[cylinder],
'Liter':[liter],
'Doors':[doors],
'Cruise':[cruise],
'Sound':[sound],
'Leather':[leather]
})
prediction=pipe.predict(input_data)[0]
return prediction
st.title("Predict Car Prices @KenanAvşar")
st.write("Enter Car Details to predict the price of the car")
make=st.selectbox("Marka",df['Make'].unique())
model=st.selectbox("Model",df[df['Make']==make]['Model'].unique())
trim=st.selectbox("Versiyon",df[(df['Make']==make)&(df['Model']==model)]['Trim'].unique())
mileage=st.number_input("Kilometre",100,df['Mileage'].max())
car_type=st.selectbox("Araç Tipi",df[(df['Make']==make)&(df['Model']==model)&(df['Trim']==trim)]['Type'].unique())
cylinder=st.selectbox("Silindir",df[(df['Make']==make)&(df['Model']==model)&(df['Trim']==trim)]['Cylinder'].unique())
liter=st.selectbox("Depo Hacmi",df[(df['Make']==make)&(df['Model']==model)&(df['Trim']==trim)]['Liter'].unique())
doors=st.selectbox("Kapı Sayısı",df[(df['Make']==make)&(df['Model']==model)&(df['Trim']==trim)]['Doors'].unique())
cruise=st.radio("Hız Sabitleyici",[True,False])
sound=st.radio("Ses Sistemi",[True,False])
leather=st.radio("Deri Döşeme",[True,False])
if st.button('Tahmin Et'):
pred=price(make,model,trim,mileage,car_type,cylinder,liter,doors,cruise,sound,leather)
st.write('Fiyat:$',round(pred,2))
# In[ ]: