-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
150 lines (119 loc) · 4.94 KB
/
app.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'''import pandas as pd
import pickle as pk
import streamlit as st
# Load the model
model = pk.load(open('model.pkl', 'rb'))
# Set up the Streamlit app
st.header('Car Price Prediction ML Model')
# Load and preprocess data
data = pd.read_csv('Cardetails.csv')
def get_brand_name(car_name):
return car_name.split(' ')[0].strip()
data['name'] = data['name'].apply(get_brand_name)
# Load mappings
brand_mapping = {brand: idx + 1 for idx, brand in enumerate(data['name'].unique())}
fuel_mapping = {fuel: idx + 1 for idx, fuel in enumerate(data['fuel'].unique())}
seller_type_mapping = {seller: idx + 1 for idx, seller in enumerate(data['seller_type'].unique())}
transmission_mapping = {transmission: idx + 1 for idx, transmission in enumerate(data['transmission'].unique())}
owner_mapping = {owner: idx + 1 for idx, owner in enumerate(data['owner'].unique())}
# Create user input widgets
name = st.selectbox('Select Car Brand', data['name'].unique())
year = st.slider('Car Manufactured Year', 1994, 2024)
km_driven = st.slider('No of kms Driven', 11, 200000)
fuel = st.selectbox('Fuel type', data['fuel'].unique())
seller_type = st.selectbox('Seller Type', data['seller_type'].unique())
transmission = st.selectbox('Transmission Type', data['transmission'].unique())
owner = st.selectbox('Owner Type', data['owner'].unique())
mileage = st.slider('Car Mileage', 10, 40)
engine = st.slider('Engine CC', 700, 5000)
max_power = st.slider('Max Power', 0, 200)
seats = st.slider('No of Seats', 5, 10)
# Prediction
if st.button("Predict"):
# Prepare input data
input_data = {
'name': brand_mapping[name],
'year': year,
'km_driven': km_driven,
'fuel': fuel_mapping[fuel],
'seller_type': seller_type_mapping[seller_type],
'transmission': transmission_mapping[transmission],
'owner': owner_mapping[owner],
'mileage': mileage,
'engine': engine,
'max_power': max_power,
'seats': seats
}
input_df = pd.DataFrame([input_data])
# Display input data for debugging
st.write("Input Data for Prediction:")
st.write(input_df)
# Make prediction
car_price = model.predict(input_df)
# Display result
st.markdown(f'Car Price is going to be ₹{car_price[0]:,.2f}')
'''
import pandas as pd
import pickle as pk
import streamlit as st
# Load the model
model = pk.load(open('model.pkl', 'rb'))
# Set up the Streamlit app
st.set_page_config(page_title='Car Price Prediction', layout='wide')
# App header and description
st.title('🚗 Car Price Prediction ML Model')
st.markdown("""
This application predicts the price of a car based on various features.
Please provide the details of the car, and the model will estimate the price.
""")
# Load and preprocess data
data = pd.read_csv('Cardetails.csv')
def get_brand_name(car_name):
return car_name.split(' ')[0].strip()
data['name'] = data['name'].apply(get_brand_name)
# Load mappings
brand_mapping = {brand: idx + 1 for idx, brand in enumerate(data['name'].unique())}
fuel_mapping = {fuel: idx + 1 for idx, fuel in enumerate(data['fuel'].unique())}
seller_type_mapping = {seller: idx + 1 for idx, seller in enumerate(data['seller_type'].unique())}
transmission_mapping = {transmission: idx + 1 for idx, transmission in enumerate(data['transmission'].unique())}
owner_mapping = {owner: idx + 1 for idx, owner in enumerate(data['owner'].unique())}
# Layout for input widgets
with st.sidebar:
st.header('Enter Car Details')
name = st.selectbox('Select Car Brand', data['name'].unique())
year = st.slider('Car Manufactured Year', 1994, 2024)
km_driven = st.slider('No of kms Driven', 11, 200000)
fuel = st.selectbox('Fuel Type', data['fuel'].unique())
seller_type = st.selectbox('Seller Type', data['seller_type'].unique())
transmission = st.selectbox('Transmission Type', data['transmission'].unique())
owner = st.selectbox('Owner Type', data['owner'].unique())
mileage = st.slider('Car Mileage (km/l)', 10, 40)
engine = st.slider('Engine CC', 700, 5000)
max_power = st.slider('Max Power (bhp)', 0, 200)
seats = st.slider('No of Seats', 5, 10)
# Prediction section
st.header('📊 Prediction Result')
if st.button("Predict"):
# Prepare input data
input_data = {
'name': brand_mapping[name],
'year': year,
'km_driven': km_driven,
'fuel': fuel_mapping[fuel],
'seller_type': seller_type_mapping[seller_type],
'transmission': transmission_mapping[transmission],
'owner': owner_mapping[owner],
'mileage': mileage,
'engine': engine,
'max_power': max_power,
'seats': seats
}
input_df = pd.DataFrame([input_data])
# Display input data for debugging
st.write("### Input Data for Prediction:")
st.dataframe(input_df)
# Make prediction
car_price = model.predict(input_df)
# Display result
st.write("### Predicted Car Price:")
st.markdown(f'**Estimated Price:** ₹{car_price[0]:,.2f}')