Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +77 -0
- cars.xls +0 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# METEHAN AYHAN
|
2 |
+
|
3 |
+
import pandas as pd
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
from sklearn.linear_model import LinearRegression
|
6 |
+
from sklearn.metrics import mean_squared_error, r2_score
|
7 |
+
from sklearn.pipeline import Pipeline
|
8 |
+
from sklearn.compose import ColumnTransformer
|
9 |
+
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
10 |
+
import streamlit as st
|
11 |
+
|
12 |
+
df = pd.read_excel('cars.xls')
|
13 |
+
x = df.drop('Price', axis=1)
|
14 |
+
y = df[['Price']]
|
15 |
+
|
16 |
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
|
17 |
+
|
18 |
+
preprocessor = ColumnTransformer(
|
19 |
+
transformers=[
|
20 |
+
('num', StandardScaler(), ['Mileage', 'Cylinder', 'Liter', 'Doors']), #numeric columns
|
21 |
+
('cat', OneHotEncoder(), ['Make', 'Model', 'Trim', 'Type']) #categorical columns
|
22 |
+
]
|
23 |
+
)
|
24 |
+
|
25 |
+
model = LinearRegression()
|
26 |
+
pipeline = Pipeline(steps=[('preprocessor', preprocessor), ('regressor', model)])
|
27 |
+
pipeline.fit(x_train, y_train)
|
28 |
+
pred = pipeline.predict(x_test)
|
29 |
+
|
30 |
+
rmse = mean_squared_error(pred, y_test) ** .5
|
31 |
+
r2 = r2_score(pred, y_test)
|
32 |
+
|
33 |
+
def price_pred(make, model, trim, mileage, car_type, cylinder, liter, doors, cruise, sound, leather):
|
34 |
+
input_df = pd.DataFrame({
|
35 |
+
'Make': [make],
|
36 |
+
'Model': [model],
|
37 |
+
'Trim': [trim],
|
38 |
+
'Mileage': [mileage],
|
39 |
+
'Type': [car_type],
|
40 |
+
'Cylinder': [cylinder],
|
41 |
+
'Liter': [liter],
|
42 |
+
'Doors': [doors],
|
43 |
+
'Cruise': [cruise],
|
44 |
+
'Sound': [sound],
|
45 |
+
'Leather': [leather]
|
46 |
+
})
|
47 |
+
|
48 |
+
prediction = pipeline.predict(input_df)[0]
|
49 |
+
return prediction
|
50 |
+
|
51 |
+
|
52 |
+
st.title('MLOps - Car Price Prediction :red_car:')
|
53 |
+
st.write('Enter Car Details to get the Price Prediction')
|
54 |
+
|
55 |
+
def main():
|
56 |
+
make = st.selectbox('Make', df['Make'].unique())
|
57 |
+
model = st.selectbox('Model', df[df['Make'] == make]['Model'].unique())
|
58 |
+
trim = st.selectbox('Trim', df[(df['Make'] == make) & (df['Model'] == model)]['Trim'].unique())
|
59 |
+
mileage = st.number_input('Mileage', 200, 60000)
|
60 |
+
car_type = st.selectbox('Type', df['Type'].unique())
|
61 |
+
cylinder = st.selectbox('Cylinder', df['Cylinder'].unique())
|
62 |
+
liter = st.number_input('Liter', 1,6)
|
63 |
+
doors = st.selectbox('Doors', df['Doors'].unique())
|
64 |
+
cruise = st.radio('Cruise',[0,1])
|
65 |
+
sound = st.radio('Sound',[0,1])
|
66 |
+
leather = st.radio('Leather',[0,1])
|
67 |
+
|
68 |
+
if st.button('Predict'):
|
69 |
+
price = price_pred(make, model, trim, mileage, car_type, cylinder, liter, doors, cruise, sound, leather)
|
70 |
+
price = float(price)
|
71 |
+
st.write(f'The predicted price is : ${price:.2f}')
|
72 |
+
|
73 |
+
if __name__ == '__main__':
|
74 |
+
main()
|
75 |
+
|
76 |
+
|
77 |
+
|
cars.xls
ADDED
Binary file (142 kB). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
scikit-learn
|
3 |
+
streamlit
|
4 |
+
openpyxl
|