<jupyter_start><jupyter_text>Linear Regression<jupyter_code>reg=  TheilSenRegressor(random_state=42)
reg = make_pipeline(PolynomialFeatures(2), reg)
reg.fit(train_features, train_target)
test_dataset['max_tube_diameter LR'] = reg.predict(test_features)

od = np.array([cuffless_OD[np.abs(cuffless_OD - mtd).argmin()] for mtd in test_dataset['max_tube_diameter LR']])
test_dataset['OD cuffless LR'] = cuffless_OD[np.maximum([j if i>0 else j-1 
                                for i, j in zip(np.sign(test_dataset['max_tube_diameter LR'] - od),
                                                [np.where(cuffless_OD == c)[0].item() for c in od])], 0)]


od = np.array([cuffed_OD[np.abs(cuffed_OD - mtd).argmin()] for mtd in test_dataset['max_tube_diameter LR']])
test_dataset['OD cuffed LR'] = cuffed_OD[np.maximum([j if i>0 else j-1 
                                for i, j in zip(np.sign(test_dataset['max_tube_diameter LR'] - od),
                                                [np.where(cuffed_OD == c)[0].item() for c in od])], 0)]<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>forest = RandomForestRegressor(random_state=42)

hyper_parameters = dict(n_estimators = [100, 300, 500, 800, 1200],
                        max_depth = [5, 8, 15, 25, 30, 50, 100, 150],
                        min_samples_split = [2, 5, 10, 15, 30, 50, 100],
                        min_samples_leaf = [1, 2, 5, 10, 15])

grid_search = GridSearchCV(forest, hyper_parameters, cv = 2, verbose = 1, n_jobs = 20)

best_rf = grid_search.fit(train_features, train_target)

print(grid_search.best_estimator_)
test_dataset['max_tube_diameter RF'] = best_rf.predict(test_features)

od = np.array([cuffless_OD[np.abs(cuffless_OD - mtd).argmin()] for mtd in test_dataset['max_tube_diameter RF']])
test_dataset['OD cuffless RF'] = cuffless_OD[np.maximum([j if i>0 else j-1 
                                for i, j in zip(np.sign(test_dataset['max_tube_diameter RF'] - od),
                                                [np.where(cuffless_OD == c)[0].item() for c in od])], 0)]


od = np.array([cuffed_OD[np.abs(cuffed_OD - mtd).argmin()] for mtd in test_dataset['max_tube_diameter RF']])
test_dataset['OD cuffed RF'] = cuffed_OD[np.maximum([j if i>0 else j-1 
                                for i, j in zip(np.sign(test_dataset['max_tube_diameter RF'] - od),
                                                [np.where(cuffed_OD == c)[0].item() for c in od])], 0)]<jupyter_output><empty_output><jupyter_text>Gradient Boosting<jupyter_code>boosting = xgb.XGBRegressor(verbosity=2, nthread=20,
                            eta=0.2, gamma=0, max_depth=50, reg_lambda=1, reg_alpha=1, tree_method='exact',
                            objective='reg:squarederror', eval_metric='mae')

boosting.fit(train_features, train_target)
test_dataset['max_tube_diameter GB'] = boosting.predict(test_features)

od = np.array([cuffless_OD[np.abs(cuffless_OD - mtd).argmin()] for mtd in test_dataset['max_tube_diameter GB']])
test_dataset['OD cuffless GB'] = cuffless_OD[np.maximum([j if i>0 else j-1 
                                for i, j in zip(np.sign(test_dataset['max_tube_diameter GB'] - od),
                                                [np.where(cuffless_OD == c)[0].item() for c in od])], 0)]


od = np.array([cuffed_OD[np.abs(cuffed_OD - mtd).argmin()] for mtd in test_dataset['max_tube_diameter GB']])
test_dataset['OD cuffed GB'] = cuffed_OD[np.maximum([j if i>0 else j-1 
                                for i, j in zip(np.sign(test_dataset['max_tube_diameter GB'] - od),
                                                [np.where(cuffed_OD == c)[0].item() for c in od])], 0)]<jupyter_output><empty_output><jupyter_text>Neural Network<jupyter_code>normalizer = tf.keras.layers.experimental.preprocessing.Normalization()
normalizer.adapt(np.array(train_features))

model = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=(2,), dtype=tf.float64),
    normalizer,
    tf.keras.layers.Dense(units=64, activation='relu'),
    tf.keras.layers.Dense(units=1)
])


model.compile(
    optimizer=tf.optimizers.Adam(learning_rate=0.001),
    loss='mse',
    metrics=[tf.keras.metrics.RootMeanSquaredError(),
            tf.keras.metrics.MeanAbsoluteError()])

history = model.fit(x=train_features, y=train_target,
                    validation_data=(test_features, test_target),
                    epochs=300)
test_dataset['max_tube_diameter NN'] = [model(np.array([row[1][:2]])).numpy().item() for row in test_dataset.iterrows()]

od = np.array([cuffless_OD[np.abs(cuffless_OD - mtd).argmin()] for mtd in test_dataset['max_tube_diameter NN']])
test_dataset['OD cuffless NN'] = cuffless_OD[np.maximum([j if i>0 else j-1 
                                for i, j in zip(np.sign(test_dataset['max_tube_diameter NN'] - od),
                                                [np.where(cuffless_OD == c)[0].item() for c in od])], 0)]


od = np.array([cuffed_OD[np.abs(cuffed_OD - mtd).argmin()] for mtd in test_dataset['max_tube_diameter NN']])
test_dataset['OD cuffed NN'] = cuffed_OD[np.maximum([j if i>0 else j-1 
                                for i, j in zip(np.sign(test_dataset['max_tube_diameter NN'] - od),
                                                [np.where(cuffed_OD == c)[0].item() for c in od])], 0)]<jupyter_output><empty_output><jupyter_text>Results<jupyter_code>pred = test_dataset[['age en mois', 'max_tube_diameter LR', "max_tube_diameter RF",
                     "max_tube_diameter GB", "max_tube_diameter NN"]]
pred = pred.set_index('age en mois', drop=True)
sns.lineplot(data=pred,  palette = "Dark2", linewidth=3);
sns.scatterplot(data=test_dataset, x='age en mois', y='max_tube_diameter');
sns.scatterplot(data=train_dataset, x='age en mois', y='max_tube_diameter', alpha=0.4);
print('--- Cuffless predictions ---')
print()
print('LR Adapted diameter: {:.2%}'.format(sum(test_dataset['OD cuffless LR'] <= test_dataset['OD cuffless ref']) / len(test_dataset)))
print('RF Adapted diameter: {:.2%}'.format(sum(test_dataset['OD cuffless RF'] <= test_dataset['OD cuffless ref']) / len(test_dataset)))
print('GB Adapted diameter: {:.2%}'.format(sum(test_dataset['OD cuffless GB'] <= test_dataset['OD cuffless ref']) / len(test_dataset)))
print('NN Adapted diameter: {:.2%}'.format(sum(test_dataset['OD cuffless NN'] <= test_dataset['OD cuffless ref']) / len(test_dataset)))
print()
print('LR Exact diameter: {:.2%}'.format(sum(test_dataset['OD cuffless LR'] == test_dataset['OD cuffless ref']) / len(test_dataset)))
print('RF Exact diameter: {:.2%}'.format(sum(test_dataset['OD cuffless RF'] == test_dataset['OD cuffless ref']) / len(test_dataset)))
print('GB Exact diameter: {:.2%}'.format(sum(test_dataset['OD cuffless GB'] == test_dataset['OD cuffless ref']) / len(test_dataset)))
print('NN Exact diameter: {:.2%}'.format(sum(test_dataset['OD cuffless NN'] == test_dataset['OD cuffless ref']) / len(test_dataset)))
print()
print('--- Cuffed prediction ---')
print()
print('LR Adapted diameter: {:.2%}'.format(sum(test_dataset['OD cuffed LR'] <= test_dataset['OD cuffed ref']) / len(test_dataset)))
print('RF Adapted diameter: {:.2%}'.format(sum(test_dataset['OD cuffed RF'] <= test_dataset['OD cuffed ref']) / len(test_dataset)))
print('GB Adapted diameter: {:.2%}'.format(sum(test_dataset['OD cuffed GB'] <= test_dataset['OD cuffed ref']) / len(test_dataset)))
print('NN Adapted diameter: {:.2%}'.format(sum(test_dataset['OD cuffed NN'] <= test_dataset['OD cuffed ref']) / len(test_dataset)))
print()
print('LR Exact diameter: {:.2%}'.format(sum(test_dataset['OD cuffed LR'] == test_dataset['OD cuffed ref']) / len(test_dataset)))
print('RF Exact diameter: {:.2%}'.format(sum(test_dataset['OD cuffed RF'] == test_dataset['OD cuffed ref']) / len(test_dataset)))
print('GB Exact diameter: {:.2%}'.format(sum(test_dataset['OD cuffed GB'] == test_dataset['OD cuffed ref']) / len(test_dataset)))
print('NN Exact diameter: {:.2%}'.format(sum(test_dataset['OD cuffed NN'] == test_dataset['OD cuffed ref']) / len(test_dataset)))
print()
print('--- Prediction errors ---')
print('Mean Absolute Error')
print('LR: {}'.format(mean_absolute_error(test_dataset['max_tube_diameter'], test_dataset['max_tube_diameter LR'])))
print('RF: {}'.format(mean_absolute_error(test_dataset['max_tube_diameter'], test_dataset['max_tube_diameter RF'])))
print('GB: {}'.format(mean_absolute_error(test_dataset['max_tube_diameter'], test_dataset['max_tube_diameter GB'])))
print('NN: {}'.format(mean_absolute_error(test_dataset['max_tube_diameter'], test_dataset['max_tube_diameter NN'])))
print('Maximum Residual Error')
print('LR: {}'.format(max_error(test_dataset['max_tube_diameter'], test_dataset['max_tube_diameter LR'])))
print('RF: {}'.format(max_error(test_dataset['max_tube_diameter'], test_dataset['max_tube_diameter RF'])))
print('GB: {}'.format(max_error(test_dataset['max_tube_diameter'], test_dataset['max_tube_diameter GB'])))
print('NN: {}'.format(max_error(test_dataset['max_tube_diameter'], test_dataset['max_tube_diameter NN'])))
def plot_loss(history, name):
    plt.plot(history.history['loss'], label='loss')
    plt.plot(history.history['val_loss'], label='val_loss')
    plt.xlabel('Epoch')
    plt.ylabel('Loss (' + name + ')')
    plt.legend()
    plt.grid(True)
    
def plot_rmse(history, name):
    plt.plot(history.history['root_mean_squared_error'], label='rmse')
    plt.plot(history.history['val_root_mean_squared_error'], label='val_rmse')
    plt.xlabel('Epoch')
    plt.ylabel('Metric (' + name + ')')
    plt.legend()
    plt.grid(True)
    
print('MAE_val: {}'.format(min(history.history['val_mean_absolute_error'])))
print('RMSE_val: {}'.format(min(history.history['val_root_mean_squared_error'])))

plot_loss(history, 'MAE');
plt.show();
plot_rmse(history, 'RMSE');
test_dataset.head()
initial_type = [('float_input', FloatTensorType([None, train_features.shape[1]]))]

onx = convert_sklearn(reg, initial_types=initial_type)
with open("onnx/theil_sen.onnx", "wb") as f:
    f.write(onx.SerializeToString())

    
onx = convert_sklearn(best_rf, initial_types=initial_type)
with open("onnx/random_forest.onnx", "wb") as f:
    f.write(onx.SerializeToString())

# sess = rt.InferenceSession("onnx/random_forest.onnx")
# input_name = sess.get_inputs()[0].name
# label_name = sess.get_outputs()[0].name
# pred_onx = sess.run([label_name], {input_name: np.array(test_features).astype(np.float32)})[0]
# print(pred_onx)

tfjs.converters.save_keras_model(model, 'onnx/neural_network')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16723581863024095, 'fraction_numerical': 0.013659647125782584, 'mean_word_length': 4.274137068534267, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '410594', 'license': 'Apache-2.0', 'n_tokens_mistral': 3905, 'n_tokens_neox': 3608, 'n_words': 657, 'path': 'prediction.ipynb', 'repo_name': 'aledelmo/Upper_Airways_Dimensions_DL', 'size': '10542'}