Scripts Used in Examples
These scripts contain the Python code for creating the user-defined functions that are used in the REST API for Embedded Python Execution examples. For use in REST API for Embedded Python Execution function endpoints, a script must be saved in the Oracle Machine Learning for Python (OML4Py) script repository as a string, rather than as a Python function.
For more examples of creating scripts and of using OML4Py functions, see the OML Notebooks in your Autonomous Database service.
Data that Some Scripts Use
The following Python code creates the oml_iris
object, which contains data that some of the scripts use.
from sklearn import datasets
import pandas as pd
import oml
# Load the iris data set and create a pandas.DataFrame for it.
iris = datasets.load_iris()
x = pd.DataFrame(iris.data, columns = ['Sepal_Length','Sepal_Width',
'Petal_Length','Petal_Width'])
y = pd.DataFrame(list(map(lambda x: {0: 'setosa', 1: 'versicolor',
2:'virginica'}[x], iris.target)),
columns = ['Species'])
# Create the IRIS database table and the proxy object for the table.
oml_iris = oml.create(pd.concat([x, y], axis=1), table = 'IRIS')
compute_random_mean
The compute_random_mean
function returns the mean of a set of random numbers. The following defines the script and stores it in the script repository.
compute_random_mean = """def compute_random_mean(index):
import numpy as np
import scipy
from statistics import mean
np.random.seed(index)
res = np.random.random((100,1))*10
return mean(res[1])"""
oml.script.create("compute_random_mean", compute_random_mean, is_global=True, overwrite=True)
group_count
The group_count
function returns the counts for each category in the Species column with the results sorted by the Sepal_Length column. This example creates a string that defines the group_count
function and saves it in the script repository.
The function uses data from the oml_iris
object.
The following defines the script and stores it in the script repository.
group_count = """def group_count(dat):
import oml
import pandas as pd
return pd.DataFrame([(dat["Species"][0], dat["Sepal_Length"][0], dat.shape[0])],
columns = ["Species", "Sepal_Length", "Count"])"""
oml.script.create("group_count", func=group_count, is_global=True, overwrite=True)
my_predict
The my_predict
function returns a prediction based on a LinearRegression model.
The following creates a LinearRegression model and stores it in an OML4Py datastore. It then defines the my_predict
function and stores it in the script repository.
from sklearn import linear_model
# Build a regression model using in-memory data.
iris = oml_iris.pull()
regr = linear_model.LinearRegression()
regr.fit(iris[['Sepal_Width', 'Petal_Length', 'Petal_Width']],
iris[['Sepal_Length']])
regr.coef_
oml.ds.save(objs={'regr':regr}, name="ds_regr", grantable=True, overwrite=True)
my_predict = """def my_predict(dat):
import pandas as pd
import oml
obj_dict = oml.ds.load(name="ds_regr", to_globals=False)
regr = obj_dict["regr"]
pred = regr.predict(dat[['Sepal_Length',
'Sepal_Width',
'Petal_Length']])
return pd.concat([dat[['Species', 'Petal_Width']],
pd.DataFrame(pred,
columns=['Pred_Petal_Width'])],
axis=1)"""
oml.script.create("my_predict", func=my_predict, is_global=True, overwrite=True)
pred_lm
The pred_lm
function returns a prediction from a LinearRegression model.
The following defines the pred_lm
function and stores it in the script repository.
pred_lm = """def pred_lm(dat):
from sklearn import linear_model
import oml
lm = linear_model.LinearRegression()
X = dat[["Petal_Width"]]
y = dat[["Petal_Length"]]
mod = lm.fit(X, y)
pred = mod.predict(dat[["Petal_Width"]])"""
oml.script.create("pred_lm", func=pred_lm, is_global=True, overwrite=True)
RandomRedDots
The RandomRedDots
function creates a simple DataFrame
and generates two plots of random red dots. This example defines the function and stores it as a script in the script repository.
RandomRedDots = """def RandomRedDots (num_dots_1=100, num_dots_2=10):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
d = {'id': range(1,10), 'val': [x/100 for x in range(1,10)]}
df = pd.DataFrame(data=d)
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.scatter(range(0,int(num_dots_1)), np.random.rand(int(num_dots_1)),c='r')
fig.suptitle("Random Red Dots")
fig2 = plt.figure(2)
ax2 = fig2.add_subplot(111)
ax2.scatter(range(0,int(num_dots_2)), np.random.rand(int(num_dots_2)),c='r')
fig2.suptitle("Random Red Dots")
return df"""
oml.script.create("RandomRedDots", func=RandomRedDots, is_global=True, overwrite=True)
return_df
The return_df
function returns a pandas.DataFrame
.
The following defines the script and stores it in the script repository.
return_df = """def return_df(num, scale):
import pandas as pd
id = list(range(0, int(num)))
res = [i/scale for i in id]
return pd.DataFrame({"ID":id, "RES":res})"""
oml.script.create("return_df", func=return_df, is_global=False, overwrite=True)