library_name: scikit-learn tags:

  • text-classification
  • sentiment-analysis
  • tfidf
  • logistic-regression
  • machine-learning
  • nlp

Basic Sentiment Analysis Model

A simple Sentiment Analysis machine learning model built using TF-IDF (Term Frequency-Inverse Document Frequency) and Logistic Regression.

The model classifies text into two sentiment categories:

  • Positive
  • Negative

This project is designed as a basic example for understanding the complete machine learning model lifecycle:

Dataset
   ↓
Text Preprocessing
   ↓
TF-IDF Vectorization
   ↓
Logistic Regression
   ↓
Model Evaluation
   ↓
Model Serialization
   ↓
Hugging Face Hub
   ↓
Inference

Model Details

Property Description
Model Type Text Classification
Task Sentiment Analysis
Algorithm Logistic Regression
Feature Extraction TF-IDF
Library Scikit-learn
Language Python
Classes Positive / Negative
Model Format Pickle (.pkl)

Model Architecture

The model consists of two primary components:

1. TF-IDF Vectorizer

The input text is converted into numerical features using TF-IDF.

Input Text
    ↓
TF-IDF Vectorizer
    ↓
Numerical Feature Vector

2. Logistic Regression

The numerical feature vector is passed to the Logistic Regression classifier.

TF-IDF Features
      ↓
Logistic Regression
      ↓
Sentiment Prediction

Complete pipeline:

Text
 ↓
Lowercase Conversion
 ↓
Tokenization
 ↓
Stop Word Removal
 ↓
TF-IDF
 ↓
Logistic Regression
 ↓
Prediction

Sentiment Labels

The model uses the following labels:

Label Sentiment
0 Negative
1 Positive

Example:

Input:
"I love this product"

Output:
1 β†’ Positive

Another example:

Input:
"This product is terrible"

Output:
0 β†’ Negative

Installation

Clone or download the project.

Install the required Python libraries:

pip install -r requirements.txt

Required packages:

scikit-learn
pandas
joblib
huggingface_hub

Training the Model

Run the training script:

python train.py

The training process performs the following steps:

1. Load training data
2. Split data into training and testing sets
3. Create TF-IDF vectorizer
4. Transform text into numerical features
5. Train Logistic Regression
6. Evaluate model
7. Save trained model

The trained model will be saved as:

model/sentiment_model.pkl

Local Prediction

After training the model, run:

python predict.py

Example input:

I really love this product

Example output:

Prediction: positive
Confidence: 0.72

Another example:

This product is terrible

Output:

Prediction: negative
Confidence: 0.69

Using the Model

The saved model can be loaded using joblib.

import joblib

model = joblib.load(
    "model/sentiment_model.pkl"
)

text = [
    "I love this product"
]

prediction = model.predict(text)

if prediction[0] == 1:
    print("Positive")
else:
    print("Negative")

Prediction With Confidence

The model can also return prediction probabilities.

import joblib

model = joblib.load(
    "model/sentiment_model.pkl"
)

text = [
    "This is an amazing product"
]

prediction = model.predict(text)
probability = model.predict_proba(text)

label = "Positive" if prediction[0] == 1 else "Negative"
confidence = max(probability[0])

print("Sentiment:", label)
print("Confidence:", round(confidence, 4))

Loading the Model From Hugging Face

Install the Hugging Face Hub library:

pip install huggingface_hub

Download the model:

from huggingface_hub import hf_hub_download
import joblib

model_path = hf_hub_download(
    repo_id="YOUR_USERNAME/basic-sentiment-model",
    filename="sentiment_model.pkl"
)

model = joblib.load(model_path)

text = [
    "This product is fantastic"
]

prediction = model.predict(text)

print(prediction)

Replace:

YOUR_USERNAME

with your Hugging Face username.


Example Predictions

Input Prediction
I love this product Positive
This product is excellent Positive
Amazing experience Positive
Very good service Positive
I am very happy Positive
I hate this product Negative
This product is terrible Negative
Very bad experience Negative
Worst service Negative
I am very disappointed Negative

Project Structure

basic-sentiment-model/
β”‚
β”œβ”€β”€ model/
β”‚   └── sentiment_model.pkl
β”‚
β”œβ”€β”€ train.py
β”œβ”€β”€ predict.py
β”œβ”€β”€ upload.py
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ README.md
└── .gitignore

Training Dataset

The current model uses a small demonstration dataset containing positive and negative sentences.

Example positive samples:

I love this product
This product is excellent
Amazing experience
Very good service
I am very happy

Example negative samples:

I hate this product
This product is terrible
Very bad experience
Worst service
I am very disappointed

The dataset is intentionally small because this project is designed as an educational demonstration of model training and Hugging Face deployment.


Evaluation

The model is evaluated using classification accuracy.

from sklearn.metrics import accuracy_score

accuracy = accuracy_score(
    y_test,
    predictions
)

print("Accuracy:", accuracy)

Because the demonstration dataset is very small, the reported accuracy should not be interpreted as a measure of real-world performance.


Limitations

This model has several limitations:

  • It is trained on a very small dataset.
  • It only supports positive and negative sentiment.
  • It may not understand sarcasm.
  • It may not understand context-dependent sentiment.
  • It may perform poorly on unseen vocabulary.
  • It is not suitable for production use without additional training and validation.
  • The confidence score should not be interpreted as a calibrated probability of correctness.

For production use, a significantly larger and more representative dataset should be used.


Future Improvements

The model can be improved by:

Dataset

Use a larger sentiment dataset containing thousands or millions of examples.

Feature Engineering

Experiment with:

TF-IDF
N-grams
Word Embeddings
Sentence Embeddings

Machine Learning Algorithms

Compare:

Logistic Regression
Naive Bayes
SVM
Random Forest
Gradient Boosting

Deep Learning

The project can later be upgraded to:

Neural Network
      ↓
LSTM
      ↓
GRU
      ↓
Transformer
      ↓
BERT

Hugging Face Transformers

A future version can use a pretrained Transformer model for improved NLP performance.


Hugging Face Deployment

The trained model can be uploaded to the Hugging Face Hub.

Example:

hf auth login

Then:

hf upload YOUR_USERNAME/basic-sentiment-model ./model .

The model will be available at:

YOUR_USERNAME/basic-sentiment-model

Intended Use

This model is intended for:

  • Learning Machine Learning
  • Learning NLP
  • Understanding text classification
  • Understanding TF-IDF
  • Understanding Logistic Regression
  • Learning model serialization
  • Learning Hugging Face Hub deployment
  • Educational demonstrations
  • Experimentation

License

This project is provided for educational and experimental purposes.

If you reuse or modify this project, add an appropriate license according to your intended use.


Author

Basic Sentiment Analysis Model

Built with:

Python
Scikit-learn
TF-IDF
Logistic Regression
Hugging Face Hub

Acknowledgements

This project uses the open-source Python machine learning ecosystem, particularly:

  • Scikit-learn
  • Pandas
  • Joblib
  • Hugging Face Hub

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support