Skip to main content

A library for String Grammar Fuzzy C-Medians

Project description

What is String Grammar Fuzzy Clustering?

String Grammar Fuzzy Clustering is a clustering framework designed for syntactic or structural pattern recognition, where each data instance is represented not as a numeric vector but as a string that encodes structural information.

Unlike conventional numerical clustering method (e.g., Fuzzy C-Means), which assume that data have a fixed-length feature vector whereas structural clustering method operates directly on string data whose lengths and internal structures may vary.

In this approach, each pattern is described by a sequence of primitives (symbols) defined by grammatical rules. This is similar to how a sentence is formed from characters following syntax rules.

To measure similarity between strings, the method employs the Levenshtein distance[1], which counts the minimum number of edit operations (insertions, deletions, substitutions) required to transform on string into another.

The "fuzzy" aspect of this framework allows each string to belong to multiple clusters, with a membership degree that reflects how strongly it is associated with each cluster. This provides a more flexible and realistic clustering behavior compared to traditional "hard" clustering, which forces each sample to belong to only one group.

About This Library

This Python library introduces an algorithm belonging to the String Grammar Fuzzy Clustering framework, namely the String Grammar Fuzzy C-Medians (sgFCMed).

String Grammar Fuzzy C-Medians (sgFCMed)[2]

The sgFCMed algorithm extends the conventional Fuzzy C-Medians to handle string-based or syntactic data by using the Levenshtein distance instead of Euclidean distance. Each cluster is represented by a prototype string that minimizes the weighted sum of distances to all strings in the cluster, and each sample has a fuzzy membership value indicating its degree of belonging to different clusters. This method effectively clusters variable-length symbolic data, capturing structural similarity without requiring vector representations.

Key Features:

  • Works directly on string-encoded or grammar-based data
  • Uses Levenshtein distance for similarity measurement
  • Maintains a fuzzy membership matrix (U) that quantifies how strongly each string belongs to each cluster
  • Parallelized medoid and modified median updates
  • Ideal for datasets with overlapping but relatively clean patterns

**Please be noted that this sgFCMed can be used for academic and research purposes only. Please also cite this paper [2].**

Reference

[1] S. K. Fu, Syntactic Pattern Recognition and Applications, 1982, Prentice-Hall, Zbl0521.68091. [2] Atcharin Klomsae, Sansanee Auephanwiriyakul, and Nipon Theera-Umpon, “A Novel String Grammar Fuzzy C-Medians,” Proceedings of the 2015 IEEE International Conference on Fuzzy Systems, Istanbul, Turkey, August 2015.

Installation

You can install the library using pip:

pip install stringGrammar

USAGE

Example Code

import random
from sgFCMed import SGFCMed # Import the clustering class

if __name__ == "__main__":
    # Set random seed for reproducibility
    random.seed(42)

    # Define a list of strings to cluster
    data = ["book", "back", "boon", "cook", "look", "cool", "kick", "lack", "rack", "tack"]

    # ======== Example 1: String Grammar Fuzzy C-Medians (sgFCMed) ========
    print("=== String Grammar Fuzzy C-Medians (sgFCMed) ===")
    model_fcmed = SGFCMed(C=2, m=2.0)   # 2 clusters, fuzzifier m = 2.0
    model_fcmed.fit(data)

    print("Prototypes:", model_fcmed.prototypes())
    print("\nMembership Matrix (U):")
    for s, u in zip(data, model_fcmed.membership()):
        print(f"{s:>6}{[round(val, 3) for val in u]}")

    # Predict clusters for new data
    new_data = ["hack", "rook", "cook"]
    preds = model_fcmed.predict(new_data)
    print("\nPredictions:")
    for s, c in zip(new_data, preds):
        print(f"{s} → Cluster {c+1}")

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

sgfcmed-0.0.0.tar.gz (2.9 MB view details)

Uploaded Source

Built Distribution

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

sgfcmed-0.0.0-py3-none-any.whl (3.3 MB view details)

Uploaded Python 3

File details

Details for the file sgfcmed-0.0.0.tar.gz.

File metadata

  • Download URL: sgfcmed-0.0.0.tar.gz
  • Upload date:
  • Size: 2.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for sgfcmed-0.0.0.tar.gz
Algorithm Hash digest
SHA256 cc2b126c7f9f89eeeaf07df8c87eb9ba8b209941a92fd8a42b53f8f0d7eea6b3
MD5 5dd471bf9915c43addc4cc95d8dc68dc
BLAKE2b-256 962aed1666446577964f7dddb079b5bdc807a1d0adebab61d2fb22e80e318450

See more details on using hashes here.

File details

Details for the file sgfcmed-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: sgfcmed-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for sgfcmed-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9f52fde99fa6e7a9addf27e0d2fe7e04401112434b4c00df6cc0ce0ad0b6da0f
MD5 6ba25879b13a855683b9063c6c6bd4a4
BLAKE2b-256 7239cf44cbefd6dcf0fdc374353224dfa1870781a7a99cceb9ff379c79c97694

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