<jupyter_start><jupyter_text>**LIBRARIES**<jupyter_code>import os           # os.path.join
import numpy as np  # np.concatenate
import pandas as pd

import bow
import utils

#pd.get_dummies

from sklearn.preprocessing import LabelBinarizer, MultiLabelBinarizer  # fit_transform<jupyter_output>[nltk_data] Downloading package stopwords to /home/ronkow/nltk_data...
[nltk_data]   Package stopwords is already up-to-date!<jupyter_text>**GLOBAL CONSTANTS, DATA**<jupyter_code>DATA_DIR = "data/"<jupyter_output><empty_output><jupyter_text>**ONE HOT ENCODE THE COMPLETE TRAINING DATASET**<jupyter_code>DATA_FILE_TEXT = os.path.join(DATA_DIR, "train_text.csv")           
DATA_FILE_KEYWORD = os.path.join(DATA_DIR, "train_keyword.csv")
DATA_FILE_TARGET = os.path.join(DATA_DIR, "train_target.csv")
# USE LARGEST SET OF BAG OF WORDS

token_list = utils.csv_to_list_of_strings('data/tokensfinal/train_text_token6.csv')
print(token_list[0:10])
print('')

text_list = utils.csv_to_list_of_strings(DATA_FILE_TEXT)
print(text_list[0:3])
print('')

text_token_list = bow.list_of_token_lists_text(text_list, token_list)
print(text_token_list[0:3])

onehot_multi_text = MultiLabelBinarizer()

onehot_text_nolabel = onehot_multi_text.fit_transform(text_token_list)
num_row, num_col = onehot_text_nolabel.shape

print(onehot_text_nolabel)
print(onehot_text_nolabel.shape)
print('')

text_labels = onehot_multi_text.classes_
text_labels = text_labels.reshape(1, num_col)

print(text_labels)
print(text_labels.shape)
print('')

onehot_text = np.concatenate((text_labels,onehot_text_nolabel), axis=0)
print(onehot_text)
print(onehot_text.shape)
keyword_list = utils.csv_to_list_of_strings(DATA_FILE_KEYWORD)
print(keyword_list[0:10])
print('')

keyword_token_list = bow.list_of_token_lists_keyword(keyword_list)
print(keyword_token_list[0:10])
onehot_multi_keyword = MultiLabelBinarizer()
onehot_keyword_nolabel = onehot_multi_keyword.fit_transform(keyword_token_list)
num_row, num_col = onehot_keyword_nolabel.shape

print(onehot_keyword_nolabel)
print(onehot_keyword_nolabel.shape)
print('')

keyword_labels = onehot_multi_keyword.classes_
keyword_labels = keyword_labels.reshape(1, num_col)

print(keyword_labels)
print(keyword_labels.shape)
print('')

onehot_keyword = np.concatenate((keyword_labels,onehot_keyword_nolabel), axis=0)
print(onehot_keyword)
print(onehot_keyword.shape)
onehot_train = np.concatenate((onehot_keyword,onehot_text), axis=1)
print(onehot_train)
print(onehot_train.shape)
train_df = utils.np_array_to_dataframe(onehot_train)
train_df<jupyter_output><empty_output><jupyter_text>**APPEND TARGET COLUMN TO TRAINING SET**<jupyter_code>train_target = utils.csv_to_dataframe(DATA_FILE_TARGET)
train_target
train_df_with_target = pd.concat([train_df, train_target], axis=1)
train_df_with_target<jupyter_output><empty_output><jupyter_text>**CONVERT DATAFRAME TO CSV FILE**<jupyter_code>DATA_FILE_BOW = os.path.join(DATA_DIR, "bowfinal/train_bow.csv")

utils.dataframe_to_csv(train_df_with_target, DATA_FILE_BOW)<jupyter_output><empty_output><jupyter_text>**SAVE KEYWORD LABELS AS CSV FILE (needed when we one-hot encode the test data)**<jupyter_code>#keyword_labels = keyword_labels.reshape(0, num_col)

keyword_labels = onehot_multi_keyword.classes_
print(keyword_labels.shape)
print(keyword_labels[0:10])

def list_to_csv(list, filepath):
    """
    ARGUMENT: list, file path
    RETURN: csv file
    Convert to dataframe first, then convert dataframe to csv file
    """
    #df = pd.DataFrame(list, columns=["text_tokens"])
    df = pd.DataFrame(list)
    df.to_csv(filepath, index=False)
DATA_FILE_BOW_KEYWORD = os.path.join(DATA_DIR, "bowfinal/train_bow_keyword.csv")

list_to_csv(keyword_labels, DATA_FILE_BOW_KEYWORD)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10649627263045794, 'fraction_numerical': 0.006389776357827476, 'mean_word_length': 8.75844155844156, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '444349', 'license': 'MIT', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1416, 'n_words': 202, 'path': '2_onehot_encode_train_all_hashtags.ipynb', 'repo_name': 'ronkow/kaggle-disaster-tweets', 'size': '3756'}