JamesCookJr90's picture
Update app.py
6ad1220 verified
raw
history blame
2.96 kB
#!/usr/bin/env python
# coding: utf-8
import pandas as pd
from sklearn.model_selection import train_test_split #veri setini bölme işlemleri
from sklearn.linear_model import LinearRegression #Doğrusal regresyon
from sklearn.metrics import r2_score,mean_squared_error #modelimizin performansını ölçmek için
from sklearn.compose import ColumnTransformer #Sütun dönüşüm işlemleri
from sklearn.preprocessing import OneHotEncoder, StandardScaler # kategori - sayısal dönüşüm ve ölçeklendirme
from sklearn.pipeline import Pipeline #Veri işleme hattı
df=pd.read_excel('cars.xls')
df
X=df.drop('Price',axis=1) #fiyat sütunu çıkar fiyata etki edenler kalsın
y=df['Price'] #tahmin
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)
preprocess=ColumnTransformer(
transformers=[
('num',StandardScaler(),['Mileage', 'Cylinder','Liter','Doors']),
('cat',OneHotEncoder(),['Make','Model','Trim','Type'])
]
)
my_model=LinearRegression()
pipe=Pipeline(steps=[('preprocessor',preprocess),('model',my_model)])
pipe.fit(X_train,y_train)
y_pred=pipe.predict(X_test)
print('RMSE',mean_squared_error(y_test,y_pred)**0.5)
print('R2',r2_score(y_test,y_pred))
df['Mileage'].max()
df['Type'].unique()
df['Liter'].max()
import streamlit as st
#price tahmin fonksiyonu tanımla
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("Used Car Price Estimation:red_car: @jameswhitecookjr90")
st.write('Select the features of the car')
make=st.selectbox('Make',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('Mileage',100,200000)
car_type=st.selectbox('Vehicle Type',df[(df['Make']==make) &(df['Model']==model)&(df['Trim']==trim)]['Type'].unique())
cylinder=st.selectbox('Cylinder',df['Cylinder'].unique())
liter=st.number_input('Engine Displacement',1,10)
doors=st.selectbox('Number of Doors',df['Doors'].unique())
cruise=st.radio('Cruise Control',[True,False])
sound=st.radio('Audio System',[True,False])
leather=st.radio('Leather Seat',[True,False])
if st.button('Predict'):
pred=price(make,model,trim,mileage,car_type,cylinder,liter,doors,cruise,sound,leather)
st.write('Price:$', round(pred[0],2))