Following this instruction : 1. Strictly adhere to PEP-8 standards, ensuring code is…
- User
Following this instruction :
- Strictly adhere to PEP-8 standards, ensuring code is clean, well-structured, and follows Python's style guide for readability.
- Use appropriate modules like
typingfor type hints, ensuring code clarity, maintainability, and leveraging Python's standard libraries effectively. - Focus on time, space, robustness, scalability; write optimized, efficient code that can handle large datasets and scale seamlessly.
- Implement comprehensive error handling, catching exceptions gracefully, providing meaningful error messages, and ensuring code robustness under unexpected conditions.
- Ensure the developer agent writes advanced, maintainable Python code, balancing performance and readability while adhering to best practices.
TASK : we have generalised , robustness , zero hard coding(mainly split, coulmns names case) generalised make professionally code , use advanced concepts of class and function , handing different task cluster different types , make generalised reusable . (plotly module)
from datasets import load_dataset
Load our data
data = load_dataset("My_dataset") data #output DatasetDict({ train: Dataset({ features: ['text', 'label'], num_rows: 8530 }) validation: Dataset({ features: ['text', 'label'], num_rows: 1066 }) test: Dataset({ features: ['text', 'label'], num_rows: 1066 }) })
data["train"][0, -1]
from sentence_transformers import SentenceTransformer
Create an embedding for each abstract
embedding_model = SentenceTransformer('thenlper/gte-small') embeddings = embedding_model.encode(abstracts, show_progress_bar=True)
from umap import UMAP
We reduce the input embeddings from 384 dimenions to 5 dimenions
umap_model = UMAP( n_components=5, min_dist=0.0, metric='cosine', random_state=42 ) reduced_embeddings = umap_model.fit_transform(embeddings)
from hdbscan import HDBSCAN
We fit the model and extract the clusters
hdbscan_model = HDBSCAN( min_cluster_size=50, metric='euclidean', cluster_selection_method='eom' ).fit(reduced_embeddings) clusters = hdbscan_model.labels_
How many clusters did we generate?
len(set(clusters))
import numpy as np
Print first three documents in cluster 0
cluster = 0 for index in np.where(clusters==cluster)[0][:3]: print(abstracts[index][:300] + "... \n")
import pandas as pd
Reduce 384-dimensional embeddings to 2 dimensions for easier visualization
reduced_embeddings = UMAP( n_components=2, min_dist=0.0, metric='cosine', random_state=42 ).fit_transform(embeddings)
Create dataframe
df = pd.DataFrame(reduced_embeddings, columns=["x", "y"]) df["title"] = titles df["cluster"] = [str(c) for c in clusters]
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.