ositamiles commited on
Commit
b4c57b9
·
verified ·
1 Parent(s): b0e214e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -79
app.py CHANGED
@@ -41,13 +41,9 @@ def load_data():
41
  # Load data
42
  df = load_data()
43
 
44
- # Identify price and demand columns
45
- price_col = [col for col in df.columns if 'price' in col.lower() and col != 'suggested_price']
46
- demand_col = [col for col in df.columns if 'demand' in col.lower()]
47
- target_col = 'suggested_price' if 'suggested_price' in df.columns else (price_col[0] if price_col else None)
48
-
49
- # Sidebar for navigation
50
  page = st.sidebar.selectbox("Choose a page", ["Data Explorer", "Model Training", "Price Prediction"])
 
51
 
52
  if page == "Data Explorer":
53
  st.title("Data Explorer")
@@ -59,82 +55,81 @@ if page == "Data Explorer":
59
  st.subheader("Data Visualization")
60
  fig, ax = plt.subplots(1, 2, figsize=(15, 5))
61
 
62
- if price_col and target_col:
63
- ax[0].scatter(df[price_col[0]], df[target_col])
64
- ax[0].set_xlabel(price_col[0])
65
- ax[0].set_ylabel(target_col)
66
- ax[0].set_title(f'{price_col[0]} vs {target_col}')
 
 
 
67
 
68
- if demand_col and target_col:
69
- ax[1].scatter(df[demand_col[0]], df[target_col])
70
- ax[1].set_xlabel(demand_col[0])
71
- ax[1].set_ylabel(target_col)
72
- ax[1].set_title(f'{demand_col[0]} vs {target_col}')
73
 
74
  st.pyplot(fig)
75
 
76
  elif page == "Model Training":
77
  st.title("Model Training")
78
 
79
- if not target_col:
80
- st.error("No suitable target column (suggested_price or another price column) found in the dataset.")
81
- else:
82
- # Data preprocessing
83
- categorical_cols = df.select_dtypes(include=['object']).columns
84
- numeric_cols = df.select_dtypes(include=['int64', 'float64']).columns
85
-
86
- # Remove target column from features if it exists
87
- feature_cols = [col for col in numeric_cols if col != target_col]
88
-
89
- encoders = {}
90
- for col in categorical_cols:
91
- encoders[col] = LabelEncoder()
92
- df[f'{col}_encoded'] = encoders[col].fit_transform(df[col])
93
- feature_cols.append(f'{col}_encoded')
94
-
95
- X = df[feature_cols]
96
- y = df[target_col]
97
-
98
- scaler = StandardScaler()
99
- X_scaled = scaler.fit_transform(X)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
- # Split the data
102
- X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
103
 
104
- # Model architecture
105
- model = Sequential([
106
- Dense(64, activation='relu', input_shape=(X_train.shape[1],)),
107
- Dense(32, activation='relu'),
108
- Dense(16, activation='relu'),
109
- Dense(1)
110
- ])
 
111
 
112
- model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae'])
 
 
 
 
 
113
 
114
- # Training
115
- if st.button("Train Model"):
116
- with st.spinner("Training in progress..."):
117
- history = model.fit(X_train, y_train, validation_split=0.2, epochs=100, batch_size=32, verbose=0)
118
-
119
- st.success("Model trained successfully!")
120
-
121
- # Plot training history
122
- fig, ax = plt.subplots(figsize=(10, 5))
123
- ax.plot(history.history['loss'], label='Training Loss')
124
- ax.plot(history.history['val_loss'], label='Validation Loss')
125
- ax.set_xlabel('Epoch')
126
- ax.set_ylabel('Loss')
127
- ax.legend()
128
- st.pyplot(fig)
129
-
130
- # Save model and preprocessing objects
131
- model.save('dynamic_pricing_model.h5')
132
- joblib.dump(scaler, 'scaler.pkl')
133
- joblib.dump(encoders, 'encoders.pkl')
134
- joblib.dump(feature_cols, 'feature_cols.pkl')
135
- joblib.dump(target_col, 'target_col.pkl')
136
-
137
- st.info("Model and preprocessing objects saved.")
138
 
139
  elif page == "Price Prediction":
140
  st.title("Price Prediction")
@@ -145,7 +140,10 @@ elif page == "Price Prediction":
145
  scaler = joblib.load('scaler.pkl')
146
  encoders = joblib.load('encoders.pkl')
147
  feature_cols = joblib.load('feature_cols.pkl')
148
- target_col = joblib.load('target_col.pkl')
 
 
 
149
 
150
  # User input
151
  input_data = {}
@@ -157,13 +155,11 @@ elif page == "Price Prediction":
157
  input_data[col] = encoders[original_col].transform([value])[0]
158
  else:
159
  if 'year' in col.lower():
160
- input_data[col] = st.slider(f"{col}", 2018, 2024, 2022)
161
- elif 'price' in col.lower():
162
- input_data[col] = st.slider(f"{col}", 20.0, 60.0, 40.0)
163
- elif 'sales' in col.lower():
164
- input_data[col] = st.slider(f"{col}", 100, 1000, 500)
165
  else:
166
- input_data[col] = st.slider(f"{col}", 0.1, 1.0, 0.5)
167
 
168
  # Prepare input for prediction
169
  input_df = pd.DataFrame([input_data])
@@ -172,7 +168,7 @@ elif page == "Price Prediction":
172
  # Make prediction
173
  if st.button("Predict Price"):
174
  predicted_price = model.predict(input_scaled)[0][0]
175
- st.success(f"Predicted {target_col}: ${predicted_price:.2f}")
176
  else:
177
  st.warning("Please train the model first!")
178
 
 
41
  # Load data
42
  df = load_data()
43
 
44
+ # Sidebar for navigation and target column selection
 
 
 
 
 
45
  page = st.sidebar.selectbox("Choose a page", ["Data Explorer", "Model Training", "Price Prediction"])
46
+ target_col = st.sidebar.selectbox("Select target column", df.columns.tolist())
47
 
48
  if page == "Data Explorer":
49
  st.title("Data Explorer")
 
55
  st.subheader("Data Visualization")
56
  fig, ax = plt.subplots(1, 2, figsize=(15, 5))
57
 
58
+ numeric_cols = df.select_dtypes(include=['int64', 'float64']).columns
59
+ x_col1 = st.selectbox("Select X-axis for first plot", numeric_cols, index=0)
60
+ x_col2 = st.selectbox("Select X-axis for second plot", numeric_cols, index=min(1, len(numeric_cols)-1))
61
+
62
+ ax[0].scatter(df[x_col1], df[target_col])
63
+ ax[0].set_xlabel(x_col1)
64
+ ax[0].set_ylabel(target_col)
65
+ ax[0].set_title(f'{x_col1} vs {target_col}')
66
 
67
+ ax[1].scatter(df[x_col2], df[target_col])
68
+ ax[1].set_xlabel(x_col2)
69
+ ax[1].set_ylabel(target_col)
70
+ ax[1].set_title(f'{x_col2} vs {target_col}')
 
71
 
72
  st.pyplot(fig)
73
 
74
  elif page == "Model Training":
75
  st.title("Model Training")
76
 
77
+ # Data preprocessing
78
+ categorical_cols = df.select_dtypes(include=['object']).columns
79
+ numeric_cols = df.select_dtypes(include=['int64', 'float64']).columns
80
+
81
+ # Remove target column from features
82
+ feature_cols = [col for col in numeric_cols if col != target_col]
83
+
84
+ encoders = {}
85
+ for col in categorical_cols:
86
+ encoders[col] = LabelEncoder()
87
+ df[f'{col}_encoded'] = encoders[col].fit_transform(df[col])
88
+ feature_cols.append(f'{col}_encoded')
89
+
90
+ X = df[feature_cols]
91
+ y = df[target_col]
92
+
93
+ scaler = StandardScaler()
94
+ X_scaled = scaler.fit_transform(X)
95
+
96
+ # Split the data
97
+ X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
98
+
99
+ # Model architecture
100
+ model = Sequential([
101
+ Dense(64, activation='relu', input_shape=(X_train.shape[1],)),
102
+ Dense(32, activation='relu'),
103
+ Dense(16, activation='relu'),
104
+ Dense(1)
105
+ ])
106
+
107
+ model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae'])
108
+
109
+ # Training
110
+ if st.button("Train Model"):
111
+ with st.spinner("Training in progress..."):
112
+ history = model.fit(X_train, y_train, validation_split=0.2, epochs=100, batch_size=32, verbose=0)
113
 
114
+ st.success("Model trained successfully!")
 
115
 
116
+ # Plot training history
117
+ fig, ax = plt.subplots(figsize=(10, 5))
118
+ ax.plot(history.history['loss'], label='Training Loss')
119
+ ax.plot(history.history['val_loss'], label='Validation Loss')
120
+ ax.set_xlabel('Epoch')
121
+ ax.set_ylabel('Loss')
122
+ ax.legend()
123
+ st.pyplot(fig)
124
 
125
+ # Save model and preprocessing objects
126
+ model.save('dynamic_pricing_model.h5')
127
+ joblib.dump(scaler, 'scaler.pkl')
128
+ joblib.dump(encoders, 'encoders.pkl')
129
+ joblib.dump(feature_cols, 'feature_cols.pkl')
130
+ joblib.dump(target_col, 'target_col.pkl')
131
 
132
+ st.info("Model and preprocessing objects saved.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
  elif page == "Price Prediction":
135
  st.title("Price Prediction")
 
140
  scaler = joblib.load('scaler.pkl')
141
  encoders = joblib.load('encoders.pkl')
142
  feature_cols = joblib.load('feature_cols.pkl')
143
+ saved_target_col = joblib.load('target_col.pkl')
144
+
145
+ if saved_target_col != target_col:
146
+ st.warning(f"Warning: The current target column ({target_col}) is different from the one used for training ({saved_target_col}). Consider retraining the model.")
147
 
148
  # User input
149
  input_data = {}
 
155
  input_data[col] = encoders[original_col].transform([value])[0]
156
  else:
157
  if 'year' in col.lower():
158
+ input_data[col] = st.slider(f"{col}", int(df[col].min()), int(df[col].max()), int(df[col].mean()))
159
+ elif 'price' in col.lower() or 'sales' in col.lower():
160
+ input_data[col] = st.slider(f"{col}", float(df[col].min()), float(df[col].max()), float(df[col].mean()))
 
 
161
  else:
162
+ input_data[col] = st.slider(f"{col}", float(df[col].min()), float(df[col].max()), float(df[col].mean()))
163
 
164
  # Prepare input for prediction
165
  input_df = pd.DataFrame([input_data])
 
168
  # Make prediction
169
  if st.button("Predict Price"):
170
  predicted_price = model.predict(input_scaled)[0][0]
171
+ st.success(f"Predicted {saved_target_col}: {predicted_price:.2f}")
172
  else:
173
  st.warning("Please train the model first!")
174