在 Inteval Importance 上调用 fit 时出现 NotFittedError

问题描述 投票:0回答:0

所以在这段代码中,我训练了一个 lstm 分类器,一切正常,它进行预测,有权重。 但是当我去拟合 IntervalImportance 时,它会根据分类器给我特征的重要性分数,它会弹出 nonfittederror 并突出显示代码的最后一行

NotFittedError:此功能实例尚未安装。在使用此估算器之前,使用适当的参数调用“fit”。

classifier = LSTMFCNClassifier(
    n_timesteps_padded, n_features, n_output=2, n_LSTM_cells=8
)

optimizer = keras.optimizers.Adam(lr=0.0001)
classifier.compile(
    optimizer=optimizer, loss="binary_crossentropy", metrics=["accuracy"]
)

>! Define the early stopping criteria
early_stopping_accuracy = keras.callbacks.EarlyStopping(
    monitor="val_accuracy", patience=30, restore_best_weights=True
)
>!  Train the model
reset_seeds()
logger.info("Training log for LSTM-FCN classifier:")
classifier_history = classifier.fit(
    X_train_processed_padded,
    y_train,
    epochs=150,
    batch_size=32,
    shuffle=True,
    verbose=True,
    validation_data=(X_test_processed_padded, y_test),
    callbacks=[early_stopping_accuracy],
)

y_pred = classifier.predict(X_test_processed_padded)
y_pred_classes = np.argmax(y_pred, axis=1)
acc = balanced_accuracy_score(y_true=y_test_classes, y_pred=y_pred_classes)
print(f"LSTM-FCN classifier trained, with validation accuracy {acc}.")

confusion_matrix_df = pd.DataFrame(
    confusion_matrix(y_true=y_test_classes, y_pred=y_pred_classes, labels=[1, 0]),
    index=["True:pos", "True:neg"],
    columns=["Pred:pos", "Pred:neg"],
)
print(f"Confusion matrix: \n{confusion_matrix_df}.")


from wildboar.explain import IntervalImportance
i = IntervalImportance(scoring="accuracy",n_intervals=10, random_state=39)
i.fit(classifier, X_train_processed_padded, y_pred_classes)
machine-learning lstm
© www.soinside.com 2019 - 2024. All rights reserved.