fasttext-numpy2

0.10.4 · active · verified Wed Apr 15

fasttext-numpy2 is a Python library that provides bindings for Facebook AI Research's fastText, focusing on compatibility with NumPy 2.x. The original fastText library is designed for efficient learning of word representations and sentence classification. This `fasttext-numpy2` fork specifically addresses a critical breaking change introduced by NumPy 2.0, allowing users to continue using fastText with newer NumPy versions. The current version is 0.10.4, and its release cadence is primarily driven by maintaining compatibility with its dependencies, especially NumPy.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to train a simple text classification model using `fasttext-numpy2` and then make a prediction. It first creates a dummy dataset in a file named `data.txt` in the format expected by fastText for supervised learning. It then trains a model using `train_supervised` and finally predicts the label for a new piece of text.

import fasttext
import os

# Create a dummy training data file
with open('data.txt', 'w') as f:
    f.write('__label__sports This is a great game.\n')
    f.write('__label__politics The election results are in.\n')
    f.write('__label__sports I love playing basketball.\n')
    f.write('__label__politics Debates are important for democracy.\n')

# Train a supervised model
model = fasttext.train_supervised('data.txt', epoch=5, lr=0.1, dim=100)

# Predict a label for a new text
text_to_predict = 'I watched a thrilling football match.'
labels, probabilities = model.predict(text_to_predict)

print(f"Text: '{text_to_predict}'")
print(f"Predicted label: {labels[0][0]}")
print(f"Probability: {probabilities[0]:.4f}")

# Clean up the dummy file
os.remove('data.txt')

view raw JSON →