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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# coding: utf-8
|
| 3 |
+
|
| 4 |
+
# # Car Prediction #
|
| 5 |
+
# İkinci el araç fiyatlarını (özelliklerine göre) tahmin eden modeller oluşturma ve MLOPs ile Hugging Face üzerinden yayımlayacağız.
|
| 6 |
+
#
|
| 7 |
+
|
| 8 |
+
# In[1]:
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
import pandas as pd
|
| 12 |
+
from sklearn.model_selection import train_test_split #veri setini bölme işlemleri
|
| 13 |
+
from sklearn.linear_model import LinearRegression #Doğrusal regresyon
|
| 14 |
+
from sklearn.metrics import r2_score,mean_squared_error #modelimizin performansını ölçmek için
|
| 15 |
+
from sklearn.compose import ColumnTransformer #Sütun dönüşüm işlemleri
|
| 16 |
+
from sklearn.preprocessing import OneHotEncoder, StandardScaler # kategori - sayısal dönüşüm ve ölçeklendirme
|
| 17 |
+
from sklearn.pipeline import Pipeline #Veri işleme hattı
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# In[2]:
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
#Excell dosyalarını okumak için
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# In[3]:
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
get_ipython().system('pip install xldr')
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# ## Veri dosyasını yükle
|
| 33 |
+
|
| 34 |
+
# In[4]:
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
ls
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# In[5]:
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
df=pd.read_excel('cars.xls')
|
| 44 |
+
df
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# In[ ]:
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
df.info()
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# In[ ]:
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Veri ön işleme
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# In[ ]:
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
X=df.drop('Price',axis=1) #fiyat sütunu çıkar fiyata etki edenler kalsın
|
| 63 |
+
y=df['Price'] #tahmin
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# In[ ]:
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# #### Veri ön işleme, standartlaştırma ve OHE işlemlerini otomatikleştiriyoruz (standarlaştırıyoruz). Artık preprocess kullanarak kullanıcında arayüz aracılığıyla gelen veriyi mdoelimize uygun hale çevirebiliriz.
|
| 73 |
+
#
|
| 74 |
+
|
| 75 |
+
# In[ ]:
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
preprocess=ColumnTransformer(
|
| 79 |
+
transformers=[
|
| 80 |
+
('num',StandardScaler(),['Mileage', 'Cylinder','Liter','Doors']),
|
| 81 |
+
('cat',OneHotEncoder(),['Make','Model','Trim','Type'])
|
| 82 |
+
]
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# In[ ]:
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
my_model=LinearRegression()
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# In[ ]:
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
#pipeline ı tanımla
|
| 96 |
+
pipe=Pipeline(steps=[('preprocessor',preprocess),('model',my_model)])
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
# In[ ]:
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
#pipeline fit
|
| 103 |
+
pipe.fit(X_train,y_train)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
# In[ ]:
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
y_pred=pipe.predict(X_test)
|
| 110 |
+
print('RMSE',mean_squared_error(y_test,y_pred)**0.5)
|
| 111 |
+
print('R2',r2_score(y_test,y_pred))
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# In[ ]:
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
#isterseniz veri setinin tammamıyla tekrar eğitim yapabilirsiniz.
|
| 118 |
+
#pipe.fit(X,y)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# ## Streamlit ile modeli yayma/deploy/kullanıma sunma
|
| 122 |
+
|
| 123 |
+
# In[ ]:
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
get_ipython().system('pip install streamlit')
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# In[ ]:
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
df['Mileage'].max()
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
# In[ ]:
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
df['Type'].unique()
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
# In[ ]:
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
df['Liter'].max()
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# #### Python ile yapılan çalışmnalrın hızlı bir şekilde deploy edilmesi için HTML render arayüzler tasarlamanızı sağlar.
|
| 148 |
+
|
| 149 |
+
# In[ ]:
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
import streamlit as st
|
| 153 |
+
#price tahmin fonksiyonu tanımla
|
| 154 |
+
def price(make,model,trim,mileage,car_type,cylinder,liter,doors,cruise,sound,leather):
|
| 155 |
+
input_data=pd.DataFrame({'Make':[make],
|
| 156 |
+
'Model':[model],
|
| 157 |
+
'Trim':[trim],
|
| 158 |
+
'Mileage':[mileage],
|
| 159 |
+
'Type':[car_type],
|
| 160 |
+
'Cylinder':[cylinder],
|
| 161 |
+
'Liter':[liter],
|
| 162 |
+
'Doors':[doors],
|
| 163 |
+
'Cruise':[cruise],
|
| 164 |
+
'Sound':[sound],
|
| 165 |
+
'Leather':[leather]})
|
| 166 |
+
prediction=pipe.predict(input_data)[0]
|
| 167 |
+
return prediction
|
| 168 |
+
st.title("Used Car Price Estimation:red_car: @jameswhitecookjr90")
|
| 169 |
+
st.write('Select the features of the car')
|
| 170 |
+
make=st.selectbox('Make',df['Make'].unique())
|
| 171 |
+
model=st.selectbox('Model',df[df['Make']==make]['Model'].unique())
|
| 172 |
+
trim=st.selectbox('Trim',df[(df['Make']==make) &(df['Model']==model)]['Trim'].unique())
|
| 173 |
+
mileage=st.number_input('Mileage',100,200000)
|
| 174 |
+
car_type=st.selectbox('Vehicle Type',df[(df['Make']==make) &(df['Model']==model)&(df['Trim']==trim)]['Type'].unique())
|
| 175 |
+
cylinder=st.selectbox('Cylinder',df['Cylinder'].unique())
|
| 176 |
+
liter=st.number_input('Engine Displacement',1,10)
|
| 177 |
+
doors=st.selectbox('Number of Doors',df['Doors'].unique())
|
| 178 |
+
cruise=st.radio('Cruise Control',[True,False])
|
| 179 |
+
sound=st.radio('Audio System',[True,False])
|
| 180 |
+
leather=st.radio('Leather Seat',[True,False])
|
| 181 |
+
if st.button('Predict'):
|
| 182 |
+
pred=price(make,model,trim,mileage,car_type,cylinder,liter,doors,cruise,sound,leather)
|
| 183 |
+
st.write('Price:$', round(pred[0],2))
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
# In[ ]:
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
#streamlit run C:\ProgramData\anaconda3\Lib\site-packages\ipykernel_launcher.py
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
# In[ ]:
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
|
cars.xls
ADDED
|
Binary file (142 kB). View file
|
|
|
requirements.txt
ADDED
|
Binary file (150 Bytes). View file
|
|
|