<jupyter_start><jupyter_text>Load data<jupyter_code>path = f'{conf.raw_lzo_path}/part-00000'
train = read_data(path)
path = f'{conf.raw_lzo_path}/part-00100'
test = read_data(path)
TARGET = 'like'<jupyter_output><empty_output><jupyter_text>- media target encoding(photo, video, gif)- tweet_type- language- tweet_timestamp- engaged_with_user_id- engaged_with_user_follower_count- engaging_user_id- engaging_user_following_count- engagee_follows_engager- 'reply_timestamp', 'retweet_timestamp','retweet_with_comment_timestamp', 'like_timestamp'  Preprocessing<jupyter_code>def set_dataframe_types(df, train):
    df['id']   = np.arange( df.shape[0] )
    df['id']   = df['id'].astype(np.uint32)

    if train:
        df['reply_timestamp']   = df['reply_timestamp'].fillna(0)
        df['retweet_timestamp'] = df['retweet_timestamp'].fillna(0)
        df['comment_timestamp'] = df['comment_timestamp'].fillna(0)
        df['like_timestamp']    = df['like_timestamp'].fillna(0)

        df['reply_timestamp']   = df['reply_timestamp'].astype(np.uint32)
        df['retweet_timestamp'] = df['retweet_timestamp'].astype(np.uint32)
        df['comment_timestamp'] = df['comment_timestamp'].astype(np.uint32)
        df['like_timestamp']    = df['like_timestamp'].astype(np.uint32)

    df['tweet_timestamp']         = df['tweet_timestamp'].astype( np.uint32 )
    df['creator_follower_count']  = df['creator_follower_count'].astype( np.uint32 )
    df['creator_following_count'] = df['creator_following_count'].astype( np.uint32 )
    df['creator_account_creation']= df['creator_account_creation'].astype( np.uint32 )
    df['engager_follower_count']  = df['engager_follower_count'].astype( np.uint32 )
    df['engager_following_count'] = df['engager_following_count'].astype( np.uint32 )
    df['engager_account_creation']= df['engager_account_creation'].astype( np.uint32 )

    return df


def preprocess(df, target, train):
    df = set_dataframe_types(df, train)
    # df = df.set_index('id')
    # df.columns = conf.raw_features + conf.labels
    df = df.drop('text_tokens', axis=1)
    
    df = feature_extraction(df, features=conf.used_features, train=train) # extract 'used_features'
    cols = []

    return df

train = preprocess(train, TARGET, True)

test = preprocess(test, TARGET, True)<jupyter_output><empty_output><jupyter_text>Negative Sampling<jupyter_code>df_positive = train[train['like']==1]
df_negative = train[train['like']==0]
print(len(df_positive))
print(len(df_negative))
df_negative = df_negative.sample(n = len(df_positive), random_state=777)
train = pd.concat([df_positive, df_negative])
train  = train.sample(frac = 1)
train.head()<jupyter_output><empty_output><jupyter_text>Modeling<jupyter_code>train.info()
label_names = ['reply', 'retweet', 'comment']
DONT_USE = ['engager_account_creation','engage_time',
            'engager_account_creation',
            'fold','tweet_id', 
            'tr','dt_day','','',
            'engager_id','engager_is_verified',
            'elapsed_time',
            'links','domains','hashtags0','hashtags1',
            'hashtags','tweet_hash','dt_second','id',
            'tw_hash0',
            'tw_hash1',
            'tw_rt_uhash',
            'same_language', 'nan_language',
            'tw_hash', 'tw_freq_hash','tw_first_word', 'tw_second_word', 'tw_last_word', 'tw_llast_word',
            'ypred','creator_count_combined','creator_user_fer_count_delta_time','creator_user_fing_count_delta_time','creator_user_fering_count_delta_time','creator_user_fing_count_mode','creator_user_fer_count_mode','creator_user_fering_count_mode'
           ]
DONT_USE += label_names
DONT_USE += conf.labels

RMV = [c for c in DONT_USE if c in train.columns]<jupyter_output><empty_output><jupyter_text>Data Split<jupyter_code>#y_train = train[TARGET]
train = train.drop(RMV, axis=1)
train, val = train_test_split(train, test_size=0.2)
test = test.drop(RMV, axis=1)<jupyter_output><empty_output><jupyter_text>Scaling<jupyter_code>columns = ['tweet_timestamp', 'creator_account_creation', 'engager_follower_count', 'engager_following_count', 'creator_follower_count', 'creator_following_count']
train = train.reset_index(drop=True)
test = test.reset_index(drop=True)
val = val.reset_index(drop=True)
standard_scaler = preprocessing.StandardScaler()
standard_scaler.fit(train[columns])
ss = standard_scaler.transform(train[columns])
std_std = pd.DataFrame(ss, columns=columns)
train[columns] = std_std
ss = standard_scaler.transform(val[columns])
std_val = pd.DataFrame(ss, columns = columns)
val[columns] = std_val
ss = standard_scaler.transform(test[columns])
std_test = pd.DataFrame(ss, columns = columns)
test[columns] = std_test
train<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>def get_quantile_based_boundaries(feature_values, num_buckets):
    boundaries = np.arange(1.0, num_buckets) / num_buckets
    quantiles = feature_values.quantile(boundaries)
    return sorted([quantiles[q] for q in quantiles.keys()])
def construct_feature_columns():
    feature_columns = [] # 변형 데이터???
    feature_layer_inputs={} # 원본 input 데이터????
    
    creator_follower_count = tf.feature_column.numeric_column("creator_follower_count")
    creator_account_creation = tf.feature_column.numeric_column("creator_account_creation")
    language = tf.feature_column.numeric_column("language")
    tweet_type = tf.feature_column.numeric_column("tweet_type")
    media = tf.feature_column.numeric_column("media")
    creator_is_verified = tf.feature_column.numeric_column("creator_is_verified")
    creator_follows_engager = tf.feature_column.numeric_column("creator_follows_engager")
    
    for header in ['language', 'creator_account_creation', 'creator_following_count', 'creator_follower_count', 'engager_following_count', 
                   'engager_follower_count', 'media', 'tweet_type', 'creator_is_verified', 'creator_follows_engager', 'dt_dow', 'dt_hour', 'len_domains']:
        feature_columns.append(tf.feature_column.numeric_column(header))
        feature_layer_inputs[header] = tf.keras.Input(shape=(1,), name=header)
    
    # creator_id = tf.feature_column.embedding_column('creator_id', dimension=8)

    bucketized_creator_follower_count = tf.feature_column.bucketized_column(
        creator_follower_count, boundaries=get_quantile_based_boundaries(
            train["creator_follower_count"], 10))
    feature_columns.append(bucketized_creator_follower_count)
    
    bucketized_creator_account_creation = tf.feature_column.bucketized_column(
        creator_account_creation, boundaries=get_quantile_based_boundaries(
            train["creator_account_creation"], 10))
    feature_columns.append(bucketized_creator_account_creation)
    
    creator_follower_x_account = tf.feature_column.crossed_column(
        set([bucketized_creator_follower_count, bucketized_creator_account_creation]), hash_bucket_size=1000)
    creator_follower_x_account = tf.feature_column.indicator_column(creator_follower_x_account)
    feature_columns.append(creator_follower_x_account)
    
    language_x_tweet_type_x_media = tf.feature_column.crossed_column(
        set(["language", "tweet_type", "media"]), hash_bucket_size=10000)
    language_x_tweet_type_x_media = tf.feature_column.indicator_column(language_x_tweet_type_x_media)
    feature_columns.append(language_x_tweet_type_x_media)
    
    language_x_tweet_type = tf.feature_column.crossed_column(
        set(["language", "tweet_type"]), hash_bucket_size=1000)
    language_x_tweet_type = tf.feature_column.indicator_column(language_x_tweet_type)
    feature_columns.append(language_x_tweet_type)
    
    language_x_media = tf.feature_column.crossed_column(
        set(["language", "media"]), hash_bucket_size=1000)
    language_x_media = tf.feature_column.indicator_column(language_x_media)
    feature_columns.append(language_x_media)
    
    language_x_media_x_tweet_type_x_creator_is_verified_x_creator_follows_engager = tf.feature_column.crossed_column(
        set(["language", "media", "tweet_type", "creator_is_verified", 'creator_follows_engager']), hash_bucket_size=10000)
    language_x_media_x_tweet_type_x_creator_is_verified_x_creator_follows_engager = tf.feature_column.indicator_column(language_x_media_x_tweet_type_x_creator_is_verified_x_creator_follows_engager)
    feature_columns.append(language_x_media_x_tweet_type_x_creator_is_verified_x_creator_follows_engager)
    
    
    #feature_layer_inputs['bucketized_creator_account_creation'] = tf.keras.Input(shape=(1,), name='bucketized_creator_account_creation')

    #feature_layer_inputs['creator_follower_x_account'] = tf.keras.Input(shape=(1,), name='creator_follower_x_account')
    
    return feature_columns, feature_layer_inputs
def df_to_dataset(dataframe, shuffle=True, batch_size=32):
    dataframe = dataframe.copy()
    labels = dataframe.pop('like')
    ds = tf.data.Dataset.from_tensor_slices((dict(dataframe), labels))
    if shuffle:
        ds = ds.shuffle(buffer_size=len(dataframe))
    ds = ds.batch(batch_size, drop_remainder=True)
    return ds
ds_train = df_to_dataset(train)
ds_val = df_to_dataset(val, shuffle=False)
ds_test = df_to_dataset(test, shuffle=False)
feature_columns, feature_layer_inputs = construct_feature_columns()
feature_layer = tf.keras.layers.DenseFeatures(feature_columns)
feature_layer_output = feature_layer(feature_layer_inputs)
input = [v for v in feature_layer_inputs.values()]
x = Dense(16, activation="relu")(feature_layer_output)
x = Dense(8, activation="relu")(x)
x = Dense(4, activation="relu")(x)
outputs = Dense(1, activation="sigmoid")(x)
model = tf.keras.Model(inputs=input, outputs=outputs, name="sequential_model")
model.compile(
    optimizer = 'adam',
    loss = 'binary_crossentropy', # softmax : sparse_categorical_crossentropy, sigmoid : binary_crossentropy
    metrics=['binary_crossentropy']) # sigmoid :binary_crossentropy
ds_train
model.summary()
ds_train
history = model.fit(ds_train, epochs=5) # batch_size=32
plt.plot(history.history['binary_crossentropy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
model.evaluate(ds_test)<jupyter_output>94767/94767 [==============================] - 107s 1ms/step - loss: 0.6548 - binary_crossentropy: 0.6548<jupyter_text>Predict<jupyter_code>y = np.concatenate([y for x, y in ds_test], axis=0)
pred = model.predict(ds_test)
pred = pred.astype(np.float64)
rce = compute_rce(pred, y)
rce
average_precision_score(y, pred)
model.summary()
model_path = f'/hdd/cpu_models_DNN/model-ffnn.h5'
            
save_model(model, model_path)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12481245311327832, 'fraction_numerical': 0.011627906976744186, 'mean_word_length': 6.13855421686747, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '654297', 'license': 'Apache-2.0', 'n_tokens_mistral': 3840, 'n_tokens_neox': 3701, 'n_words': 456, 'path': 'models/notebooks/FFNN/03-FFNN_feature_crossing.ipynb', 'repo_name': 'RecKIE7/recsys2021-twitter', 'size': '10664'}