Mainpy (Customer Segmentation)
Mainpy (Customer Segmentation)
# app = FastAPI()
# @app.get("/", response_class=HTMLResponse)
# async def main():
# with open("index.html", "r") as f:
# return f.read()
# @app.post("/upload/", response_class=HTMLResponse)
# async def handle_upload(file: UploadFile = File(...)):
# if file.filename.endswith('.csv'):
# dataframe = pd.read_csv(BytesIO(await file.read()))
# # Convert plots to inline HTML images and wrap every two plots side by
side
# html_images = []
# for i, img in enumerate(plot_images):
# buffer = BytesIO()
# img.savefig(buffer, format="png")
# buffer.seek(0)
# image_png = buffer.getvalue()
# encoded = base64.b64encode(image_png).decode('utf-8')
# html_img = f'<img src="data:image/png;base64,{encoded}"
class="plot">'
# if i % 2 == 0 and i != 0:
# html_images.append('</div>')
# if i % 2 == 0:
# html_images.append('<div class="row">') # Start a new row every
two plots
# html_images.append(html_img)
# if len(html_images) % 2 != 0:
# html_images.append('</div>') # Close the last row if there's an odd
number of plots
# def process_data_and_plot(df):
# figs = []
# return figs
# import webbrowser
# import asyncio
# from fastapi import FastAPI, File, UploadFile
# from fastapi.responses import HTMLResponse
# from fastapi.staticfiles import StaticFiles
# import pandas as pd
# from io import BytesIO
# import base64
# import matplotlib.pyplot as plt
# import seaborn as sns
# from sklearn.cluster import KMeans
# app = FastAPI()
# return HTMLResponse(html_content)
# else:
# return HTMLResponse("Invalid file format. Please upload a CSV.",
status_code=400)
# def process_data_and_plot(df):
# figs = []
# return html_content
# import webbrowser
# import asyncio
# from fastapi import FastAPI, File, UploadFile
# from fastapi.responses import HTMLResponse
# from fastapi.staticfiles import StaticFiles
# import pandas as pd
# import matplotlib.pyplot as plt
# import seaborn as sns
# from sklearn.cluster import KMeans
# from io import BytesIO
# import base64
# @app.post("/upload", response_class=HTMLResponse)
# async def upload_csv(file: UploadFile = File(...)):
# if not file.filename.endswith(".csv"):
# return HTMLResponse(content="<p>Invalid file format. Please upload a CSV
file.</p>", status_code=400)
# return HTMLResponse(content=final_html)
# # Univariate Analysis
# for col in ['Age', 'Annual Income (k$)', 'Spending Score (1-100)']:
# fig, ax = plt.subplots(figsize=plot_size, dpi=plot_dpi) # Smaller plot
# sns.kdeplot(df[col], fill=True, ax=ax)
# figs.append(fig)
# # Bivariate Analysis
# fig, ax = plt.subplots(figsize=plot_size, dpi=plot_dpi) # Smaller plot
# sns.scatterplot(data=df, x='Annual Income (k$)', y='Spending Score (1-100)',
ax=ax)
# figs.append(fig)
# # Clustering Analysis
# kmeans = KMeans(n_clusters=3)
# df['Income Cluster'] = kmeans.fit_predict(df[['Annual Income (k$)']])
# fig, ax = plt.subplots(figsize=plot_size, dpi=plot_dpi) # Smaller plot
# sns.scatterplot(data=df, x='Annual Income (k$)', y='Spending Score (1-100)',
hue='Income Cluster', ax=ax)
# figs.append(fig)
# # Multivariate Clustering
# kmeans = KMeans(n_clusters=5)
# df['Cluster'] = kmeans.fit_predict(df[['Age', 'Annual Income (k$)', 'Spending
Score (1-100)']])
# fig, ax = plt.subplots(figsize=plot_size, dpi=plot_dpi) # Smaller plot
# sns.scatterplot(data=df, x='Annual Income (k$)', y='Spending Score (1-100)',
hue='Cluster', ax=ax)
# figs.append(fig)
# return figs