Spaces:
Sleeping
Sleeping
import pandas as pd | |
from sklearn.model_selection import train_test_split | |
from sklearn.linear_model import LinearRegression | |
from sklearn.metrics import mean_squared_error, r2_score | |
from sklearn.compose import ColumnTransformer | |
from sklearn.preprocessing import OneHotEncoder, StandardScaler | |
from sklearn.pipeline import Pipeline | |
import streamlit as st | |
df = pd.read_excel("cars.xls") | |
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) | |
preprocess = ColumnTransformer( | |
transformers=[ | |
('num', StandardScaler(), ['Mileage', 'Cylinder', 'Liter', 'Doors']), | |
('cat', OneHotEncoder(), ['Make', 'Model', 'Trim', 'Type', 'Cruise', 'Sound', 'Leather']) | |
] | |
) | |
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)) | |
st.title("II. El Araba Fiyatı Tahmin:red_car: @aysel_olcer") | |
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['Type'].unique()) | |
cylinder = st.selectbox('Silindir', 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 Sistemi.', [True, False]) | |
leather = st.radio('Deri döşeme.', [True, False]) | |
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 | |
if st.button('Tahmin Et'): | |
pred = price(make, model, trim, mileage, car_type, cylinder, liter, doors, cruise, sound, leather) | |
st.write('Fiyat:red_car: $', round(pred, 2)) | |