48 – Real-World Python Projects – Smart Expense Predictor

This project uses Machine Learning Regression to predict your future monthly expenses based on your past spending patterns.

It’s similar to what budgeting apps (like MoneyView, Walnut, YNAB) use.


🎯 What This Project Can Do

✔ Learn from your last 6–12 months of expenses
✔ Predict next month’s spending
✔ Show category-wise predictions (food, rent, travel)
✔ Visualize data
✔ Support CSV or SQL database
✔ Optionally build a mobile-friendly Streamlit dashboard


📁 Folder Structure

expense_predictor/
│── expense_data.csv
│── train_model.py
│── predict.py
│── model.pkl
│── vectorizer.pkl (if using categories)

📊 Example CSV (expense_data.csv)

month,expense
1,12000
2,13000
3,15000
4,14500
5,16000
6,17000

🧠 1. ML Model Training (train_model.py)

Using Linear Regression (best for simple numeric trends):

import pandas as pd
from sklearn.linear_model import LinearRegression
import joblib

df = pd.read_csv("expense_data.csv")

X = df[["month"]]
y = df["expense"]

model = LinearRegression()
model.fit(X, y)

joblib.dump(model, "model.pkl")
print("Model trained and saved as model.pkl")

🧠 2. Predict Next Month (predict.py)

import joblib
import pandas as pd

df = pd.read_csv("expense_data.csv")
next_month = df["month"].max() + 1

model = joblib.load("model.pkl")
prediction = model.predict([[next_month]])

print(f"Predicted Expense for Month {next_month}: ₹{prediction[0]:.2f}")

📈 Optional Enhanced Dataset (category-based)

date,food,travel,rent,shopping,others
2025-01-01,2200,300,8500,0,200
2025-02-01,2500,450,8500,500,300

🧠 Advanced ML Version (Multiple Features)

Using RandomForestRegressor for higher accuracy:

from sklearn.ensemble import RandomForestRegressor
import pandas as pd
import joblib

df = pd.read_csv("expense_data.csv")

X = df.drop(columns=["total"])
y = df["total"]

model = RandomForestRegressor()
model.fit(X, y)

joblib.dump(model, "model.pkl")
print("Advanced model trained!")

Comments

Leave a Reply

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