<jupyter_start><jupyter_text>Weighted Logistic RegressionThis method learns $P(y|x)$ from PU data by considering a class weight.<jupyter_code>import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>Load the dataset<jupyter_code>from data import load_scar, load_sar, load_pg

train, valid, test, c = load_scar()<jupyter_output><empty_output><jupyter_text>Learn a classifier considering a class weight<jupyter_code>train_xs, train_ys, train_ss, train_es = train

class_weight = {
    0: (train_ss == 1).sum() / len(train_ss),
    1: (train_ss == 0).sum() / len(train_ss),
}
from sklearn.linear_model import LogisticRegression

clf = LogisticRegression(class_weight=class_weight, random_state=0).fit(train_xs, train_ss)<jupyter_output><empty_output><jupyter_text>Predict<jupyter_code>test_xs, test_ys, test_ss, test_es = test

test_ys_hat = clf.predict(test_xs)
test_ys_prob = clf.predict_proba(test_xs)[:, 1]<jupyter_output><empty_output><jupyter_text>Evaluate the performance<jupyter_code>from sklearn.metrics import f1_score

f1_score(test_ys, test_ys_hat)<jupyter_output><empty_output><jupyter_text>Visualize the result<jupyter_code>from utils import plot_x_y, plot_x_y_proba
plot_x_y(test_xs, test_ys_hat)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10292953285827396, 'fraction_numerical': 0.006334125098970704, 'mean_word_length': 9.533333333333333, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '647167', 'license': 'MIT', 'n_tokens_mistral': 491, 'n_tokens_neox': 478, 'n_words': 84, 'path': 'notebooks/weighted_logistic_regression.ipynb', 'repo_name': 'hkiyomaru/pu-learning', 'size': '1263'}