<jupyter_start><jupyter_text>**New Data Testing**<jupyter_code># Load saved model
restored_model = tf.keras.models.load_model('keras_pred_subreddit_model_v5.h5')
restored_model.summary()
loss, acc = restored_model.evaluate(X_test,  y_test, verbose=2)
print('Model Accuracy: {:5.2f}%'.format(100*acc))
with open('keras_tokenizer_v5.pickle', 'rb') as handle:
    tokenizer = pickle.load(handle)
with open('sklearn_label_encoder_v5.pickle', 'rb') as handle:
    encoder = pickle.load(handle)
test_post_1 = '''My cat likes to zone out when I am holding her head in my palm. 
She fell asleep on my chest while I was writing my term paper and I had to take 
a break to support her coconut head properly as she was about to slide down on 
my lap. The break was extended to 20 more minutes lol'''
test_post_2 = '''Was in Vegas this weekend and hit up Speed Vegas. My plan 
originally was to drive the C8 but this time I could afford the Ferrari so I 
went with that and I don't regret my decision at all. Driving a Ferrari was a 
childhood dream come true and its something I'll never forget as long as I live.
The car was super easy to drive and get used to. After the first lap I felt right 
at home with how it drove. Hitting those turns at 55-60mph without having to 
brake or put the gas was amazing. This car handled those turns like a champ and 
I was shocked at how good it was at taking those turns. On the straight-away I 
hit 142mph and it got up to that speed super quick. The sound of the engine right 
behind your head screaming as your floor it is something that you gotta hear.'''
def predict_on_new(input, tokenizer=tokenizer, restored_model=restored_model, 
                   maxlen=maxlen, encoder=encoder):
  seq = tokenizer.texts_to_sequences([input])
  pad_seq = sequence.pad_sequences(seq, maxlen=maxlen)
  pred = restored_model.predict(pad_seq)
  class_names = list(encoder.inverse_transform(pred[0].argsort()[-5:][::-1]))

  t1_category = []
  t2_category = []

  pred_key = ['pred_1', 'pred_2', 'pred_3', 'pred_4', 'pred_5']

  for c in class_names:
    for i, sub_red in enumerate(subreddit_info['subreddit']):
      if sub_red == c:
        t1_cat = subreddit_info['category_1'].iloc[i]
        t1_category.append(t1_cat)
        t2_cat = subreddit_info['category_2'].iloc[i]
        t2_category.append(t2_cat)

  pred_dict = OrderedDict(zip(pred_key, zip(class_names, zip(t1_category, t2_category))))
  return json.dumps(pred_dict)
test_1 = predict_on_new(test_post_1)
test_1
test_2 = predict_on_new(test_post_2)
test_2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08907040062232595, 'fraction_numerical': 0.01866977829638273, 'mean_word_length': 4.531182795698925, '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': '263691', 'license': 'MIT', 'n_tokens_mistral': 875, 'n_tokens_neox': 841, 'n_words': 302, 'path': 'code/Subreddit_Predictor.ipynb', 'repo_name': 'kevinliu121/ds', 'size': '2571'}