Transformer-based item reduction tool
Project description
teshort teshort is a Python class designed for item reduction (short-form development) using transformer-based embeddings and KMeans clustering. It helps generate semantically representative short forms of psychological or survey items by selecting items closest to cluster centroids.
Key Features Embedding generation Generates embeddings for text items using SentenceTransformer (default: sentence-t5-xxl).
Optimal cluster search visualization Provides an elbow plot using average distances of nearest neighbors to help select an appropriate number of clusters.
Clustering Performs KMeans clustering to group items based on semantic similarity.
Short form selection Selects a specified number of items, choosing those closest to the cluster centroids from each cluster.
Save results Saves the selected short form items to a CSV file.
Class Structure python
teshort( file_path='result.csv', # File path to save results model_name='sentence-t5-xxl' # Transformer model to use )
Main Methods Method Description embedding(df) Generates and stores embeddings from the first column of a DataFrame. find_nclustes() Plots an elbow chart to assist in determining the optimal number of clusters. cluster_only(n_clusters) Performs KMeans clustering and returns DataFrame with cluster labels. short(n_clusters, n_items) Selects a specified number of items (closest to centroids) to create a short form. sav() Saves the selected short form items to a CSV file specified by file_path.
Input Data Requirements The DataFrame passed to embedding(df) and short() must:
Have the first column containing the text items to be embedded.
Have a proper index to uniquely identify each item (item number, ID, etc.).
Example Usage python from teshort_module import teshort # Assuming you saved it as teshort.py
Initialize class
model = teshort(file_path='short_form.csv')
Load data
import pandas as pd df = pd.read_csv('items.csv', index_col=0)
Generate embeddings
model.embedding(df)
Visualize optimal cluster number
model.find_nclustes()
Perform clustering (e.g., 5 clusters)
clustered_df = model.cluster_only(n_clusters=5)
Select short form items (e.g., total 15 items across 5 clusters)
short_df = model.short(n_clusters=5, n_items=15)
Save short form to file
model.sav() Notes Be sure to call embedding(df) before using cluster_only() or short().
find_nclustes() only visualizes average distance trends; it does not automatically select the cluster number.
The short() method distributes selected items approximately evenly across clusters.
Dependencies Install required packages:
pip install sentence-transformers scikit-learn matplotlib pandas numpy License This code is provided for research and educational purposes. Commercial use requires permission from the author. If you'd like, I can generate this as a properly formatted .md file or add badges (e.g., Python version, license) at the top. Let me know!
-- Sample Data Data used in previous studies has been collected and organized separately; you may check and review them if needed.
Sample data is provided for your convenience. We recommend using the 50positive dataset in particular, as it is suitable for generating text embeddings.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file teshort-0.0.1.tar.gz.
File metadata
- Download URL: teshort-0.0.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33a0bf53248de892209b5753e004bfa9551b4221572acef4d0d10f787703a654
|
|
| MD5 |
5e11c15ec8e25e643a9fbcf1e9e7e487
|
|
| BLAKE2b-256 |
720f7dad0eb05cfff5ef91bc7289d5a8df45a824c6f990c1a5f185571f4a5c09
|
File details
Details for the file teshort-0.0.1-py3-none-any.whl.
File metadata
- Download URL: teshort-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a794886603ddacfc7c2574403387f257e446f589873bf904da6b3668867c7f15
|
|
| MD5 |
97f4079c29545786f6d346987fb1fd4c
|
|
| BLAKE2b-256 |
a2f40c21ca6e3a99d930609e3576134fc673b461e03ac969d143de2822cf5ee9
|