<jupyter_start><jupyter_text>Scikit-learn Deployment<jupyter_code>!pip install skl2onnx onnxmltools onnxruntime onnxoptimizer
from IPython import display
import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.svm import SVC
import matplotlib.pyplot as plt
iris = datasets.load_iris() # Setosa, Versicolour, and Virginica
df = pd.DataFrame(data= np.c_[iris['data'], iris['target']],
                     columns= iris['feature_names'] + ['target'])

df.head()
df["target"].value_counts()
display.Image("./misc/iris.jpeg")
import matplotlib.cm
cmap = matplotlib.cm.get_cmap('Set1')
fig = df.plot.scatter(x="sepal length (cm)", y="sepal width (cm)", c="target", cmap=cmap, colorbar=False)
plt.show()<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(df.iloc[:,:4], df.iloc[:,-1], test_size=0.3, random_state=142)
model = LogisticRegression()
model.fit(X_train, y_train)
print(f"Train score: {model.score(X_train, y_train)}")
print(f"Test score:  {model.score(X_test, y_test)}")
model.predict_proba(np.array([[1, 2, 3, 4]]))<jupyter_output><empty_output><jupyter_text>Model Deployment The pickle API for serializing standard Python objects.The joblib API for efficiently serializing Python objects with NumPy arrays.  Pickle<jupyter_code>import pickle

filename = './save/model.pkl'
pickle.dump(model, open(filename, 'wb'))
loaded_model = pickle.load(open(filename, 'rb'))
result = loaded_model.predict_proba(np.array([[5.5, 2.6, 2.7, 7.8]]))
print(result)<jupyter_output>[[7.27380947e-06 1.74212245e-05 9.99975305e-01]]<jupyter_text>Joblib<jupyter_code>import joblib

filename = './save/model.joblib'
joblib.dump(model, filename)
loaded_model = joblib.load(filename)
result = loaded_model.predict_proba(np.array([[0.5, 0.6, 0.7, 0.8]]))
print(result)<jupyter_output>[[9.95228088e-01 4.77185714e-03 5.48725454e-08]]<jupyter_text>ONNX<jupyter_code>import onnxmltools
from skl2onnx.common.data_types import FloatTensorType

filename = './save/model.onnx'
initial_types = [('input', FloatTensorType([1, 4]))]
onnx_model = onnxmltools.convert_sklearn(model, 'Iris Model', initial_types=initial_types)

with open(filename, "wb") as f:
    f.write(onnx_model.SerializeToString())
import onnxruntime as rt

sess = rt.InferenceSession(filename)
print(list(map(lambda x: x.name, sess.get_inputs())))
print(list(map(lambda x: x.name, sess.get_outputs())))

inputs = sess.get_inputs()
outputs = sess.get_outputs()
input_name = inputs[0].name
input_data = {input_name: np.array([[0.5, 0.6, 0.7, 0.8]]).astype(np.float32)}

pred_onx = sess.run([], input_data)
pred_onx<jupyter_output>['input']
['output_label', 'output_probability']<jupyter_text>ONNX LinearRegression<jupyter_code>model = LinearRegression()
model.fit(X_train, y_train)

filename = './save/model_linear.onnx'
initial_types = [('input', FloatTensorType([1, 4]))]
onnx_model = onnxmltools.convert_sklearn(model, 'Iris Model', initial_types=initial_types)

with open(filename, "wb") as f:
    f.write(onnx_model.SerializeToString())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1487115802545793, 'fraction_numerical': 0.03446134740763738, 'mean_word_length': 7.2615384615384615, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '310832', 'license': 'MIT', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1213, 'n_words': 221, 'path': 'sklearn/sklearn.ipynb', 'repo_name': 'spugachev/mlops', 'size': '3221'}