Oracle e-Business: Building a sample payment prediction machine learning model using oracle ERP data

Oracle e-Business: Payment Prediction Machine Learning Model

  • AI Applications:
    • AI can analyze customer payment patterns to predict when invoices will be paid.
    • Natural Language Processing (NLP) can extract key payment terms from contracts or emails.
  • Benefits:
    • Enhanced cash flow forecasting.
    • Better planning for financial operations.

Below is an example Python code for a Payment Prediction model using machine learning. This code uses the Scikit-learn library to build a predictive model based on historical payment data.

Payment Prediction Code


import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error, r2_score

# Load the dataset (replace with your actual dataset)
# Example dataset should have columns: 'InvoiceAmount', 'PaymentTerm', 'CustomerCreditScore', 'DaysToPay'
data = pd.read_csv('payment_data.csv')

# Preprocessing
# Handle missing values
data.fillna(data.mean(), inplace=True)

# Features and target variable
X = data[['InvoiceAmount', 'PaymentTerm', 'CustomerCreditScore']]
y = data['DaysToPay']  # Target variable: Number of days to payment

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Build the model
model = RandomForestRegressor(n_estimators=100, random_state=42)

# Train the model
model.fit(X_train, y_train)

# Predict on the test set
y_pred = model.predict(X_test)

# Evaluate the model
mae = mean_absolute_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"Mean Absolute Error: {mae}")
print(f"R^2 Score: {r2}")

# Save the model for future use (optional)
import joblib
joblib.dump(model, 'payment_prediction_model.pkl')

# Predict payment days for new invoices
new_data = pd.DataFrame({
    'InvoiceAmount': [5000, 10000],
    'PaymentTerm': [30, 45],
    'CustomerCreditScore': [750, 680]
})
predicted_days = model.predict(new_data)
print("Predicted Days to Pay for New Invoices:", predicted_days)


Steps in the Code:

  1. Load and Prepare Data: The dataset includes historical payment data. Columns like InvoiceAmount, PaymentTerm, and CustomerCreditScore are features, and DaysToPay is the target variable.

  2. Handle Missing Data: Missing values are replaced with the mean of the respective column for simplicity.

  3. Split Data: Data is split into training and testing sets for model evaluation.

  4. Build the Model: A Random Forest Regressor is used for its robustness in handling non-linear data and feature interactions.

  5. Train and Evaluate: The model is trained on the training set and evaluated using metrics like Mean Absolute Error (MAE) and R² Score.

  6. Predict New Data: The trained model predicts payment days for new invoices.

Installation Steps

1. Set Up a Virtual Environment (Optional but Recommended)

To avoid conflicts between libraries, create a virtual environment:


python -m venv payment_prediction_env
source payment_prediction_env/bin/activate # On Windows: payment_prediction_env\Scripts\activate

2. Install Required Libraries

Run the following command to install the required Python libraries:

pip install pandas numpy scikit-learn joblib

3. Prepare the Dataset

  • Save your historical payment data in a file named payment_data.csv (or another name).
  • Ensure the file includes columns: InvoiceAmount, PaymentTerm, CustomerCreditScore, and DaysToPay.

Execution Instructions

1. Run the Python Script

Save the provided Python code in a file named payment_prediction.py. To execute it, run:

python payment_prediction.py

2. Interpret Output

The script will:

  • Train the machine learning model using historical data.
  • Output evaluation metrics like Mean Absolute Error (MAE) and R² score.
  • Predict the days to payment for new invoice data.

3. Add New Predictions

To predict payment days for new invoices, update the new_data DataFrame in the script with your data.

Optional Steps

1. Save and Reload the Model

If you want to reuse the trained model without retraining, the script already saves it as payment_prediction_model.pkl. To load the model for future predictions, use:


from joblib import load
model = load('payment_prediction_model.pkl') new_data = pd.DataFrame({ 'InvoiceAmount': [5000, 10000], 'PaymentTerm': [30, 45], 'CustomerCreditScore': [750, 680] }) predicted_days = model.predict(new_data) print(predicted_days)

2. Deploy the Model as a Web Service

You can use frameworks like Flask or FastAPI to deploy the model as a web service.

3. Automate Data Updates

Automate data ingestion and preprocessing by integrating with Oracle ERP systems using APIs or ETL tools.


Troubleshooting

  1. Library Installation Issues:

    • If libraries fail to install, ensure pip is updated:
      pip install --upgrade pip
  2. Dataset Errors:

    • Ensure your dataset file (payment_data.csv) is in the correct directory and properly formatted.
  3. Python Path Issues:

    • If Python commands don’t work, ensure Python is added to your system’s PATH.


Below is an example of how to deploy the Payment Prediction model as a web service using FastAPI, a modern web framework for building APIs with Python. This service will allow users to input invoice data and get predicted payment days through an HTTP request.


from fastapi import FastAPI from pydantic import BaseModel import pandas as pd from joblib import load # Load the trained model model = load('payment_prediction_model.pkl') # Initialize FastAPI app app = FastAPI() # Define request schema class InvoiceData(BaseModel): InvoiceAmount: float PaymentTerm: int CustomerCreditScore: int # Define prediction endpoint @app.post("/predict/") def predict_payment_days(data: InvoiceData): # Convert input data to DataFrame input_data = pd.DataFrame([{ 'InvoiceAmount': data.InvoiceAmount, 'PaymentTerm': data.PaymentTerm, 'CustomerCreditScore': data.CustomerCreditScore }]) # Make prediction predicted_days = model.predict(input_data) # Return result return {"PredictedDaysToPay": predicted_days[0]}

Installation and Execution Instructions

1. Set Up the Environment

Ensure you have the following libraries installed:

pip install fastapi uvicorn pandas joblib

2. Save the Trained Model

Make sure the trained model file (payment_prediction_model.pkl) is in the same directory as the script.

3. Run the Web Service

Start the FastAPI service using Uvicorn:

uvicorn payment_prediction_service:app --host 0.0.0.0 --port 8000

4. Access the API

Once the service is running, you can access it at:
http://localhost:8000/docs

API Testing

FastAPI automatically generates an interactive API documentation at /docs. Use the following steps to test the service:

  1. Open a browser and go to http://localhost:8000/docs.
  2. Use the POST /predict/ endpoint.
  3. Enter sample input data, such as:

{ "InvoiceAmount": 5000, "PaymentTerm": 30, "CustomerCreditScore": 750 }

Click Execute to get the predicted number of days to pay.

Example API Response

Input:

json

{
"InvoiceAmount": 5000, "PaymentTerm": 30, "CustomerCreditScore": 750 }

Response:

json

{
"PredictedDaysToPay": 28.5 }

Deploying the Service

To make the service accessible over the internet, you can:

  1. Use a Cloud Platform:

    • Deploy the service on AWS, Azure, or Google Cloud.
    • Use Docker to containerize the application for easy deployment.
  2. Expose Locally with Ngrok:

    • Install Ngrok and run:
      ngrok http 8000
    • Share the public URL provided by Ngrok for external access.


Here are step-by-step instructions to deploy your FastAPI Payment Prediction service on Microsoft Azure using Azure App Service.

Prerequisites

  1. Azure Account: Ensure you have an active Azure account. Sign up for a free account if needed.
  2. Azure CLI: Install the Azure CLI. Download here.
  3. Docker: Install Docker if you prefer containerized deployment (optional but recommended).
  4. Code and Model:
    • The payment_prediction_service.py FastAPI script.
    • The payment_prediction_model.pkl trained model file.

To implement a CI/CD deployment pipeline for deploying the Payment Prediction FastAPI service to Azure, we’ll use GitHub Actions. This approach automates the build, test, and deployment processes.


Prerequisites

  1. Azure Setup:

    • An active Azure account.
    • Azure CLI installed locally or Azure Service Principal created.
    • An App Service created (refer to earlier steps).
  2. GitHub Repository:

    • Your project code hosted in a GitHub repository.
  3. Dockerized Application:

    • A Dockerfile created in your project (optional for container-based deployment).
  4. Azure Container Registry (ACR):

    • If deploying via containers, ensure you have an ACR set up and credentials available.

Pipeline Overview

The pipeline will:

  1. Build the FastAPI application.
  2. Run tests to ensure the app works as expected.
  3. Deploy to Azure App Service or Azure Container Registry.

Step 1: Create Azure Credentials for GitHub

Generate Azure credentials for GitHub Actions:

  1. Login to Azure:

    az login
  2. Create a Service Principal:

    az ad sp create-for-rbac --name PaymentPredictionSP --role Contributor --scopes /subscriptions/<SUBSCRIPTION_ID>

    Replace <SUBSCRIPTION_ID> with your Azure subscription ID. This command outputs:

    json {"appId": "xxxxxx",
    "displayName": "PaymentPredictionSP", "password": "xxxxxx", "tenant": "xxxxxx" }
  3. Store Credentials in GitHub:

    • Go to your GitHub repository.
    • Navigate to Settings > Secrets and variables > Actions > New repository secret.
    • Add the following secrets:
      • AZURE_CLIENT_ID: appId
      • AZURE_CLIENT_SECRET: password
      • AZURE_TENANT_ID: tenant
      • AZURE_SUBSCRIPTION_ID: Your Azure subscription ID.

Step 2: Create GitHub Actions Workflow

Create a .github/workflows/deploy.yml file in your repository with the following content:

For Code-Based Deployment

name: CI/CD Pipeline for Azure
on: push: branches: - main jobs: build-and-deploy: runs-on: ubuntu-latest steps: # Checkout code - name: Checkout code uses: actions/checkout@v3 # Set up Python environment - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.9' # Install dependencies - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt # Run tests (add your test commands here) - name: Run tests run: pytest # Deploy to Azure App Service - name: 'Deploy to Azure Web App' uses: azure/webapps-deploy@v2 with: app-name: 'PaymentPredictionApp' # Replace with your Azure App Service name slot-name: 'production' publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}

For Docker-Based Deployment

name: CI/CD Pipeline for Azure
on: push: branches: - main jobs: build-and-deploy: runs-on: ubuntu-latest steps: # Checkout code - name: Checkout code uses: actions/checkout@v3 # Log in to Azure Container Registry (ACR) - name: Log in to Azure uses: azure/docker-login@v1 with: login-server: <ACR_NAME>.azurecr.io username: ${{ secrets.AZURE_CLIENT_ID }} password: ${{ secrets.AZURE_CLIENT_SECRET }} # Build and push Docker image to ACR - name: Build and Push Docker Image run: | docker build -t <ACR_NAME>.azurecr.io/payment-prediction:${{ github.sha }} . docker push <ACR_NAME>.azurecr.io/payment-prediction:${{ github.sha }} # Deploy Docker image to Azure App Service - name: Deploy to Azure App Service uses: azure/webapps-deploy@v2 with: app-name: 'PaymentPredictionApp' # Replace with your Azure App Service name images: '<ACR_NAME>.azurecr.io/payment-prediction:${{ github.sha }}'

Step 3: Test the Pipeline

  1. Push changes to the main branch of your GitHub repository.

  2. The pipeline will automatically trigger:

    • Build and Test the application.
    • Deploy it to Azure.
  3. Verify the deployment:

    • Navigate to your Azure App Service.
    • Access the API at http://<your-app-name>.azurewebsites.net/docs.

Step 4: Enhance Pipeline

  1. Add Test Cases: Ensure the app passes all tests before deployment.
  2. Add Notifications: Use tools like Slack or Teams to notify on pipeline success/failure.
  3. Use Staging Environments:
    • Deploy to a staging slot before pushing to production.
    • Modify the pipeline for slot deployment.