例で使用されるスクリプト
これらのスクリプトには、Embedded R ExecutionのREST API例で使用されるユーザー定義関数を作成するためのRコードが含まれています。Embedded R ExecutionのREST API関数エンドポイントで使用するには、スクリプトをR関数ではなく文字列としてOracle Machine Learning for R (OML4R)スクリプト・リポジトリに保存する必要があります。
スクリプトの作成例およびOML4R関数の使用例は、Autonomous DatabaseサービスのOMLノートブックを参照してください。
一部のスクリプトで使用されるデータ
次のRコードでは、一部のスクリプトで使用されるデータを含むIRIS
データ・フレーム・プロキシ・オブジェクトを作成します。
# Load libraries and suppress warnings
library(ORE)
options(ore.warn.order=FALSE)
# Drop IRIS table if it already exists
try(ore.drop(table="IRIS"))
ore.create(iris, table = "IRIS")
# print the first 6 lines of IRIS
head(IRIS)
compute.randomMean
compute.randomMean
関数は、乱数セットの平均を返します。次では、スクリプトを定義し、スクリプト・リポジトリに格納します。
compute.randomMean <- function(index)
{
set.seed(index)
x <- rnorm(10)
res <- mean(x)
res
}
ore.scriptCreate("compute.randomMean", compute.randomMean, global=TRUE, overwrite=TRUE)
group.count
group.count
関数は、Species列の各カテゴリのカウントを返します。この例では、group.count
関数を作成し、スクリプト・リポジトリに保存します。この関数は、IRIS
オブジェクトのデータを使用します。
group.count <- function(dat){
x <- data.frame(table(dat$Species))
names(x) <- c("Species", "Count")
x
}
ore.scriptCreate("group.count", FUN=group.count, global=TRUE, overwrite=TRUE)
my.predict
my_predict
関数は、LinearRegressionモデルに基づいて予測を返します。
次の例では、LinearRegressionモデルを作成してOML4Rデータストアに格納します。次に、my.predict
関数を定義し、スクリプト・リポジトリに格納します。
iris = ore.pull(IRIS)
regr = lm(Sepal.Length ~ ., data=iris)
coef(regr)
ore.save(regr, "regr", name="ds.regr", overwrite=TRUE)
my.predict <- function(dat)
{
ore.load(name="ds.regr")
pred = cbind(PRED=predict(regr, newdata=dat, supplemental.cols="Sepal.Length"), dat["Sepal.Length"])
pred
}
ore.scriptCreate("my.predict",
my.predict,
global=TRUE,
overwrite=TRUE)
RandomRedDots
RandomRedDots
関数は、単純なdata.frameを作成し、ランダムな赤い点のプロットを生成します。この例では、関数を定義し、スクリプト・リポジトリにスクリプトとして格納します。
RandomRedDots <- function(divisor = 100, numDots = 100) {
id <- 1:10
plot(1:numDots, rnorm(numDots), pch = 21, bg = "red", cex = 2 )
data.frame(id = id, val = id / divisor)}
ore.scriptCreate("RandomRedDots", FUN=RandomRedDots, global=TRUE, overwrite=TRUE)