Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save renet123/765904ed40e641c0fed4fcdf06ec6304 to your computer and use it in GitHub Desktop.
Save renet123/765904ed40e641c0fed4fcdf06ec6304 to your computer and use it in GitHub Desktop.
Python script for automated reporting using AI with financial data on different reporting periods
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import seaborn as sns
# Example Monthly Balance Sheet and Income Statement Data
# Replace with your actual data
balance_sheet_data = {
'Month': ['2020-01', '2020-02', '2020-03', '2020-04', '2020-05', '2020-06'],
'Assets': [100000, 105000, 110000, 115000, 120000, 125000],
'Liabilities': [50000, 52000, 54000, 56000, 58000, 60000],
'Equity': [50000, 53000, 56000, 59000, 62000, 65000]
}
income_statement_data = {
'Month': ['2020-01', '2020-02', '2020-03', '2020-04', '2020-05', '2020-06'],
'Revenue': [20000, 21000, 22000, 23000, 24000, 25000],
'Expenses': [15000, 15500, 16000, 16500, 17000, 17500],
'Net Income': [5000, 5500, 6000, 6500, 7000, 7500]
}
# Convert to DataFrame
balance_sheet = pd.DataFrame(balance_sheet_data)
income_statement = pd.DataFrame(income_statement_data)
# Merge DataFrames on Month
financial_data = pd.merge(balance_sheet, income_statement, on='Month')
# Scale data
scaler = StandardScaler()
financial_data_scaled = scaler.fit_transform(financial_data.iloc[:, 1:])
# Train model to predict next month's Net Income based on previous financial data
model = LinearRegression()
model.fit(financial_data_scaled[:, :-1], financial_data_scaled[:, -1])
# Make predictions
predictions = model.predict(financial_data_scaled[:, :-1])
# Plot results
plt.figure(figsize=(10, 6))
plt.plot(financial_data['Month'], financial_data_scaled[:, -1], label='Actual')
plt.plot(financial_data['Month'], predictions, label='Predicted')
plt.title('Actual vs Predicted Net Income')
plt.legend()
plt.show()
# Save results to CSV
results = pd.DataFrame({'Month': financial_data['Month'], 'Actual': financial_data_scaled[:, -1], 'Predicted': predictions})
results.to_csv('results.csv', index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment