Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

teshort-0.0.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

teshort-0.0.2-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file teshort-0.0.2.tar.gz.

File metadata

  • Download URL: teshort-0.0.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for teshort-0.0.2.tar.gz
Algorithm Hash digest
SHA256 42713232386c359324bd0ca2302d769226f2c0528248ba0f1522970dc87849e9
MD5 a80a9354245b91350e41344808818571
BLAKE2b-256 4f351c6006a8e13db419cf109efe89ffbb78fd70f872d5940b08e3e0849a14a6

See more details on using hashes here.

File details

Details for the file teshort-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: teshort-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for teshort-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e8189bede0cb9e92c60e5f8f616a7aa2233001c226abd315efd2ca0376e9ccd3
MD5 5e59196a0f8bbbc8fcff2274943729b2
BLAKE2b-256 acdfe8deec21418454579f622b14be8730fc3d807a393c88c5d14b5e5764b3ff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page