<jupyter_start><jupyter_text>Predict The News Category : Anshul Patel  Import General Libraries<jupyter_code>!pip3 install ktrain
import ktrain
from ktrain import text
import pandas as pd<jupyter_output><empty_output><jupyter_text>Import Training Data<jupyter_code>DATA_PATH = '/content/drive/My Drive/MH/PTNC/Data_Train.csv'
docs = pd.read_csv(DATA_PATH,encoding='cp1252')
docs.head()<jupyter_output><empty_output><jupyter_text>Preprocessing Text Data<jupyter_code>MAXLEN = 500
trn, val, preproc = text.texts_from_df(docs,
                      'Text',
                      label_columns = ['Label'],
                      preprocess_mode = 'distilbert', 
                      maxlen=MAXLEN,
                      ngram_range=1)<jupyter_output><empty_output><jupyter_text>Initialize Model<jupyter_code>model = text.text_classifier('distilbert', trn, preproc=preproc)
learner = ktrain.get_learner(model,train_data=trn, val_data=val, batch_size=6)<jupyter_output>Is Multi-Label? False
maxlen is 500<jupyter_text>Train the Model<jupyter_code>learner.fit_onecycle(2e-5, 3)<jupyter_output>begin training using onecycle policy with max lr of 2e-05...
Epoch 1/3
1145/1145 [==============================] - 261s 228ms/step - loss: 0.3544 - accuracy: 0.9208 - val_loss: 0.0910 - val_accuracy: 0.9712
Epoch 2/3
1145/1145 [==============================] - 261s 228ms/step - loss: 0.0689 - accuracy: 0.9827 - val_loss: 0.0846 - val_accuracy: 0.9725
Epoch 3/3
1145/1145 [==============================] - 261s 228ms/step - loss: 0.0246 - accuracy: 0.9933 - val_loss: 0.0569 - val_accuracy: 0.9830<jupyter_text>Predict on Test Data<jupyter_code>predictor = ktrain.get_predictor(learner.model, preproc)
import pandas as pd
docs = pd.read_csv('/content/drive/My Drive/MH/PTNC/Data_Test.csv',encoding='WINDOWS-1252')
docs.head()
data = docs['TEXT'].to_list()
test_result = predictor.predict(data)
for i in range(len(test_result)):
  test_result[i] = int(test_result[i][-1])
df_T = pd.DataFrame(test_result)
df_T.to_excel(excel_writer = "/content/drive/My Drive/MH/PTNC/DistilBertResult.xlsx")<jupyter_output><empty_output><jupyter_text>RESULT ON PUBLIC DATA : 0.98653<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1582537517053206, 'fraction_numerical': 0.06275579809004093, 'mean_word_length': 4.994550408719346, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '87083', 'license': 'MIT', 'n_tokens_mistral': 910, 'n_tokens_neox': 796, 'n_words': 138, 'path': 'source/Predict The News Category/Notebooks/DistilBERT_PTNC.ipynb', 'repo_name': 'anshulp2912/Machine-hack-Codes', 'size': '2199'}