import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20tensorflow%20as%20tf%0A%20%20%20%20from%20tensorflow%20import%20keras%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Keras%20%E3%82%AA%E3%83%BC%E3%83%88%E3%82%A8%E3%83%B3%E3%82%B3%E3%83%BC%E3%83%80%E3%81%A7%E7%95%B0%E5%B8%B8%E6%A4%9C%E7%9F%A5%0A%0A%20%20%20%20%E5%85%83%20notebook%20%E3%81%AF%20Kaggle%20%E3%81%AE%20credit-card%20fraud%20%E3%83%87%E3%83%BC%E3%82%BF%E3%82%BB%E3%83%83%E3%83%88%E3%82%92%20API%20token%20%E3%81%A7%E5%8F%96%E5%BE%97%E3%81%97%E3%81%A6%E3%81%84%E3%81%BE%E3%81%97%E3%81%9F%E3%80%82%0A%20%20%20%20%E3%81%93%E3%81%AE%20marimo%20%E7%89%88%E3%81%A7%E3%81%AF%E7%A7%98%E5%AF%86%E6%83%85%E5%A0%B1%E3%82%92%E4%BD%BF%E3%82%8F%E3%81%9A%E3%81%AB%E5%86%8D%E7%8F%BE%E3%81%A7%E3%81%8D%E3%82%8B%E3%81%93%E3%81%A8%E3%82%92%E5%84%AA%E5%85%88%E3%81%97%E3%80%81%0A%20%20%20%20%60sklearn.make_classification%60%20%E3%81%A7%E4%BD%9C%E3%82%8B%E4%B8%8D%E5%9D%87%E8%A1%A1%E3%81%AA%E7%96%91%E4%BC%BC%E3%83%88%E3%83%A9%E3%83%B3%E3%82%B6%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3%E3%83%87%E3%83%BC%E3%82%BF%E3%81%B8%E7%BD%AE%E3%81%8D%E6%8F%9B%E3%81%88%E3%81%A6%E3%81%84%E3%81%BE%E3%81%99%E3%80%82%0A%20%20%20%20%E6%AD%A3%E5%B8%B8%E3%83%87%E3%83%BC%E3%82%BF%E3%81%A7%E8%87%AA%E5%B7%B1%E5%86%8D%E6%A7%8B%E6%88%90%E3%82%92%E5%AD%A6%E7%BF%92%E3%81%97%E3%80%81%E5%86%8D%E6%A7%8B%E6%88%90%E8%AA%A4%E5%B7%AE%E3%81%A7%E7%95%B0%E5%B8%B8%E3%82%92%E6%A4%9C%E5%87%BA%E3%81%99%E3%82%8B%E6%84%8F%E5%9B%B3%E3%81%AF%E3%81%9D%E3%81%AE%E3%81%BE%E3%81%BE%E3%81%A7%E3%81%99%E3%80%82%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20tf.keras.utils.set_random_seed(42)%0A%20%20%20%20device_name%20%3D%20%22%2FGPU%3A0%22%20if%20tf.config.list_physical_devices(%22GPU%22)%20else%20%22%2FCPU%3A0%22%0A%20%20%20%20runtime_frame%20%3D%20pd.DataFrame(%7B%22package%22%3A%20%5B%22tensorflow%22%2C%20%22keras%22%2C%20%22numpy%22%2C%20%22device%22%5D%2C%20%22value%22%3A%20%5Btf.__version__%2C%20tf.keras.__version__%2C%20np.__version__%2C%20device_name%5D%7D)%0A%20%20%20%20runtime_frame%0A%20%20%20%20return%20(device_name%2C)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20sklearn.datasets%20import%20make_classification%0A%0A%20%20%20%20feature_names%20%3D%20%5Bf%22V%7Bi%3A02d%7D%22%20for%20i%20in%20range(1%2C%2029)%5D%20%2B%20%5B%22Amount%22%2C%20%22Velocity%22%5D%0A%20%20%20%20features%2C%20labels%20%3D%20make_classification(n_samples%3D12000%2C%20n_features%3D30%2C%20n_informative%3D12%2C%20n_redundant%3D10%2C%20n_clusters_per_class%3D1%2C%20weights%3D%5B0.992%2C%200.008%5D%2C%20class_sep%3D2.0%2C%20flip_y%3D0.001%2C%20random_state%3D42)%0A%20%20%20%20transactions%20%3D%20pd.DataFrame(features%2C%20columns%3Dfeature_names)%0A%20%20%20%20transactions%5B%22Class%22%5D%20%3D%20labels%0A%20%20%20%20return%20(transactions%2C)%0A%0A%0A%40app.cell%0Adef%20_(transactions)%3A%0A%20%20%20%20class_balance%20%3D%20transactions%5B%22Class%22%5D.value_counts().rename_axis(%22Class%22).reset_index(name%3D%22count%22)%0A%20%20%20%20class_balance%5B%22ratio%22%5D%20%3D%20class_balance%5B%22count%22%5D%20%2F%20class_balance%5B%22count%22%5D.sum()%0A%20%20%20%20class_balance%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(transactions)%3A%0A%20%20%20%20from%20sklearn.model_selection%20import%20train_test_split%0A%20%20%20%20from%20sklearn.preprocessing%20import%20StandardScaler%0A%0A%20%20%20%20train_frame%2C%20test_frame%20%3D%20train_test_split(transactions%2C%20test_size%3D0.33%2C%20random_state%3D2018%2C%20stratify%3Dtransactions%5B%22Class%22%5D)%0A%20%20%20%20train_normal%20%3D%20train_frame.loc%5Btrain_frame%5B%22Class%22%5D%20%3D%3D%200%5D.drop(columns%3D%22Class%22)%0A%20%20%20%20test_features%20%3D%20test_frame.drop(columns%3D%22Class%22)%0A%20%20%20%20test_labels%20%3D%20test_frame%5B%22Class%22%5D.to_numpy()%0A%20%20%20%20scaler%20%3D%20StandardScaler()%0A%20%20%20%20x_train%20%3D%20scaler.fit_transform(train_normal)%0A%20%20%20%20x_test%20%3D%20scaler.transform(test_features)%0A%20%20%20%20return%20test_labels%2C%20x_test%2C%20x_train%0A%0A%0A%40app.cell%0Adef%20_(device_name%2C%20x_train)%3A%0A%20%20%20%20with%20tf.device(device_name)%3A%0A%20%20%20%20%20%20%20%20autoencoder%20%3D%20keras.Sequential(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20keras.layers.Input(shape%3D(x_train.shape%5B1%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20keras.layers.Dense(24%2C%20activation%3D%22relu%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20keras.layers.Dense(12%2C%20activation%3D%22relu%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20keras.layers.Dense(4%2C%20activation%3D%22relu%22%2C%20name%3D%22bottleneck%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20keras.layers.Dense(12%2C%20activation%3D%22relu%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20keras.layers.Dense(24%2C%20activation%3D%22relu%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20keras.layers.Dense(x_train.shape%5B1%5D%2C%20activation%3D%22linear%22)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%20name%3D%22transaction_autoencoder%22)%0A%20%20%20%20%20%20%20%20autoencoder.compile(optimizer%3Dkeras.optimizers.Adam(learning_rate%3D1e-3)%2C%20loss%3D%22mse%22)%0A%20%20%20%20%20%20%20%20history%20%3D%20autoencoder.fit(x_train%2C%20x_train%2C%20validation_split%3D0.1%2C%20epochs%3D20%2C%20batch_size%3D256%2C%20verbose%3D0)%0A%20%20%20%20history_frame%20%3D%20pd.DataFrame(history.history)%0A%20%20%20%20return%20autoencoder%2C%20history_frame%0A%0A%0A%40app.cell%0Adef%20_(history_frame)%3A%0A%20%20%20%20history_frame.tail()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(history_frame)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(6%2C%204))%0A%20%20%20%20_ax.plot(history_frame.index%20%2B%201%2C%20history_frame%5B%22loss%22%5D%2C%20label%3D%22train%22)%0A%20%20%20%20_ax.plot(history_frame.index%20%2B%201%2C%20history_frame%5B%22val_loss%22%5D%2C%20label%3D%22validation%22)%0A%20%20%20%20_ax.set_xlabel(%22epoch%22)%0A%20%20%20%20_ax.set_ylabel(%22mse%22)%0A%20%20%20%20_ax.set_title(%22Autoencoder%20reconstruction%20loss%22)%0A%20%20%20%20_ax.legend()%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(autoencoder%2C%20x_test%2C%20x_train)%3A%0A%20%20%20%20train_reconstruction%20%3D%20autoencoder.predict(x_train%2C%20verbose%3D0)%0A%20%20%20%20test_reconstruction%20%3D%20autoencoder.predict(x_test%2C%20verbose%3D0)%0A%20%20%20%20train_errors%20%3D%20np.mean(np.square(x_train%20-%20train_reconstruction)%2C%20axis%3D1)%0A%20%20%20%20test_errors%20%3D%20np.mean(np.square(x_test%20-%20test_reconstruction)%2C%20axis%3D1)%0A%20%20%20%20threshold%20%3D%20float(np.quantile(train_errors%2C%200.99))%0A%20%20%20%20predicted_labels%20%3D%20(test_errors%20%3E%3D%20threshold).astype(int)%0A%20%20%20%20return%20predicted_labels%2C%20test_errors%2C%20threshold%2C%20train_errors%0A%0A%0A%40app.cell%0Adef%20_(predicted_labels%2C%20test_errors%2C%20test_labels%2C%20threshold)%3A%0A%20%20%20%20from%20sklearn.metrics%20import%20(%0A%20%20%20%20%20%20%20%20average_precision_score%2C%0A%20%20%20%20%20%20%20%20confusion_matrix%2C%0A%20%20%20%20%20%20%20%20precision_recall_curve%2C%0A%20%20%20%20%20%20%20%20precision_recall_fscore_support%2C%0A%20%20%20%20%20%20%20%20roc_auc_score%2C%0A%20%20%20%20%20%20%20%20roc_curve%2C%0A%20%20%20%20)%0A%0A%20%20%20%20precision%2C%20recall%2C%20_%20%3D%20precision_recall_curve(test_labels%2C%20test_errors)%0A%20%20%20%20fpr%2C%20tpr%2C%20_%20%3D%20roc_curve(test_labels%2C%20test_errors)%0A%20%20%20%20prf%20%3D%20precision_recall_fscore_support(test_labels%2C%20predicted_labels%2C%20average%3D%22binary%22%2C%20zero_division%3D0)%0A%20%20%20%20confusion%20%3D%20confusion_matrix(test_labels%2C%20predicted_labels)%0A%20%20%20%20metrics_frame%20%3D%20pd.DataFrame(%7B%22metric%22%3A%20%5B%22Average%20Precision%22%2C%20%22ROC%20AUC%22%2C%20%22Precision%40threshold%22%2C%20%22Recall%40threshold%22%2C%20%22F1%40threshold%22%2C%20%22threshold%22%5D%2C%20%22value%22%3A%20%5Baverage_precision_score(test_labels%2C%20test_errors)%2C%20roc_auc_score(test_labels%2C%20test_errors)%2C%20prf%5B0%5D%2C%20prf%5B1%5D%2C%20prf%5B2%5D%2C%20threshold%5D%7D)%0A%20%20%20%20curves%20%3D%20%7B%22precision%22%3A%20precision%2C%20%22recall%22%3A%20recall%2C%20%22fpr%22%3A%20fpr%2C%20%22tpr%22%3A%20tpr%7D%0A%20%20%20%20return%20confusion%2C%20curves%2C%20metrics_frame%0A%0A%0A%40app.cell%0Adef%20_(metrics_frame)%3A%0A%20%20%20%20metrics_frame%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(curves%2C%20test_errors%2C%20test_labels%2C%20threshold%2C%20train_errors)%3A%0A%20%20%20%20_fig%2C%20_axes%20%3D%20plt.subplots(1%2C%203%2C%20figsize%3D(15%2C%204))%0A%20%20%20%20_axes%5B0%5D.plot(curves%5B%22recall%22%5D%2C%20curves%5B%22precision%22%5D%2C%20color%3D%22tab%3Ablue%22)%0A%20%20%20%20_axes%5B0%5D.set_xlabel(%22recall%22)%0A%20%20%20%20_axes%5B0%5D.set_ylabel(%22precision%22)%0A%20%20%20%20_axes%5B0%5D.set_title(%22Precision-Recall%22)%0A%20%20%20%20_axes%5B1%5D.plot(curves%5B%22fpr%22%5D%2C%20curves%5B%22tpr%22%5D%2C%20color%3D%22tab%3Ared%22)%0A%20%20%20%20_axes%5B1%5D.plot(%5B0%2C%201%5D%2C%20%5B0%2C%201%5D%2C%20linestyle%3D%22--%22%2C%20color%3D%22black%22)%0A%20%20%20%20_axes%5B1%5D.set_xlabel(%22false%20positive%20rate%22)%0A%20%20%20%20_axes%5B1%5D.set_ylabel(%22true%20positive%20rate%22)%0A%20%20%20%20_axes%5B1%5D.set_title(%22ROC%22)%0A%20%20%20%20_axes%5B2%5D.hist(train_errors%2C%20bins%3D40%2C%20alpha%3D0.6%2C%20label%3D%22train%20normal%22%2C%20density%3DTrue)%0A%20%20%20%20_axes%5B2%5D.hist(test_errors%5Btest_labels%20%3D%3D%200%5D%2C%20bins%3D40%2C%20alpha%3D0.5%2C%20label%3D%22test%20normal%22%2C%20density%3DTrue)%0A%20%20%20%20_axes%5B2%5D.hist(test_errors%5Btest_labels%20%3D%3D%201%5D%2C%20bins%3D40%2C%20alpha%3D0.5%2C%20label%3D%22test%20anomaly%22%2C%20density%3DTrue)%0A%20%20%20%20_axes%5B2%5D.axvline(threshold%2C%20color%3D%22black%22%2C%20linestyle%3D%22--%22%2C%20label%3D%22threshold%22)%0A%20%20%20%20_axes%5B2%5D.set_xlabel(%22reconstruction%20error%22)%0A%20%20%20%20_axes%5B2%5D.set_title(%22Error%20distribution%22)%0A%20%20%20%20_axes%5B2%5D.legend()%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(confusion)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(4%2C%204))%0A%20%20%20%20_image%20%3D%20_ax.imshow(confusion%2C%20cmap%3D%22Reds%22)%0A%20%20%20%20for%20row%20in%20range(confusion.shape%5B0%5D)%3A%0A%20%20%20%20%20%20%20%20for%20col%20in%20range(confusion.shape%5B1%5D)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.text(col%2C%20row%2C%20confusion%5Brow%2C%20col%5D%2C%20ha%3D%22center%22%2C%20va%3D%22center%22)%0A%20%20%20%20_ax.set_xlabel(%22predicted%22)%0A%20%20%20%20_ax.set_ylabel(%22true%22)%0A%20%20%20%20_ax.set_title(%22Confusion%20matrix%20at%20threshold%22)%0A%20%20%20%20_fig.colorbar(_image%2C%20ax%3D_ax)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
1c73fce60d6f8024ed43ef6402abe332