car_predict / app.py
tuhanasinan's picture
Upload 3 files
463f33a verified
# %%
import pandas as pd
import numpy as np
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ı
# %% [markdown]
# # Car Prediction #
# İkinci el araç fiyatlarını (özelliklerine göre) tahmin eden modeller oluşturma ve MLOPs ile Hugging Face üzerinden yayımlayacağız.
#
# %%
#pip install xlrd
# %%
df=pd.read_excel('cars.xls')
df
# %%
# %%
def grab_col_names(dataframe, cat_th=10, car_th=20):
cat_cols = [col for col in dataframe.columns if dataframe[col].dtypes == "O"]
num_but_cat = [col for col in dataframe.columns if dataframe[col].nunique() < cat_th and dataframe[col].dtypes != "O"]
cat_but_car = [col for col in dataframe.columns if dataframe[col].nunique() > car_th and dataframe[col].dtypes == "O"]
cat_cols = cat_cols + num_but_cat
cat_cols = [col for col in cat_cols if col not in cat_but_car]
num_cols = [col for col in dataframe.columns if dataframe[col].dtypes != "O"]
num_cols = [col for col in num_cols if col not in num_but_cat]
print(f"Observations: {dataframe.shape[0]}")
print(f"Variables: {dataframe.shape[1]}")
print(f'cat_cols: {len(cat_cols)}')
print(f'num_cols: {len(num_cols)}')
print(f'cat_but_car: {len(cat_but_car)}')
print(f'num_but_cat: {len(num_but_cat)}')
return cat_cols, num_cols, cat_but_car
cat_cols,num_cols,cat_but_car=grab_col_names(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)
# %%
num_cols=[col for col in num_cols if col!="Price"]
# %%
all_cat_cols = cat_cols + cat_but_car
preprocess = ColumnTransformer(
transformers=[
('num', StandardScaler(), num_cols),
('cat', OneHotEncoder(), all_cat_cols)
]
)
# %%
my_model=LinearRegression()
# %%
pipe=Pipeline(steps=[("preprocesser",preprocess),("model",my_model)])
# %%
pipe.fit(X_train,y_train)
# %%
y_pred=pipe.predict(X_test)
# %%
# %%
#!pip install streamlit
# %%
df["Type"].value_counts()
# %% [markdown]
# #### Python ile yapılan çalışmnalrın hızlı bir şekilde deploy edilmesi için HTML render arayüzler tasarlamanızı sağlar.
# %%
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("II. El Araba Fiyatı Tahmin:red_car: @tuhanasinan")
st.write('Arabanın özelliklerini seçiniz')
make=st.selectbox('Marka',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('Kilometre',100,200000)
car_type=st.selectbox('Araç Tipi',df[(df['Make']==make) &(df['Model']==model)&(df['Trim']==trim)]['Type'].unique())
cylinder=st.selectbox('Cylinder',df['Cylinder'].unique())
liter=st.number_input('Yakıt hacmi',1,10)
doors=st.selectbox('Kapı sayısı',df['Doors'].unique())
cruise=st.radio('Hız Sbt.',[True,False])
sound=st.radio('Ses Sis.',[True,False])
leather=st.radio('Deri döşeme.',[True,False])
if st.button('Tahmin'):
pred=price(make,model,trim,mileage,car_type,cylinder,liter,doors,cruise,sound,leather)
st.write('Fiyat:$', pred)
# %%
#pip install pipreqs
# %%