Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| from sklearn.linear_model import LinearRegression | |
| import numpy as np | |
| def predict_expense(df): | |
| if len(df) < 5: | |
| return {"error": "Not enough data"} | |
| df['date'] = pd.to_datetime(df['date']) | |
| df['day'] = df['date'].dt.day | |
| df['month'] = df['date'].dt.month | |
| X = df[['day', 'month']] | |
| y = df['amount'] | |
| model = LinearRegression() | |
| model.fit(X, y) | |
| future = pd.DataFrame({ | |
| "day": [df['day'].max() + i for i in range(1, 8)], | |
| "month": [df['month'].iloc[-1]] * 7 | |
| }) | |
| preds = model.predict(future) | |
| return { | |
| "next_7_days": float(np.sum(preds)), | |
| "daily_prediction": preds.tolist() | |
| } |