Skip to content

Instantly share code, notes, and snippets.

View randyphoa's full-sized avatar

Randy Phoa randyphoa

  • IBM Data Science and AI Elite
View GitHub Profile
@randyphoa
randyphoa / wml_utils.py
Created January 11, 2021 07:12
A Simple Way to Manage Data Science Assets with IBM Watson Machine Learning API
import requests
# from watson_machine_learning_client import WatsonMachineLearningAPIClient
from ibm_watson_machine_learning import APIClient
class WMLUtils:
def __init__(self, wml_credentials):
# self.client = WatsonMachineLearningAPIClient(wml_credentials)
self.client = APIClient(wml_credentials)
def set_deployment_space(self, deployment_space):
@randyphoa
randyphoa / Deploy Prophet Model.ipynb
Created December 13, 2021 04:15
Deploying your Prophet model with custom environments on Watson Machine Learning
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@randyphoa
randyphoa / create-custom-environment.py
Last active June 7, 2022 05:16
Deploy R in online mode - create custom environment
# create a conda config with the require libraries
config_yaml = """
name: custom
channels:
- conda-forge
dependencies:
- r
- libiconv
"""
@randyphoa
randyphoa / train-iris-model.R
Last active June 7, 2022 05:15
Deploy R in online mode - train iris model
# install specific version of xgboost to ensure the development and deployment environment have the same version
install.packages("https://cloud.r-project.org/src/contrib/Archive/xgboost/xgboost_1.5.2.1.tar.gz", repos=NULL, type="source")
library(xgboost)
data(iris)
species <- iris$Species
label <- as.integer(iris$Species) - 1
@randyphoa
randyphoa / predict-function.R
Created June 7, 2022 02:12
Deploy R in online mode - predict function
#!/usr/bin/env Rscript
library(xgboost)
library(jsonlite)
args <- commandArgs(trailingOnly = TRUE)
if (length(args) == 0) {
stop("Insufficient arguments.", call. = FALSE)
}
@randyphoa
randyphoa / deploy-python-function-to-call-R.py
Created June 7, 2022 05:18
Deploy R in online mode - deploy python function to call R
params = {
"wml_credentials": wml_credentials,
"space_id": SPACE_ID,
"script": "predict.R",
"required_assets": ["iris_xgb.model"],
"r_packages": ["jsonlite", "xgboost"],
}
def r_function(params=params):
"""
@randyphoa
randyphoa / r-online-deployment-demo.ipynb
Created June 7, 2022 06:43
Deploy R in online mode - complete example notebook
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@randyphoa
randyphoa / batch-job-to-deploy-R-model.py
Created June 7, 2022 08:12
Deploy R in online mode - batch job to deploy R model
config = {
"function_name": "lead scoring",
"script": "lead_scoring.R",
"required_assets": ["products.csv", "transactions.rds", "xgboost.rds", "mssql-jdbc-10.2.1.jre11.jar"],
"r_packages": ["dplyr", "RJDBC", "xgboost"],
}
payload = {"scoring": {"input_data": [{"values": [config]}]}, "deployment": {"id": deployment_id}, "space_id": SPACE_ID, "name": "lead_scoring_deployment"}
headers = {"Content-Type": "application/json", "Accept": "application/json", "Authorization": wml_client._get_headers()["Authorization"]}
params = {"version": "2021-06-24", "space_id": SPACE_ID}
@randyphoa
randyphoa / cpd-operators.yaml
Last active July 15, 2022 01:26
Cloud Pak for Data operators deployment file
apiVersion: v1
kind: Namespace
metadata:
name: ibm-common-services
---
apiVersion: v1
kind: Namespace
metadata:
name: cpd
---
@randyphoa
randyphoa / cpd-services.yaml
Created July 15, 2022 01:26
Cloud Pak for Data services deployment file
apiVersion: databases.cpd.ibm.com/v1
kind: Db2oltpService
metadata:
name: db2oltp-cr
namespace: cpd
spec:
license:
accept: true
license: Standard
---