text
stringlengths
1
93.6k
train_feature = train_feature.toarray()
test_feature = test_feature.toarray()
elif method == "charlength":
train_feature = np.asarray(
[len(s) for s in train["processed_text"].values]
).reshape(-1, 1)
test_feature = np.asarray(
[len(s) for s in test["processed_text"].values]
).reshape(-1, 1)
elif method == "wordlength":
train_feature = np.asarray(
[len(s.split(" ")) for s in train["processed_text"].values]
).reshape(-1, 1)
test_feature = np.asarray(
[len(s.split(" ")) for s in test["processed_text"].values]
).reshape(-1, 1)
return train_feature, test_feature
def parse_arguments():
parser = argparse.ArgumentParser(description="Run classification for memorization.")
parser.add_argument(
"--train_split", type=float, default=0.8, help="Fraction for training"
)
parser.add_argument(
"--method",
type=str,
choices=["tfidf", "bow", "wordlength", "charlength", "combine",],
default="charlength",
help="Vectorization method",
)
parser.add_argument(
"--text_field",
type=str,
choices=[
"quiz",
"names",
"solution",
"solution_text",
"solution_text_format",
"cot_steps",
"cot_repeat_steps",
"statements",
"response",
"all_fields",
"state_quiz",
"state_quiz_resp",
"quiz_resp",
"state_resp",
],
default="quiz",
help="The field to featurize",
)
parser.add_argument(
"--input_file",
type=str,
default="",
help="Path to data jsonl file",
)
parser.add_argument(
"--output_dir", type=str, default="result/", help="Directory to save output CSV"
)
parser.add_argument("--no_balance_label", action="store_true")
return parser.parse_args()
def prepare_cls_data(df, train_split=0.8):
return train_test_split(
df,
test_size=1 - train_split,
stratify=df["label"],
random_state=42,
)
def train_and_evaluate(train_feature, test_feature, train_label, test_label):
model = LogisticRegression(random_state=42,max_iter=10000)
model.fit(train_feature, train_label)
train_pred = model.predict(train_feature)
test_pred = model.predict(test_feature)
# Predict probabilities instead of labels
train_probs = model.predict_proba(train_feature)
test_probs = model.predict_proba(test_feature)
evaluation= {
"train_accuracy": accuracy_score(train_label, train_pred),
"test_accuracy": accuracy_score(test_label, test_pred),
"train_auc": roc_auc_score(train_label, train_probs[:, 1]),
"test_auc":roc_auc_score(test_label, test_probs[:, 1]),
}
report= classification_report(test_label, test_pred,output_dict=True)
evaluation.update(report)