Rename app.py to 1_Home.py
Browse files
1_Home.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
# Home Page
|
| 4 |
+
st.title("Welcome to the Hotel Data Analysis App")
|
| 5 |
+
st.markdown("""
|
| 6 |
+
This application is designed to help analyze hotel data, uncover insights, and build predictive models.
|
| 7 |
+
Navigate through the app using the sidebar to explore various functionalities.
|
| 8 |
+
### Features:
|
| 9 |
+
- **Introduction and About Data**: Learn about the dataset and download a sample file.
|
| 10 |
+
- **EDA and Feature Engineering**: Upload and analyze your dataset to uncover patterns and relationships.
|
| 11 |
+
- **Model Creation**: Build and evaluate machine learning models using your data.
|
| 12 |
+
- **Conclusion**: Summarize findings and key insights.
|
| 13 |
+
### Purpose:
|
| 14 |
+
This app is tailored for exploring relationships between features like price, ratings, discounts, cashback, and hotel categories, ultimately enabling data-driven decision-making.
|
| 15 |
+
**Get started by selecting a page from the sidebar!**
|
| 16 |
+
""")
|
app.py
DELETED
|
@@ -1,114 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
-
import seaborn as sns
|
| 6 |
-
import plotly.express as px
|
| 7 |
-
import warnings
|
| 8 |
-
from sklearn.linear_model import LogisticRegression
|
| 9 |
-
from sklearn.neighbors import KNeighborsClassifier
|
| 10 |
-
from sklearn.model_selection import train_test_split, cross_val_score
|
| 11 |
-
from sklearn.preprocessing import StandardScaler, LabelEncoder
|
| 12 |
-
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, log_loss
|
| 13 |
-
import optuna
|
| 14 |
-
from imblearn.over_sampling import SMOTE
|
| 15 |
-
from sklearn.preprocessing import PolynomialFeatures
|
| 16 |
-
|
| 17 |
-
warnings.filterwarnings('ignore')
|
| 18 |
-
|
| 19 |
-
if st.button("EDA"):
|
| 20 |
-
st.switch_page("pages/EDA.py")
|
| 21 |
-
|
| 22 |
-
# Streamlit App Title
|
| 23 |
-
st.title("Consumer Electronics Sales Prediction App")
|
| 24 |
-
|
| 25 |
-
# Upload CSV Dataset
|
| 26 |
-
uploaded_file = st.file_uploader("Upload CSV File", type=["csv"])
|
| 27 |
-
|
| 28 |
-
if uploaded_file:
|
| 29 |
-
data = pd.read_csv(uploaded_file)
|
| 30 |
-
df = data.copy()
|
| 31 |
-
st.write("### Raw Data:")
|
| 32 |
-
st.write(df.head())
|
| 33 |
-
|
| 34 |
-
# Data Preprocessing
|
| 35 |
-
df = df.rename(columns={'ProductCategory': 'Category', 'ProductBrand': 'Brand', 'ProductPrice': 'Price'})
|
| 36 |
-
df['Price'] = df['Price'].apply(lambda x: round(x, 2))
|
| 37 |
-
|
| 38 |
-
# Bin age into categories
|
| 39 |
-
bins = [0, 18, 35, 50, 65, 100]
|
| 40 |
-
labels = ['Child', 'Young Adult', 'Adult', 'Middle Aged', 'Senior']
|
| 41 |
-
df['age_bins'] = pd.cut(df['CustomerAge'], bins=bins, labels=labels, right=False)
|
| 42 |
-
|
| 43 |
-
# Encode age_bins to numerical values using LabelEncoder
|
| 44 |
-
le_age_bins = LabelEncoder()
|
| 45 |
-
df['age_bins'] = le_age_bins.fit_transform(df['age_bins'].astype(str))
|
| 46 |
-
|
| 47 |
-
# Show Data Description
|
| 48 |
-
st.write("### Data Description")
|
| 49 |
-
st.write(df.describe())
|
| 50 |
-
|
| 51 |
-
# Visualize Product Category Distribution
|
| 52 |
-
fig, ax = plt.subplots()
|
| 53 |
-
sns.countplot(x='Category', data=df, ax=ax, palette='viridis')
|
| 54 |
-
ax.set_title("Product Category Distribution")
|
| 55 |
-
st.pyplot(fig)
|
| 56 |
-
|
| 57 |
-
# Encode Categorical Features
|
| 58 |
-
le_category = LabelEncoder()
|
| 59 |
-
df['Category'] = le_category.fit_transform(df['Category'])
|
| 60 |
-
le_brand = LabelEncoder()
|
| 61 |
-
df['Brand'] = le_brand.fit_transform(df['Brand'])
|
| 62 |
-
|
| 63 |
-
# Feature Engineering with Polynomial Features
|
| 64 |
-
fv = df.drop(columns=['PurchaseIntent'])
|
| 65 |
-
cv = df['PurchaseIntent']
|
| 66 |
-
poly = PolynomialFeatures(degree=2, include_bias=False)
|
| 67 |
-
numeric_columns = [col for col in fv.select_dtypes(include=[float, int]).columns if col != 'ProductID']
|
| 68 |
-
poly_features = poly.fit_transform(fv[numeric_columns])
|
| 69 |
-
poly_feature_names = poly.get_feature_names_out(numeric_columns)
|
| 70 |
-
fv_with_poly = pd.DataFrame(poly_features, columns=poly_feature_names)
|
| 71 |
-
fv_with_poly = pd.concat([fv.reset_index(drop=True), fv_with_poly], axis=1)
|
| 72 |
-
|
| 73 |
-
# Handle Class Imbalance with SMOTE
|
| 74 |
-
smote = SMOTE()
|
| 75 |
-
X_resampled, y_resampled = smote.fit_resample(fv_with_poly, cv)
|
| 76 |
-
|
| 77 |
-
# Train-Test Split
|
| 78 |
-
X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2, random_state=42)
|
| 79 |
-
|
| 80 |
-
# Standardize the Data
|
| 81 |
-
scaler = StandardScaler()
|
| 82 |
-
X_train = scaler.fit_transform(X_train)
|
| 83 |
-
X_test = scaler.transform(X_test)
|
| 84 |
-
|
| 85 |
-
# Optuna Optimization
|
| 86 |
-
def objective(trial):
|
| 87 |
-
solver, penalty = trial.suggest_categorical("choices", [("lbfgs", "l2"), ("newton-cg", "l2"), ("sag", "l2"), ("saga", "l1"), ("saga", "l2"), ("saga", "elasticnet")])
|
| 88 |
-
C = trial.suggest_float("C", 0.01, 1000.0)
|
| 89 |
-
l1_ratio = trial.suggest_float("l1_ratio", 0, 1) if penalty == "elasticnet" else None
|
| 90 |
-
model = LogisticRegression(solver=solver, penalty=penalty, C=C, l1_ratio=l1_ratio if l1_ratio else None)
|
| 91 |
-
return -1 * cross_val_score(model, X_train, y_train, cv=5, scoring="neg_log_loss").mean()
|
| 92 |
-
|
| 93 |
-
study = optuna.create_study(direction="minimize")
|
| 94 |
-
study.optimize(objective, n_trials=100)
|
| 95 |
-
|
| 96 |
-
best_params = study.best_params
|
| 97 |
-
st.write("### Best Hyperparameters")
|
| 98 |
-
st.write(best_params)
|
| 99 |
-
|
| 100 |
-
# Train Final Model
|
| 101 |
-
final_model = LogisticRegression(**best_params)
|
| 102 |
-
final_model.fit(X_train, y_train)
|
| 103 |
-
acc = final_model.score(X_test, y_test)
|
| 104 |
-
st.write(f"### Test Accuracy: {acc:.2f}")
|
| 105 |
-
|
| 106 |
-
# Hugging Face Upload Section
|
| 107 |
-
st.write("#### Upload Model to Hugging Face")
|
| 108 |
-
if st.button("Upload to Hugging Face"):
|
| 109 |
-
import joblib
|
| 110 |
-
import huggingface_hub
|
| 111 |
-
joblib.dump(final_model, "model.joblib")
|
| 112 |
-
huggingface_hub.login(token="<YOUR_HUGGINGFACE_TOKEN>")
|
| 113 |
-
huggingface_hub.upload_file(path_or_fileobj="model.joblib", path_in_repo="model.joblib", repo_id="<your_repo>")
|
| 114 |
-
st.success("Model successfully uploaded!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|