Building a personal finance tracker with automated expense categorization requires knowledge of Python, command of necessary libraries like Pandas, SQLite, and Machine Learning along with basics of web development for visualization (HTML, CSS, JavaScript).
In this guide, we'll use a simple Flask application and SQLite as the database. We'll use a CSV file to simulate bank transactions for manual input.
Here is a step by step guide to achieve that.
Ask your specific question in Mate AI
In Mate you can connect your project, ask questions about your repository, and use AI Agent to solve programming tasks
Step 1: Importing Required Libraries
We will be using Flask for our Web App, SQLite for database management, Pandas for data manipulation, and sklearn for clustering our transactions.
from flask import Flask, render_template, request, make_response
import sqlite3
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.cluster import KMeans
Step 2: Database Creation
Creating a SQLite database to store transactions.
# Create a connection to the database
conn = sqlite3.connect('transactions.db')
# Create a cursor
cursor = conn.cursor()
# Create the transactions table
cursor.execute('''CREATE TABLE transactions
(Date TEXT,
Description TEXT,
Amount REAL,
Category TEXT);''')
conn.commit()
conn.close()
Step 3: Importing Data From CSV
For demo purposes, use a CSV file to simulate bank transactions. Import this file into a Pandas DataFrame, then convert it into a SQLite database.
# Connect to SQLite database
conn = sqlite3.connect('transactions.db')
df = pd.read_csv('Transactions.csv')
df.to_sql('transactions', conn, if_exists='replace', index = False)
# Close the database connection
conn.close()
Step 4: Clustering Transactions
Here we're using a simple K Means clustering algorithm from sklearn to cluster similar transactions.
# load transactions into csv
df = pd.read_csv('transactions.csv')
# Initialize the Count Vectorizer
cv = CountVectorizer()
# Fit and transform the processed titles
vector = cv.fit_transform(df['Description'].values.astype('U'))
kmeans = KMeans(n_clusters = 6, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0)
y_kmeans = kmeans.fit_predict(vector)
df['Category'] = y_kmeans
Step 5: Building the Flask App
In the Flask app, we'll have two routes - one for viewing transactions and one for adding transactions.
app = Flask(__name__)
@app.route('/')
def view_transactions():
conn = sqlite3.connect('transactions.db')
df = pd.read_sql_query("SELECT * FROM transactions", conn)
conn.close()
return render_template("index.html", tables=[df.to_html(classes='data')], titles=df.columns.values)
@app.route('/add', methods = ['POST'])
def add_transaction():
conn = sqlite3.connect('transactions.db')
cursor = conn.cursor()
cursor.execute("INSERT INTO transactions (Date, Description, Amount, Category) VALUES (?,?,?,?)", (request.form['Date'], request.form['Description'], request.form['Amount'], request.form['Category']))
conn.commit()
conn.close()
return make_response('Success', 200)
if __name__ == '__main__':
app.run()
Note: In the template files, you'll use HTML to create forms for input and table to display the data.
That's the basic structure for building a personal finance tracker using Python and SQLite with automated categorization of transactions. You can extend this setup to accommodate more complex scenarios and more detailed categorization. You can also work on the visualization aspect of this application by using platforms like ChartJS to create graphs.
Please keep in mind that this is an example only and shouldn't be used as-is for financial tracking, it's merely a proof of concept. There are many other factors to consider such as security, data validation, error handling, etc. You should consult a professional before developing and deploying such applications.
AI agent for developers
Boost your productivity with Mate:
easily connect your project, generate code, and debug smarter - all powered by AI.
Do you want to solve problems like this faster? Download now for free.