My Brain Cells

Easiest (and best) learning materials for anyone with a curiosity for machine learning and artificial intelligence, Deep learning, Programming, and other fun life hacks.

Personal Finance Analysis with Local LLMs

GitHub Link: https://github.com/anthonysandesh/Personal-Finance-Analysis-using-local-llm/tree/main

In today’s digitally driven world, managing personal finances efficiently is crucial for financial stability and growth. With the advancements in technology, individuals now have access to sophisticated tools and techniques to analyze their financial data effectively. One such innovative approach is utilizing Local Language Model (LLM) frameworks for expense classification and data analytics, as demonstrated in Merlin’s insightful project, “Analyzing My Finances Locally Using LLMs | Expense Classification and Data Analytics in Python.”

Prerequisites:

  • A laptop with a decent amount of RAM (at least 4GB)
  • An internet connection to download the necessary software
  • Python 3.6 or later installed on your laptop

Software:

Steps:

Install Lama and required libraries:

Open a terminal or command prompt.
Clone the Lama repository: git clone https://github.com/openai/lama.git
Follow the instructions in the README file to install Lama.
Make sure you have Pandas installed (pip install pandas).
Download your bank transaction data:

Export your bank transaction data from your bank’s website or app in a CSV format.
Make sure the CSV file includes columns for the transaction date, amount, and description.
Create a Jupyter Notebook:

Open Jupyter Notebook and create a new notebook.

#Import the necessary libraries:
import pandas as pd
from olama import Lama

#Load your bank transaction data:
data = pd.read_csv(“your_bank_transactions.csv”)


#Create a function to classify transactions:
def classify_transactions(transactions):
# Use the Lama API to classify the transactions
lama = Lama(api_key=”YOUR_OPENAI_API_KEY”)
responses = lama.predict(
inputs=transactions,
model=”llama2″,
temperature=0.8,
prompt=”Classify the following expenses into categories:”,
)
# Extract the categories from the responses
categories = [response[“choices”][0][“text”].split(“:”, 1)[1].strip() for response in responses]
return categories


#Use the function to classify your transactions:
data[“category”] = classify_transactions(data[“description”])


Preprocess the categories:

This step involves merging similar categories and ensuring consistency. For example, you might merge “Food” and “Dining” into “Food and Drinks”, or rename “Transportation” to “Travel”. You can use string manipulation techniques or regular expressions for this task.
Save the results:

#Save the categorized data to a new CSV file:
data.to_csv(“transactions_2022_2023_categorized.csv”, index=False)


Now after we get the categorized dataset saved, we have to visualize these results for that:

import pandas as pd
import numpy as np
import plotly.express as px
import panel as pl

This code block imports the necessary libraries for the script to function. Here’s a breakdown of each library:

pandas: Used for data manipulation and analysis, like reading and processing CSV files.
numpy: Used for numerical computations and array operations.
plotly.express: Used for creating interactive charts and visualizations.
panel: Used for building web applications with interactive dashboards.
Reading transaction data:

Read transactions_2022_2023_categorized.csv

df = pd.read_csv(‘transactions_2022_2023_categorized.csv’)

Add year and month columns

df[‘Year’] = pd.to_datetime(df[‘Date’]).dt.year
df[‘Month’] = pd.to_datetime(df[‘Date’]).dt.month
df[‘Month Name’] = pd.to_datetime(df[‘Date’]).dt.strftime(“%b”)

Remove unnecessary columns

df = df.drop(columns=[‘Transaction’, ‘Transaction vs category’])

This code reads the CSV file containing your categorized transactions, adds new columns for year and month, and removes unnecessary columns.

Preparing data for charts:

For Income rows, assign Name / Description to Category

df[‘Category’] = np.where(df[‘Expense/Income’] == ‘Income’, df[‘Name / Description’], df[‘Category’])

Creating pie charts:


def make_pie_chart(df, year, label):
# Filter data for expense/income and year
sub_df = df[(df[‘Expense/Income’] == label) & (df[‘Year’] == year)]

# Configure chart layout and colors
pie_fig = px.pie(sub_df, values=’Amount (EUR)’, names=’Category’, color_discrete_sequence = px.colors.qualitative.Set2)
pie_fig.update_traces(textposition=’inside’, direction =’clockwise’, hole=0.3, textinfo=”label+percent”)

# Calculate total expense/income and saving rate
total_expense = df[(df[‘Expense/Income’] == ‘Expense’) & (df[‘Year’] == year)][‘Amount (EUR)’].sum()
total_income = df[(df[‘Expense/Income’] == ‘Income’) & (df[‘Year’] == year)][‘Amount (EUR)’].sum()
saving_rate = round((total_income – total_expense)/total_income*100)
saving_rate_text = “: Saving rate ” + str(saving_rate) + “%”

# Add title, annotations, and format the chart
pie_fig.update_layout(uniformtext_minsize=10,
uniformtext_mode=’hide’,
title=dict(text=label+” Breakdown ” + str(year) + saving_rate_text),
annotations=[
dict(
text=”€ ” + str(round(total_expense)),
x=0.5, y=0.5, font_size=12,
showarrow=False
)
]
)
return pie_fig

This code defines a function make_pie_chart that creates pie charts for income or expense breakdown by year. It filters the data, configures the chart layout, calculates totals and saving rate, and adds annotations and formatting.

Creating bar charts:


def make_monthly_bar_chart(df, year, label):
# Filter data for expense/income and year
df = df[(df[‘Expense/Income’] == label) & (df[‘Year’] == year)]

# Calculate monthly sum and sort by month
total_by_month = (df.groupby([‘Month’, ‘Month Name’])[‘Amount (EUR)’].sum()
.to_frame()
.reset_index()
.sort_values(by=’Month’)
.reset_index(drop=True))

# Configure chart layout and color
bar_fig = px.bar(total_by_month, x=’Month Name’, y=’Amount (EUR)’, text_auto=’.2s’, title=label+” per month”, color=’Amount (EUR)’, color_continuous

Anthony

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top