<jupyter_start><jupyter_text>Test with Multiple Labels<jupyter_code>tweet_pairs = []
y_data = []

indexer = {c:i for i, c in enumerate(list(category_to_label.keys()))}
indexer_inv = {i:c for c,i in indexer.items()}
for tweet_id, categories in category_df.groupby("tweet_id"):
    
    tup = (
        tweet_id_map[np.int64(tweet_id)], 
        [indexer[category] for category in categories["category"] if category != 'Unknown']
    )
    tweet_pairs.append(tup)

tweet_texts = [tp[0]["text"] for tp in tweet_pairs]
y_data_ = [tp[1] for tp in tweet_pairs]

def one_hot_y(y_list):
    encoded = [0] * len(indexer)
    for y in y_list:
        encoded[y] = 1
    return encoded

y_data = np.array([one_hot_y(y) for y in y_data_])
tfidf = vectorizer.transform(tweet_texts).toarray()
vocab = {v:i for i, v in enumerate(vectorizer.get_feature_names())}
idf_vals = vectorizer.idf_
idf_dict = {i:idf_vals[i] for i in vocab.values()} #keys are indices; values are IDF scores
other_ftr_data = np.array([other_features(tweet) for tweet, _ in tweet_pairs])
other_ftr_data.shape
sources_ftrs = [tweet_sources_index.get(tweet["source"], 0) for tweet, _ in tweet_pairs]
source_ftr_onehot = []
for ftr in sources_ftrs:
    f = np.zeros(len(tweet_sources_index) + 1)
    f[ftr] = 1.0
    source_ftr_onehot.append(f)
X_test_sources = np.array(source_ftr_onehot)
X_data = np.concatenate([
    tfidf, 
    other_ftr_data, 
    X_test_sources,
], axis=1)

ftr_names_ = np.concatenate([
    np.array([x for x in vocab]), 
    other_features_names, 
    ["Unknown"] + list(tweet_sources_index.keys()),
])

print(X_data.shape, y_data.shape)
skf = KFold(n_splits=10, random_state=r_state)
for train, test in skf.split(X_data, y_data):

    X_train = X_data[train]    
    X_test = X_data[test]
    
    y_train = y_data[train]
    y_test = y_data[test]


    # train
    fitted_model = sklearn.multiclass.OneVsRestClassifier(
#         RandomForestClassifier(**rf_params)
        BernoulliNB(**nb_params)
    )
    fitted_model.fit(X_train, y_train)

    # Compute Precision-Recall 
    y_infer_local = fitted_model.predict(X_test)
    local_f1 = f1_score(y_test, y_infer_local, average="weighted")
    local_score = fitted_model.score(X_test, y_test)
    print("\tAccuracy:", local_score)
    print("\tF1:", local_f1)
    
    f1_accum.append(local_f1)
    accuracy_accum.append(local_score)

print("Accuracy:", np.mean(accuracy_accum))
print("F1:", np.mean(f1_accum))
prs = fitted_model.predict(X_test)
prs
for i, x in enumerate(prs.sum(axis=0)):
    print(i, indexer_inv[i], x)
clf = sklearn.multiclass.OneVsRestClassifier(
#         RandomForestClassifier(**rf_params)
    BernoulliNB(**nb_params)
)
clf.fit(X_data, y_data)
y_test_labels = clf.predict(X_test_data)

all_rows = []
for tid, row_labels in zip([t["id"] for t in test_tweets], y_test_labels):
    
    row = [tid] + row_labels.tolist()
    all_rows.append(row)


id_to_cat_map = {y:x for x,y in category_to_label.items()}

df = pd.DataFrame(all_rows, columns=["tweet_id"] + [indexer_inv[i] for i in range(len(indexer_inv))])

df.to_csv("trec2019b_test_results_run_baseline_multi.csv", index=None)
df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1309148264984227, 'fraction_numerical': 0.008201892744479496, 'mean_word_length': 4.534031413612565, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '170924', 'license': 'MIT', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1269, 'n_words': 230, 'path': '01_Baseline.ipynb', 'repo_name': 'cbuntain/trecis', 'size': '3170'}