Expense Category Predictor Using Machine Learning

1. Introduction

An Expense Category Predictor is a Machine Learning application that automatically classifies expenses into different categories based on the description provided by the user. Managing personal or business expenses manually can be time-consuming, especially when there are a large number of transactions. An automated expense classification system can help users organize their financial records quickly and efficiently.

For example, when a user enters an expense description such as "Bought groceries from supermarket", the system can automatically identify it as a Food expense. Similarly, "Uber ride to office" can be classified as Transport, while "Monthly electricity bill" can be classified as Utilities.

Some examples are:

Expense DescriptionCategoryBought groceries from supermarketFoodUber ride to officeTransportMonthly electricity billUtilitiesBought a new mobile phoneShoppingMovie ticket purchaseEntertainment

This is a Supervised Machine Learning problem because the model is trained using historical expense data where each expense already has a category label. The model learns patterns and relationships between expense descriptions and their corresponding categories.

Since the output is a category such as Food, Transport, Shopping, Utilities, Entertainment, or Health, this problem is considered a Classification problem.

In this project, two important Machine Learning techniques are used:

  • TF-IDF Vectorization → Converts text descriptions into numerical features.

  • Naive Bayes Classifier → Uses the numerical features to classify expenses into categories.

2. Objective

The main objective of this project is to build an AI-based system that can automatically predict the category of a new expense based on its description.

The system learns from previously categorized expenses and uses the learned patterns to classify new expenses. This can reduce manual work and make expense management more efficient.

The project also demonstrates important Machine Learning concepts such as text preprocessing, feature extraction, supervised learning, classification, model training, and prediction.

3. Technologies Used

The following technologies and libraries are used:

  • Python – Main programming language.

  • Pandas – Used to create and manage the expense dataset.

  • Scikit-learn – Used to implement the Machine Learning algorithms.

  • TF-IDF Vectorizer – Converts text descriptions into numerical values.

  • Multinomial Naive Bayes – Classifies expenses based on their text features.

4. Dataset

The dataset contains sample expense descriptions and their corresponding categories. Each record consists of two main columns:

  • Description – A text description of the expense.

  • Category – The category assigned to that expense.

The categories used in this example are:

  • Food

  • Utilities

  • Transport

  • Shopping

  • Entertainment

  • Health

The dataset contains ten sample expenses that are used to train the Machine Learning model.

Dataset

                Description        Category
0   Bought groceries from supermarket       Food
1       Restaurant dinner with friends       Food
2              Paid electricity bill  Utilities
3          Monthly internet payment  Utilities
4              Uber ride to office   Transport
5                 Fuel for my car   Transport
6          Bought a new mobile phone Shopping
7      Purchased laptop accessories Shopping
8             Movie ticket purchase Entertainment
9          Gym membership payment      Health

5. Working Principle

The system works in several stages.

First, the expense dataset is created using Pandas. The descriptions and their categories are stored in a DataFrame.

Next, the expense descriptions are separated from the category labels. The descriptions are used as the input variable, while the categories are used as the target variable.

Since Machine Learning algorithms cannot directly understand text, the descriptions must first be converted into numerical values. TF-IDF Vectorization is used for this purpose. TF-IDF analyzes the words in the expense descriptions and assigns numerical values based on their importance.

After converting the descriptions into numerical features, a Multinomial Naive Bayes classifier is created. The classifier is trained using the vectorized expense descriptions and their known categories.

When a new expense is entered, it is converted into the same TF-IDF numerical representation. The trained Naive Bayes model then analyzes the features and predicts the most appropriate category.

6. Python Implementation

The following Python program implements the Expense Category Predictor.

# Import required libraries

import pandas as pd

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB

# --------------------------------------------
# Step 1: Create expense dataset
# --------------------------------------------

data = {
    "Description": [
        "Bought groceries from supermarket",
        "Restaurant dinner with friends",
        "Paid electricity bill",
        "Monthly internet payment",
        "Uber ride to office",
        "Fuel for my car",
        "Bought a new mobile phone",
        "Purchased laptop accessories",
        "Movie ticket purchase",
        "Gym membership payment"
    ],

    "Category": [
        "Food",
        "Food",
        "Utilities",
        "Utilities",
        "Transport",
        "Transport",
        "Shopping",
        "Shopping",
        "Entertainment",
        "Health"
    ]
}

# Convert dictionary into DataFrame

df = pd.DataFrame(data)

print("Dataset:")
print(df)

# --------------------------------------------
# Step 2: Select input and target
# --------------------------------------------

X = df["Description"]

y = df["Category"]

# --------------------------------------------
# Step 3: Convert text into numbers
# --------------------------------------------

vectorizer = TfidfVectorizer()

X_vectorized = vectorizer.fit_transform(X)

# --------------------------------------------
# Step 4: Create Machine Learning model
# --------------------------------------------

model = MultinomialNB()

# --------------------------------------------
# Step 5: Train the model
# --------------------------------------------

model.fit(
    X_vectorized,
    y
)

# --------------------------------------------
# Step 6: Predict new expense category
# --------------------------------------------

new_expense = [
    "Paid for monthly Netflix subscription"
]

# Convert new expense into numerical format

new_expense_vector = vectorizer.transform(
    new_expense
)

prediction = model.predict(
    new_expense_vector
)

# --------------------------------------------
# Step 7: Display prediction
# --------------------------------------------

print("\nExpense Category:")
print(prediction[0])

7. Explanation of the Python Program

The program begins by importing Pandas, TfidfVectorizer, and MultinomialNB. Pandas is used to create the dataset, while Scikit-learn provides the text vectorization and classification algorithms.

In Step 1, a dictionary containing expense descriptions and categories is created. This dictionary is converted into a Pandas DataFrame.

In Step 2, the Description column is selected as the input variable X, while the Category column is selected as the target variable y.

In Step 3, the TfidfVectorizer converts the text descriptions into numerical feature vectors. This is necessary because Machine Learning algorithms work with numerical data rather than raw text.

In Step 4, a Multinomial Naive Bayes model is created. Naive Bayes is a popular classification algorithm for text-based problems because it works well with word-based features.

In Step 5, the model is trained using the vectorized expense descriptions and their corresponding categories. During training, the model learns the relationship between words and expense categories.

In Step 6, a new expense is provided:

Paid for monthly Netflix subscription

The new description is converted into a TF-IDF vector using the same vectorizer that was used for the training data. The trained model then predicts its category.

8. Prediction

Input

Paid for monthly Netflix subscription

Output

Expense Category:
Entertainment

The model predicts Entertainment because the training data contains an entertainment-related example, such as "Movie ticket purchase". The word patterns in the new description are compared with the learned patterns, and the classifier selects the most likely category.

9. Advantages

The Expense Category Predictor provides several advantages:

  • Automatically categorizes expenses.

  • Reduces manual expense management.

  • Can process text-based expense descriptions.

  • Uses a simple and efficient Machine Learning algorithm.

  • Can be integrated into personal finance applications.

  • Can be extended by adding more training examples.

  • Can support multiple expense categories.

10. Limitations

The main limitation of this beginner-level system is the small size of the dataset. Only ten sample expenses are used for training, so the model may not perform accurately for every possible expense description.

For example, different users may describe the same expense in different ways. A larger and more diverse dataset would help the model learn more variations of expense descriptions.

The system also relies heavily on the words present in the description. It does not deeply understand the meaning or context of the sentence.

The accuracy can be improved by collecting more historical expense data, adding more examples for each category, and experimenting with more advanced Natural Language Processing and Machine Learning techniques.

11. Future Improvements

This project can be improved in several ways. A larger dataset containing thousands of real-world expense descriptions could be used to train the model. Additional categories such as Rent, Education, Travel, Insurance, and Subscriptions could also be added.

More advanced NLP models such as BERT or other Transformer-based models could be explored for better text understanding. The system could also be integrated into a mobile or web-based expense management application where users enter expenses and receive automatic categories.

Another useful improvement would be to allow users to correct incorrect predictions. These corrections could be stored and used as additional training data to improve the model over time.

12. Conclusion

The Expense Category Predictor Using Machine Learning demonstrates how Machine Learning and Natural Language Processing can be used to automatically classify financial expenses. The system uses TF-IDF Vectorization to convert expense descriptions into numerical features and Multinomial Naive Bayes to predict the appropriate category.

For the example input, "Paid for monthly Netflix subscription", the model predicts the category as Entertainment.

Although the project uses a small dataset and a simple classification algorithm, it provides a practical introduction to text classification and supervised Machine Learning. With a larger dataset and more advanced NLP techniques, this concept can be developed into a powerful automatic expense management system.