|
import streamlit as st
|
|
import pandas as pd
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.preprocessing import LabelEncoder, StandardScaler
|
|
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
|
|
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
from sklearn.metrics import confusion_matrix, classification_report
|
|
import matplotlib.pyplot as plt
|
|
import seaborn as sns
|
|
import re
|
|
|
|
st.title("Expense Category Prediction")
|
|
|
|
|
|
df = pd.read_csv("financial_data.csv", sep='\s\s+', engine='python')
|
|
|
|
|
|
def preprocess_data(df):
|
|
|
|
|
|
df['Date'] = df['Date'].str.extract(r'(\d{4}-\d{2}-\d{2})')
|
|
|
|
|
|
df['Date'] = df['Date'].ffill()
|
|
|
|
|
|
df.dropna(subset=['Date'], inplace=True)
|
|
|
|
|
|
df['Date'] = pd.to_datetime(df['Date'])
|
|
|
|
|
|
df['Expense_Category'] = df['Expense_Category'].fillna('Unknown')
|
|
df['Description'] = df['Description'].fillna('Unknown')
|
|
|
|
|
|
df['Amount'] = pd.to_numeric(df['Amount'], errors='coerce').fillna(0)
|
|
|
|
|
|
df['Month'] = df['Date'].dt.month
|
|
df['DayOfWeek'] = df['Date'].dt.dayofweek
|
|
|
|
|
|
def clean_text(text):
|
|
text = text.lower()
|
|
text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
|
|
return text
|
|
|
|
df['Description_Cleaned'] = df['Description'].apply(clean_text)
|
|
|
|
|
|
tfidf_vectorizer = TfidfVectorizer(max_features=100)
|
|
tfidf_features = tfidf_vectorizer.fit_transform(df['Description_Cleaned']).toarray()
|
|
tfidf_df = pd.DataFrame(tfidf_features, index=df.index)
|
|
|
|
|
|
features_df = pd.concat([df[['Amount', 'Month', 'DayOfWeek']], tfidf_df], axis=1)
|
|
|
|
|
|
label_encoder = LabelEncoder()
|
|
df['Expense_Category_Encoded'] = label_encoder.fit_transform(df['Expense_Category'])
|
|
|
|
|
|
X = features_df
|
|
y = df['Expense_Category_Encoded']
|
|
|
|
|
|
scaler = StandardScaler()
|
|
X = scaler.fit_transform(X)
|
|
|
|
return X, y, label_encoder, df
|
|
|
|
X, y, label_encoder, df = preprocess_data(df.copy())
|
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(
|
|
X, y, test_size=0.2, random_state=42)
|
|
|
|
|
|
models = {
|
|
"Random Forest": RandomForestClassifier(random_state=42),
|
|
"Gradient Boosting": GradientBoostingClassifier(random_state=42)
|
|
}
|
|
|
|
|
|
tabs = st.tabs(list(models.keys()))
|
|
|
|
for tab, model_name in zip(tabs, models.keys()):
|
|
with tab:
|
|
st.header(model_name)
|
|
model = models[model_name]
|
|
model.fit(X_train, y_train)
|
|
y_pred = model.predict(X_test)
|
|
|
|
|
|
st.subheader("Confusion Matrix")
|
|
cm = confusion_matrix(y_test, y_pred)
|
|
plt.figure(figsize=(8, 6))
|
|
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
|
|
plt.xlabel("Predicted")
|
|
plt.ylabel("Actual")
|
|
st.pyplot(plt.gcf())
|
|
|
|
|
|
st.subheader("Classification Report")
|
|
cr = classification_report(y_test, y_pred,
|
|
target_names=label_encoder.inverse_transform(
|
|
df['Expense_Category_Encoded'].unique()),
|
|
zero_division=0)
|
|
st.text(cr)
|
|
|
|
|
|
st.subheader("Remarks")
|
|
st.write("Model Performance Analysis:")
|
|
st.write(
|
|
f"The {model_name} model's performance in predicting Expense Categories is shown above.")
|
|
st.write("Key Metrics:")
|
|
st.write(
|
|
"- The model uses a combination of expense amount, time-based features, and text descriptions to predict the expense category."
|
|
)
|
|
st.write(
|
|
"- The classification report provides insights into the model's precision, recall, and F1-score for each expense category."
|
|
) |