File size: 2,698 Bytes
0744d17
1e4d774
 
 
 
 
 
 
 
 
 
d3ad973
1e4d774
 
a889f18
e358224
1e4d774
e358224
1e4d774
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0744d17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import streamlit as st
import pandas as pd
import numpy as np

from sklearn.linear_model import Ridge
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler

# === Load dataset ===
@st.cache_data
def load_data():
    df = pd.read_csv("src/Car.csv")  # Make sure this file exists in your directory
    return df

df = load_data()

st.title("🚗 Car Price Predictor App")


# === Data Preprocessing ===
X = df.drop(columns=['selling_price'])
y = df['selling_price']

# Manually defined categorical columns
categorical_cols = ['name', 'fuel', 'seller_type', 'transmission', 'owner']
numerical_cols = [col for col in X.columns if col not in categorical_cols]

# Label Encoding
label_encoders = {}
for col in categorical_cols:
    le = LabelEncoder()
    X[col] = le.fit_transform(X[col])
    label_encoders[col] = le

# Scaling
scaler = StandardScaler()
X[numerical_cols] = scaler.fit_transform(X[numerical_cols])

# Train Model
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = Ridge(alpha=1.0)
model.fit(X_train, y_train)

# === Input UI ===
st.sidebar.header("Enter Car Details")
car_name = st.sidebar.selectbox("Car Name", df['name'].unique())
year = st.sidebar.number_input("Year", min_value=1990, max_value=2025, step=1)
km_driven = st.sidebar.number_input("Kilometers Driven", min_value=0)
fuel = st.sidebar.selectbox("Fuel Type", df['fuel'].unique())
seller_type = st.sidebar.selectbox("Seller Type", df['seller_type'].unique())
transmission = st.sidebar.selectbox("Transmission", df['transmission'].unique())
owner = st.sidebar.selectbox("Owner Type", df['owner'].unique())
mileage = st.sidebar.number_input("Mileage (kmpl)", min_value=0.0)
engine = st.sidebar.number_input("Engine (CC)", min_value=500.0)
seats = st.sidebar.number_input("Seats", min_value=2, max_value=10)

# === Predict Button ===
if st.sidebar.button("Predict Price"):
    input_df = pd.DataFrame({
        'name': [car_name],
        'year': [year],
        'km_driven': [km_driven],
        'fuel': [fuel],
        'seller_type': [seller_type],
        'transmission': [transmission],
        'owner': [owner],
        'mileage': [mileage],
        'engine': [engine],
        'seats': [seats]
    })

    # Apply Label Encoding
    for col in categorical_cols:
        le = label_encoders[col]
        input_df[col] = le.transform(input_df[col])

    # Apply Scaling
    input_df[numerical_cols] = scaler.transform(input_df[numerical_cols])

    # Predict
    pred_price = model.predict(input_df)[0]
    pred_price = max(0, round(pred_price))

    st.success(f"💰 Predicted Selling Price: ₹{pred_price}")