57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
|
from flask import Flask, render_template
|
||
|
import pandas as pd
|
||
|
from sklearn.manifold import TSNE
|
||
|
from sklearn.preprocessing import LabelEncoder
|
||
|
from sklearn.cluster import KMeans
|
||
|
import matplotlib.pyplot as plt
|
||
|
from io import BytesIO
|
||
|
import base64
|
||
|
|
||
|
app = Flask(__name__)
|
||
|
|
||
|
@app.route('/')
|
||
|
def index():
|
||
|
# Load the dataset
|
||
|
df = pd.read_csv('top_240_restaurants_recommended_in_los_angeles_2.csv')
|
||
|
|
||
|
# Select the columns for clustering
|
||
|
columns_for_clustering = ['Rank', 'StarRating', 'NumberOfReviews', 'Style']
|
||
|
|
||
|
# Encode the 'Style' column
|
||
|
label_encoder = LabelEncoder()
|
||
|
df['Style_encoded'] = label_encoder.fit_transform(df['Style'])
|
||
|
|
||
|
# Select the columns for clustering
|
||
|
data_for_clustering = df[['Rank', 'StarRating', 'NumberOfReviews', 'Style_encoded']]
|
||
|
|
||
|
# Perform t-SNE for dimensionality reduction
|
||
|
tsne = TSNE(n_components=2, random_state=42)
|
||
|
tsne_result = tsne.fit_transform(data_for_clustering)
|
||
|
|
||
|
# Perform k-means clustering
|
||
|
kmeans = KMeans(n_clusters=3, random_state=42)
|
||
|
df['Cluster'] = kmeans.fit_predict(data_for_clustering)
|
||
|
|
||
|
# Add t-SNE results to the DataFrame
|
||
|
df['tsne_1'] = tsne_result[:, 0]
|
||
|
df['tsne_2'] = tsne_result[:, 1]
|
||
|
|
||
|
# Save the figure to a BytesIO object
|
||
|
plt.scatter(df['tsne_1'], df['tsne_2'], c=df['Cluster'], cmap='viridis')
|
||
|
plt.title('t-SNE Clustering')
|
||
|
plt.xlabel('t-SNE Component 1')
|
||
|
plt.ylabel('t-SNE Component 2')
|
||
|
plt.tight_layout()
|
||
|
img_data = BytesIO()
|
||
|
plt.savefig(img_data, format='png')
|
||
|
img_data.seek(0)
|
||
|
|
||
|
# Convert the BytesIO object to base64 for embedding in HTML
|
||
|
img_base64 = base64.b64encode(img_data.getvalue()).decode('utf-8')
|
||
|
|
||
|
# Render the HTML template with the embedded image
|
||
|
return render_template('index.html', img_base64=img_base64)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app.run(debug=True)
|