Car_Prediction / app.py
Metinhsimi's picture
Update app.py
6af18e1 verified
import pandas as pd
from sklearn.model_selection import train_test_split as tts
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
import streamlit as st
# Verileri yükleyin
df = pd.read_excel('cars.xls')
# Bağımlı ve bağımsız değişkenleri ayırın
x = df.drop('Price', axis=1)
y = df[['Price']]
# Eğitim ve test verilerine bölün
x1, x2, y1, y2 = tts(x, y, random_state=42, test_size=0.30)
# Veri ön işleme adımları
preprocessor = ColumnTransformer(
transformers=[
('num', StandardScaler(), ['Mileage', 'Cylinder', 'Liter', 'Doors']),
('cat', OneHotEncoder(handle_unknown='ignore'), ['Make', 'Model', 'Trim', 'Type'])
]
)
# Pipeline oluşturma
model = LinearRegression()
pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('regressor', model)])
# Modeli eğitme
pipeline.fit(x1, y1)
# Tahmin yapma
predictions = pipeline.predict(x2)
# Performans metrikleri hesaplama
rmse = mean_squared_error(y2, predictions, squared=False)
r2 = r2_score(y2, predictions)
print(f'RMSE: {rmse}')
print(f'R^2: {r2}')
def price_pred(make, model, trim, mileage, type, cylinder, liter, doors, cruise, sound, leather):
# Girdi verilerini bir DataFrame'e dönüştür
input_data = pd.DataFrame({
'Make': [make],
'Model': [model],
'Trim': [trim],
'Mileage': [mileage],
'Type': [type],
'Cylinder': [cylinder],
'Liter': [liter],
'Doors': [doors],
'Cruise': [cruise],
'Sound': [sound],
'Leather': [leather]
})
# Tahmin yapın
prediction = pipeline.predict(input_data)[0] # Tahminlerin ilk elemanını al
return prediction
# Streamlit arayüzü
st.title("ML OPS Car Price Prediction :red_car:")
st.write("Enter Car Details to predict the price")
# Kullanıcı girdi alanları
make = st.selectbox("Marka", df['Make'].unique())
model = st.selectbox("Model", df['Model'].unique())
trim = st.selectbox("Döşeme", df['Trim'].unique())
mileage = st.number_input("Kilometre", min_value=200, max_value=60000)
type = st.selectbox("Tip", df['Type'].unique())
cylinder = st.number_input("Silindir", min_value=0, format="%d")
liter = st.number_input("Litre", min_value=1.0, step=0.5, max_value=8.0, format="%f")
doors = st.number_input("Kapı", min_value=2, max_value=5, format="%d")
# Radio button'lar
cruise = st.radio("Cruise", [0, 1])
sound = st.radio("Sound", [0, 1])
leather = st.radio("Leather", [0, 1])
# Tahmin butonu
if st.button('Tahmin Et'):
price = price_pred(make, model, trim, mileage, type, cylinder, liter, doors, cruise, sound, leather)
st.success(f'Tahmin Edilen Fiyat: ${price}')