我处于必须在 Oracle 数据库上运行随机森林模型的情况。可以生成执行与 Python Sk-learn RF 模型相同功能的 PL/SQL 包。
一旦你从这个 SO中得到了 Daniele 的回答,这很简单
首先你有这个文件:rforest_to_plsql.py
def t(n):
return " " * 4 * n
def get_est_code(tree, feature_names):
left = tree.tree_.children_left
right = tree.tree_.children_right
threshold = tree.tree_.threshold
features = [feature_names[i] for i in tree.tree_.feature]
value = tree.tree_.value
def recurse(left, right, threshold, features, node, depth, code):
if (threshold[node] != -2):
code += t(depth) + "if ( " + features[node] + " <= " + str(threshold[node]) + " ) then\n"
depth += 1
if left[node] != -1:
code = recurse (left, right, threshold, features,left[node], depth, code)
code += t(depth - 1) + "else\n"
if right[node] != -1:
code = recurse (left, right, threshold, features,right[node], depth, code)
code += t(depth - 1) + "end if;\n"
depth -= 1
else:
code += t(depth) + "return two_values(" + str(value[node][0][0]) + ", " + str(value[node][0][1]) + ");\n"
return code
return recurse(left, right, threshold, features, 0, 2, "")
def get_pkg_header_code(clf, feature_names):
pkg_h_code = """create or replace package pkg_rforest_model as
function predict_proba (\n"""
for feat in feature_names:
pkg_h_code += t(2) + feat + " number,\n"
pkg_h_code = pkg_h_code[:-2] + ") return number;\n"
pkg_h_code += "end pkg_rforest_model;"
return pkg_h_code
def get_pkg_body_code(clf, feature_names):
pkg_b_code = "create or replace package body pkg_rforest_model as\n"
#code for each estimator
for index, estimator in enumerate(clf.estimators_):
func_name = "f_est_" + str(index).zfill(3)
pkg_b_code += t(1) + "function " + func_name + " (\n"
for feat in feature_names:
pkg_b_code += t(2) + feat + " number,\n"
pkg_b_code = pkg_b_code[:-2] + ") return two_values as\n begin\n"
pkg_b_code += get_est_code(clf.estimators_[index], ["f" + str(i) for i in range(7)])
pkg_b_code += " end " + func_name + ";\n"
#this function calls all each estimator function and returns a weighted probability
pkg_b_code += " function predict_proba (\n"
for feat in feature_names:
pkg_b_code += t(2) + feat + " number,\n"
pkg_b_code = pkg_b_code[:-2] + ") return number as\n v_prob number;\n"
for index, estimator in enumerate(clf.estimators_):
func_name = "f_est_" + str(index).zfill(3)
pkg_b_code += t(2) + "v_" + func_name + "_a number;\n"
pkg_b_code += t(2) + "v_" + func_name + "_b number;\n"
pkg_b_code += t(2) + "pr_est_" + str(index).zfill(3) + " number;\n"
pkg_b_code += t(1) + "begin\n"
for index, estimator in enumerate(clf.estimators_):
func_name = "f_est_" + str(index).zfill(3)
pkg_b_code += t(2) + "v_" + func_name + "_a := " + func_name+ "(" + ", ".join(feature_names) + ").a;\n"
pkg_b_code += t(2) + "v_" + func_name + "_b := " + func_name+ "(" + ", ".join(feature_names) + ").b;\n"
pkg_b_code += t(2) + "pr_est_" + str(index).zfill(3) + " := v_" + func_name + "_a / ( v_" + \
func_name + "_a + v_" + func_name + "_b);\n"
pkg_b_code += t(2) + "return ("
for index, estimator in enumerate(clf.estimators_):
pkg_b_code += "pr_est_" + str(index).zfill(3) + " + "
pkg_b_code = pkg_b_code[:-2] + ") / " + str(len(clf.estimators_)) + ";\n"
pkg_b_code += t(1) + "end predict_proba;\n"
pkg_b_code += "end pkg_rforest_model;"
return pkg_b_code
然后你训练你的模型,并让 PL/SQL 代码返回文件的函数:
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
import rforest_to_plsql
n_features = 4
X, y = make_classification(n_samples=1000, n_features=n_features,
n_informative=2, n_redundant=0,
random_state=0, shuffle=False)
clf = RandomForestClassifier(max_depth=2, random_state=0)
clf.fit(X, y)
features = ["f" + str(i) for i in range(n_features)]
pkg_h_code = rforest_to_plsql.get_pkg_header_code(clf, features)
pkg_b_code = rforest_to_plsql.get_pkg_body_code(clf, features)
print pkg_h_code
print pkg_b_code
在数据库上创建该包后,您可以执行以下操作:
select pkg_rforest_model.predict_proba(0.513889 , 0.511111 , 0.491667 , 0)
from dual;
这是纯 PL/SQL 并且应该运行得非常快。如果你有一个非常大的 RF,那么你可以在本地编译包以获得更高的性能。请注意 - 包裹可能是 1000 个 LOC 中的 10 个。