<jupyter_start><jupyter_text>Credit Card Examplescikit-learn to ONNX  imports<jupyter_code>import numpy as np
import pandas as pd
import onnxruntime as rt
import sklearn
import skl2onnx
import onnx

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler, LabelEncoder, normalize
from sklearn.metrics import accuracy_score
%load_ext watermark
%watermark --iversions<jupyter_output>numpy       1.18.1
sklearn     0.23.2
onnxruntime 1.4.0
onnx        1.7.0
skl2onnx    1.7.0
pandas      1.1.1<jupyter_text>Data Load<jupyter_code>cust_df = pd.read_csv('cust_history_new_1K.csv')

print("There are " + str(len(cust_df)) + " observations in the customer history dataset.")
print("There are " + str(len(cust_df.columns)) + " variables in the dataset.")

#cust_df.head()<jupyter_output>There are 1000 observations in the customer history dataset.
There are 19 variables in the dataset.<jupyter_text>Data Preparation  Split Dataframe into Features and Label<jupyter_code>cust_df_Y = cust_df[['IS_DEFAULT']]
cust_df_X = cust_df.drop(['IS_DEFAULT'],axis=1)

print(f'cust_df_X.shape={cust_df_X.shape}, cust_df_Y.shape={cust_df_Y.shape}')<jupyter_output>cust_df_X.shape=(1000, 18), cust_df_Y.shape=(1000, 1)<jupyter_text>Transform Label<jupyter_code>le = LabelEncoder()
cust_df_Y = le.fit_transform(cust_df_Y['IS_DEFAULT'])
type(cust_df_Y)<jupyter_output><empty_output><jupyter_text>Transform Features  One hot encoding for categorical Columns<jupyter_code>categoricalColumns = ['CREDIT_HISTORY', 'TRANSACTION_CATEGORY', 'ACCOUNT_TYPE', 'ACCOUNT_AGE',
                      'STATE', 'IS_URBAN', 'IS_STATE_BORDER', 'HAS_CO_APPLICANT', 'HAS_GUARANTOR',
                      'OWN_REAL_ESTATE', 'OTHER_INSTALMENT_PLAN',
                      'OWN_RESIDENCE', 'RFM_SCORE', 'OWN_CAR', 'SHIP_INTERNATIONAL']
cust_df_X = pd.get_dummies(cust_df_X, columns=categoricalColumns)

#cust_df_X.head()<jupyter_output><empty_output><jupyter_text>Normalize Features<jupyter_code>min_max_scaler = MinMaxScaler()
features = min_max_scaler.fit_transform(cust_df_X)
features = normalize(features, axis=1, norm='l1')

cust_df_X = pd.DataFrame(features,columns=cust_df_X.columns)
#cust_df_X.head()<jupyter_output><empty_output><jupyter_text>Split Train and Test Dataset<jupyter_code>label    = cust_df_Y
features  = cust_df_X.values

label = np.reshape(label,(-1,1))
X_train,X_test,y_train,y_test = \
       train_test_split(features, label, test_size=0.3, random_state=42, stratify=label)
print(f'X_train.shape={X_train.shape} Y_train.shape={y_train.shape}')
print(f'X_test.shape={X_test.shape} Y_test.shape={y_test.shape}')<jupyter_output>X_train.shape=(700, 51) Y_train.shape=(700, 1)
X_test.shape=(300, 51) Y_test.shape=(300, 1)<jupyter_text>sklearn Train<jupyter_code># Import
from sklearn.linear_model import LogisticRegression
sklearn_lr = LogisticRegression()
# TRAIN
sklearn_lr.fit(X_train, y_train.ravel())
sklearn_lr.coef_.shape
sklearn_lr.coef_.dtype
sklearn_lr.coef_
sklearn_lr.intercept_.shape
sklearn_prediction = sklearn_lr.predict(X_test)
print(f'sklearn ml accuracy score = {accuracy_score(y_test,sklearn_prediction)}')
sklearn_prediction.shape<jupyter_output><empty_output><jupyter_text>Convert to ONNX format<jupyter_code>from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import DoubleTensorType
initial_type = [('double_input', DoubleTensorType([None, 51]))]
onx = convert_sklearn(sklearn_lr, initial_types=initial_type)
with open("cc_default.onnx", "wb") as f:
    f.write(onx.SerializeToString())<jupyter_output><empty_output><jupyter_text>View the ONNC model<jupyter_code># Load the ONNX model
model = onnx.load("cc_default.onnx")

# Check that the IR is well formed
onnx.checker.check_model(model)

# Print a human readable representation of the graph
print(model.graph)<jupyter_output>node {
  input: "double_input"
  output: "label"
  output: "probability_tensor"
  name: "LinearClassifier"
  op_type: "LinearClassifier"
  attribute {
    name: "classlabels_ints"
    ints: 0
    ints: 1
    type: INTS
  }
  attribute {
    name: "coefficients"
    floats: -0.7748340368270874
    floats: -0.41529330611228943
    floats: -0.006116610951721668
    floats: -0.6085692048072815
    floats: 1.2961233854293823
    floats: 0.08430526405572891
    floats: -0.11118007451295853
    floats: -0.580898642539978
    floats: -0.2937869131565094
    floats: 0.8845047950744629
    floats: -0.09702741354703903
    floats: -0.7301174402236938
    floats: -0.161238893866539
    floats: -0.08285558968782425
    floats: 0.5603021383285522
    floats: -0.14239107072353363
    floats: 0.27800247073173523
    floats: 1.1163368225097656
    floats: 0.30734995007514954
    floats: -1.4795174598693848
    floats: -0.10504589974880219
    floats: 0.7133731842041016
    floats: -0.13243678212165833
[...]<jupyter_text>View the model in https://lutzroeder.github.io/netron/  Use ONNX runtime for scoring<jupyter_code># Compute the prediction with ONNX Runtime
sess = rt.InferenceSession("cc_default.onnx")
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name
pred_onx = sess.run([label_name], {input_name: X_test})[0]
pred_onx.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11096231107709967, 'fraction_numerical': 0.09125693514444232, 'mean_word_length': 5.897097625329815, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '607921', 'license': 'Apache-2.0', 'n_tokens_mistral': 2321, 'n_tokens_neox': 2029, 'n_words': 336, 'path': 'basics/cc-fraud-sklearn-onnx.ipynb', 'repo_name': 'ksangeek/onnx-experiments', 'size': '5227'}