Model.to_python

In [ ]:
Model.to_python(name: str = "predict", 
                return_proba: bool = False, 
                return_distance_clusters: bool = False, 
                return_str: bool = False,)

Returns the Python code needed to deploy the model without using built-in Vertica functions.

Parameters

Name Type Optional Description
name
bool
Function Name.
return_proba
list
If set to True and the model is a classifier, the function will return the model probabilities.
return_distance_clusters
bool
If set to True and the model type is KMeans or NearestCentroids, the function will return the model clusters distances.
return_str
bool
If set to True, the function str will be returned.

Returns

str / func : Python function

Example

In [12]:
from verticapy.learn.ensemble import XGBoostClassifier
model = XGBoostClassifier("xgb_iris", max_ntree = 2, max_depth = 2)
model.drop()
model.fit("public.iris", 
          ["PetalLengthCm", "PetalWidthCm",],
          "Species")

# Returns the string needed to deploy the model's prediction in Python
print(model.to_python(return_str = True))
def predict(X):
	import numpy as np
	n = 2
	classes = np.array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'])
	tree_list = []
	tree_list += [[[1, None, None], [2, None, None], [0, None, None], ['1.921875', None, None], [None, '', ''], [False, None, None], [None, {'Iris-versicolor': -1.5, 'Iris-virginica': -1.5, 'Iris-setosa': 3.0}, {'Iris-versicolor': 0.75, 'Iris-virginica': 0.75, 'Iris-setosa': -1.5}]]]
	tree_list += [[[1, None, None], [2, None, None], [1, None, None], ['0.625000', None, None], [None, '', ''], [False, None, None], [None, {'Iris-versicolor': -1.43035, 'Iris-virginica': -1.43035, 'Iris-setosa': 2.48164}, {'Iris-versicolor': 0.658272, 'Iris-virginica': 0.658272, 'Iris-setosa': -1.43035}]]]
	def predict_tree(tree, node_id, X,):
		if tree[0][node_id] == tree[1][node_id]:
			return tree[6][node_id]
		else:
			idx, right_node, left_node = tree[2][node_id], tree[1][node_id], tree[0][node_id]
			if (tree[5][node_id] and str(X[idx]) == tree[3][node_id]) or (not(tree[5][node_id]) and float(X[idx]) < float(tree[3][node_id])):
				return predict_tree(tree, left_node, X)
			else:
				return predict_tree(tree, right_node, X)
	def predict_tree_final(X):
		result = [predict_tree(tree, 0, X,) for tree in tree_list]
		all_classes_score = {}
		for elem in classes:
			all_classes_score[elem] = 0
		logodds = np.array([-0.6931471805599454, -0.6931471805599454, -0.6931471805599454, ])
		for idx, elem in enumerate(all_classes_score):
			for val in result:
				all_classes_score[elem] += val[elem]
			all_classes_score[elem] = 1 / (1 + np.exp( - (logodds[idx] + 0.1 * all_classes_score[elem])))
		result = [all_classes_score[elem] for elem in all_classes_score]
		return classes[np.argmax(np.array(result))]
	return np.apply_along_axis(predict_tree_final, 1, X)

In [13]:
# Only for classification
# Returns the string needed to deploy the model's probability in Python
print(model.to_python(return_str = True, return_proba = True))
def predict(X):
	import numpy as np
	n = 2
	classes = np.array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'])
	tree_list = []
	tree_list += [[[1, None, None], [2, None, None], [0, None, None], ['1.921875', None, None], [None, '', ''], [False, None, None], [None, {'Iris-versicolor': -1.5, 'Iris-virginica': -1.5, 'Iris-setosa': 3.0}, {'Iris-versicolor': 0.75, 'Iris-virginica': 0.75, 'Iris-setosa': -1.5}]]]
	tree_list += [[[1, None, None], [2, None, None], [1, None, None], ['0.625000', None, None], [None, '', ''], [False, None, None], [None, {'Iris-versicolor': -1.43035, 'Iris-virginica': -1.43035, 'Iris-setosa': 2.48164}, {'Iris-versicolor': 0.658272, 'Iris-virginica': 0.658272, 'Iris-setosa': -1.43035}]]]
	def predict_tree(tree, node_id, X,):
		if tree[0][node_id] == tree[1][node_id]:
			return tree[6][node_id]
		else:
			idx, right_node, left_node = tree[2][node_id], tree[1][node_id], tree[0][node_id]
			if (tree[5][node_id] and str(X[idx]) == tree[3][node_id]) or (not(tree[5][node_id]) and float(X[idx]) < float(tree[3][node_id])):
				return predict_tree(tree, left_node, X)
			else:
				return predict_tree(tree, right_node, X)
	def predict_tree_final(X):
		result = [predict_tree(tree, 0, X,) for tree in tree_list]
		all_classes_score = {}
		for elem in classes:
			all_classes_score[elem] = 0
		logodds = np.array([-0.6931471805599454, -0.6931471805599454, -0.6931471805599454, ])
		for idx, elem in enumerate(all_classes_score):
			for val in result:
				all_classes_score[elem] += val[elem]
			all_classes_score[elem] = 1 / (1 + np.exp( - (logodds[idx] + 0.1 * all_classes_score[elem])))
		result = [all_classes_score[elem] for elem in all_classes_score]
		return np.array(result) / np.sum(result)
	return np.apply_along_axis(predict_tree_final, 1, X)

In [14]:
from verticapy.learn.cluster import KMeans
model = KMeans("kmeans_iris",)
model.drop()
model.fit("public.iris",)

# Only for clustering
# Returns the string needed to deploy the distance to the model's clusters in Python
print(model.to_python(return_str = True, return_distance_clusters = True))
def predict(X):
	import numpy as np
	centroids = np.array([[7.15, 2.9, 5.98333333333333, 1.83333333333333], [5.006, 3.418, 1.464, 0.244], [6.02777777777778, 2.73333333333333, 5.02777777777778, 1.79444444444444], [5.68260869565217, 2.68260869565217, 4.16086956521739, 1.30434782608696], [7.64285714285714, 3.22857142857143, 6.5, 2.2], [6.45, 3.01666666666667, 4.60555555555556, 1.43888888888889], [6.56190476190476, 3.11428571428571, 5.52380952380952, 2.18095238095238], [5.24285714285714, 2.37142857142857, 3.44285714285714, 1.02857142857143]])
	result = []
	for centroid in centroids:
		result += [np.sum((np.array(centroid) - X) ** 2, axis=1) ** (1 / 2)]
	result = np.column_stack(result)
	return result

In [15]:
# Getting in-memory predictions
model.to_python()([[7.15, 2.9, 5.9, 1.8], [3.15, 12.9, 15.9, 0.8]])
Out[15]:
array([0, 4])