MasterSoftware commited on
Commit
4ad8f72
1 Parent(s): eb42b70

Upload 2 files

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