Have you ever found yourself in a pinch, needing a Spacy TextCategorizer but don’t have the luxury of time to train one from scratch? Worry not, for we’re diving into Classy Classification – an easy and efficient way to handle text classification tasks!
What is Classy Classification?
Classy Classification allows you to perform few-shot classification using sentence-transformers or spaCy models. You can provide a dictionary with labels and examples, or simply lists of labels for zero-shot classification using Huggingface zero-shot classifiers. Let’s explore how to get started!
Installation
To use Classy Classification in your projects, simply install it via pip:
pip install classy-classification
Getting Started
SpaCy Embeddings
The following example shows how to classify text based on the furniture and kitchen categories using SpaCy embeddings:
import spacy
data = {
'furniture': [
'This text is about chairs.',
'Couches, benches and televisions.',
'I really need to get a new sofa.'
],
'kitchen': [
'There also exist things like fridges.',
'I hope to be getting a new stove today.',
'Do you also have some ovens.'
]
}
nlp = spacy.load("en_core_web_trf")
nlp.add_pipe("classy_classification", config={
'data': data,
'model': 'spacy'
})
print(nlp("I am looking for kitchen appliances.")._.cats)
# Output: [furniture: 0.21, kitchen: 0.79]
Think of this code as a chef assembling ingredients to make a delicious dish. Just as a chef chooses the right ingredients to create a tasty meal, you provide the right text snippets (ingredients) under their respective categories (dishes), and the model combines these “ingredients” to classify new queries based on what it’s learned.
Sentence Level Classification
You can also perform sentence-level classification as follows:
nlp.add_pipe("classy_classification", config={
'data': data,
'model': 'spacy',
'include_sent': True
})
print(nlp("I am looking for kitchen appliances. And I love doing so.").sents[0]._.cats)
# Output: [furniture: 0.21, kitchen: 0.79]
This approach allows you to classify sentences individually, enhancing the granularity of your analysis.
Multi-label Classification
Occasionally, you may need to assign multiple labels to a single text snippet. For multi-label classification:
nlp.add_pipe("classy_classification", config={
'data': data,
'model': 'spacy',
'multi_label': True,
})
print(nlp("I am looking for furniture and kitchen equipment.")._.cats)
# Output: [furniture: 0.92, kitchen: 0.91]
This is akin to selecting multiple toppings on your pizza; each topping (label) adds to the overall flavor (meaning) of the final product.
Troubleshooting Ideas
If you encounter issues while implementing Classy Classification, here are some troubleshooting tips:
- Ensure that you have installed all necessary packages.
- Check that your data is formatted correctly — labels should match the keys in your dictionary and examples should be assembled properly.
- If you are facing issues with model configuration, experiment with different models or configurations to see what works best for your dataset.
- In case of low classification scores, consider refining your training data with more diverse examples.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations. Now go ahead and enjoy the seamless text classification experience with Classy Classification!