Spaces:
Sleeping
Sleeping
File size: 680 Bytes
2bd21ab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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()
} |