positional-vectorizer


Namepositional-vectorizer JSON
Version 0.0.7 PyPI version JSON
download
home_pagehttps://github.com/timotta/positional-vectorizer
SummaryPositional Vectorizer is a scikit-learn transformer that converts text to bag of words vector using a positional ranking algorithm as score
upload_time2024-05-01 23:27:41
maintainerNone
docs_urlNone
authorTiago Albineli Motta
requires_python<3.12,>=3.11
licensenew BSD
keywords machine learning embedding vectorizer scikit-learn text nlp
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Positional Vectorizer

The Positional Vectorizer is a transformer in scikit-learn designed to transform text into a bag of words vector using a positional ranking algorithm to assign scores. Similar to scikit-learn's CountVectorizer and TFIDFVectorizer, it assigns a value to each dimension based on the term's position in the original text.

## How to use

```bash
pip install positional-vectorizer
```

Using to generate de text vectors

```python
from positional_vectorizer import PositionalVectorizer

input_texts = ["my text here", "other text here"]

vectorizer = PositionalVectorizer()
vectorizer.fit(input_texts)

encoded_texts = vectorizer.transform(input_texts)
```

Using with scikit-learn pipeline

```python
from positional_vectorizer import PositionalVectorizer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import SGDClassifier

pipeline = Pipeline([
    ('vect', PositionalVectorizer(ngram_range=(1, 2))),
    ('clf', SGDClassifier(random_state=42, loss='modified_huber'))
])

pipeline.fit(X_train, y_train)
```

## Why this new vectorizer?

Text embeddings based on bag-of-words using count, binary, or TF-IDF normalization are highly effective in most scenarios. However, in certain cases, such as those involving languages like Latin, the position of terms becomes crucial, which these techniques fail to capture.

For instance, consider the importance of word position in a Portuguese classification task distinguishing between a smartphone device and a smartphone accessory. In traditional bag-of-words approaches with stop words removed, the following titles yield identical representations:

* "xiaomi com fone de ouvido" => {"xiaomi", "fone", "ouvido"}
* "fone de ouvido do xiaomi" => {"xiaomi", "fone", "ouvido"}

As demonstrated, the order of words significantly alters the meaning, but this meaning is not reflected in the vectorization.

One common workaround is to employ n-grams instead of single words, but this can inflate the feature dimensionality, potentially increasing the risk of overfitting.

## How it works

The value in each dimension is calculated as `1 / math.log(rank + 1)` (similar to the Discounted Cumulative Gain formula), where the rank denotes the position of the corresponding term, starting from 1.

If a term appears multiple times in the text, only its lowest rank is taken into account.

## TODO

* Test the common parameters of _VectorizerMixin to identify potential issues when upgrading scikit-learn. Currently, only the `ngrams_range` and `analyzer` parameters are automatically tested.
* Implement the max_features parameter.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/timotta/positional-vectorizer",
    "name": "positional-vectorizer",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.12,>=3.11",
    "maintainer_email": null,
    "keywords": "machine learning, embedding, vectorizer, scikit-learn, text, NLP",
    "author": "Tiago Albineli Motta",
    "author_email": "timotta@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/64/0f/d1e144903208c8e2a9b70e2c51b3e1d3769fc3b640269c0b320255cd9adc/positional_vectorizer-0.0.7.tar.gz",
    "platform": null,
    "description": "# Positional Vectorizer\n\nThe Positional Vectorizer is a transformer in scikit-learn designed to transform text into a bag of words vector using a positional ranking algorithm to assign scores. Similar to scikit-learn's CountVectorizer and TFIDFVectorizer, it assigns a value to each dimension based on the term's position in the original text.\n\n## How to use\n\n```bash\npip install positional-vectorizer\n```\n\nUsing to generate de text vectors\n\n```python\nfrom positional_vectorizer import PositionalVectorizer\n\ninput_texts = [\"my text here\", \"other text here\"]\n\nvectorizer = PositionalVectorizer()\nvectorizer.fit(input_texts)\n\nencoded_texts = vectorizer.transform(input_texts)\n```\n\nUsing with scikit-learn pipeline\n\n```python\nfrom positional_vectorizer import PositionalVectorizer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.linear_model import SGDClassifier\n\npipeline = Pipeline([\n    ('vect', PositionalVectorizer(ngram_range=(1, 2))),\n    ('clf', SGDClassifier(random_state=42, loss='modified_huber'))\n])\n\npipeline.fit(X_train, y_train)\n```\n\n## Why this new vectorizer?\n\nText embeddings based on bag-of-words using count, binary, or TF-IDF normalization are highly effective in most scenarios. However, in certain cases, such as those involving languages like Latin, the position of terms becomes crucial, which these techniques fail to capture.\n\nFor instance, consider the importance of word position in a Portuguese classification task distinguishing between a smartphone device and a smartphone accessory. In traditional bag-of-words approaches with stop words removed, the following titles yield identical representations:\n\n* \"xiaomi com fone de ouvido\" => {\"xiaomi\", \"fone\", \"ouvido\"}\n* \"fone de ouvido do xiaomi\" => {\"xiaomi\", \"fone\", \"ouvido\"}\n\nAs demonstrated, the order of words significantly alters the meaning, but this meaning is not reflected in the vectorization.\n\nOne common workaround is to employ n-grams instead of single words, but this can inflate the feature dimensionality, potentially increasing the risk of overfitting.\n\n## How it works\n\nThe value in each dimension is calculated as `1 / math.log(rank + 1)` (similar to the Discounted Cumulative Gain formula), where the rank denotes the position of the corresponding term, starting from 1.\n\nIf a term appears multiple times in the text, only its lowest rank is taken into account.\n\n## TODO\n\n* Test the common parameters of _VectorizerMixin to identify potential issues when upgrading scikit-learn. Currently, only the `ngrams_range` and `analyzer` parameters are automatically tested.\n* Implement the max_features parameter.\n\n",
    "bugtrack_url": null,
    "license": "new BSD",
    "summary": "Positional Vectorizer is a scikit-learn transformer that converts text to bag of words vector using a positional ranking algorithm as score",
    "version": "0.0.7",
    "project_urls": {
        "Homepage": "https://github.com/timotta/positional-vectorizer"
    },
    "split_keywords": [
        "machine learning",
        " embedding",
        " vectorizer",
        " scikit-learn",
        " text",
        " nlp"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "640fd1e144903208c8e2a9b70e2c51b3e1d3769fc3b640269c0b320255cd9adc",
                "md5": "05eb382848e63f36def69a07003e15e2",
                "sha256": "e07ca26479477649edd67632dc613cb0185b76c922f5b7099269cfd5bff634c8"
            },
            "downloads": -1,
            "filename": "positional_vectorizer-0.0.7.tar.gz",
            "has_sig": false,
            "md5_digest": "05eb382848e63f36def69a07003e15e2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.12,>=3.11",
            "size": 10194,
            "upload_time": "2024-05-01T23:27:41",
            "upload_time_iso_8601": "2024-05-01T23:27:41.001786Z",
            "url": "https://files.pythonhosted.org/packages/64/0f/d1e144903208c8e2a9b70e2c51b3e1d3769fc3b640269c0b320255cd9adc/positional_vectorizer-0.0.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-01 23:27:41",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "timotta",
    "github_project": "positional-vectorizer",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "positional-vectorizer"
}
        
Elapsed time: 0.26757s