# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + colab={"base_uri": "https://localhost:8080/"} id="i4WE1j8_I-_A" executionInfo={"status": "ok", "timestamp": 1635264764911, "user_tz": -420, "elapsed": 722, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}} outputId="5c31b0d3-44d2-4f97-f9e1-20bda0030b2b"
# Mount to Google Drive either 
# to read or write to Google Drive
from google.colab import drive
drive.mount('/content/drive')

# + [markdown] id="hoYnlSnjcn9H"
# # I. Read data and EDA

# + id="vAExi4hjJut0" executionInfo={"status": "ok", "timestamp": 1635264765626, "user_tz": -420, "elapsed": 7, "user": {"displayName": "<NAME> \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}}
# Import the necessary packages
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report

# + colab={"base_uri": "https://localhost:8080/"} id="r3TY-gu7K45f" executionInfo={"status": "ok", "timestamp": 1635264766221, "user_tz": -420, "elapsed": 600, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}} outputId="dda5d94d-ffc0-4306-df44-50a8bab88351"
# Load training documents
print("[INFO] Loading training data ...")
docs_train = fetch_20newsgroups(subset='train', shuffle=True, random_state=42)

# + id="1tEkjgOJRlDd" executionInfo={"status": "ok", "timestamp": 1635264766223, "user_tz": -420, "elapsed": 29, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}}
class_names = docs_train.target_names

# + colab={"base_uri": "https://localhost:8080/"} id="bT0mMEIScTEC" executionInfo={"status": "ok", "timestamp": 1635264766223, "user_tz": -420, "elapsed": 26, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}} outputId="4c12eb70-9619-4508-dabb-6641205204a4"
# Load tesing documents
print("[INFO] Loading testing data ...")
docs_test = fetch_20newsgroups(subset='test', shuffle=True)

# + colab={"base_uri": "https://localhost:8080/"} id="vfTD0EZRM8KL" executionInfo={"status": "ok", "timestamp": 1635264766224, "user_tz": -420, "elapsed": 17, "user": {"displayName": "l\u00e2<NAME>\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}} outputId="f183a1d9-cea5-4a54-b577-94d1a3da9c0d"
print("Number of category: ", len(class_names))
print("Number of train documents: ", len(docs_train.target))
print("Number of test documents: ", len(docs_test.target))
print("Categories: \n", '\n'.join(class_names))

# + id="LOdysacrsPc0" executionInfo={"status": "ok", "timestamp": 1635264766225, "user_tz": -420, "elapsed": 10, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}}
# Distribution of class
_, train_class_dist = np.unique(docs_train.target, return_counts=True)
_, test_class_dist = np.unique(docs_test.target, return_counts=True)

# + colab={"base_uri": "https://localhost:8080/", "height": 618} id="hIJsHQaHxK5o" executionInfo={"status": "ok", "timestamp": 1635264767761, "user_tz": -420, "elapsed": 1545, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}} outputId="f809ca28-d1c7-40d5-c499-102311942cca"
import seaborn as sns

plt.figure()
plt.subplot(121)
sns.barplot(train_class_dist, class_names)

plt.figure()
plt.subplot(122)
sns.barplot(test_class_dist, class_names)

# + colab={"base_uri": "https://localhost:8080/"} id="Q6cqp2JDNHm4" executionInfo={"status": "ok", "timestamp": 1635264767763, "user_tz": -420, "elapsed": 30, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}} outputId="9f759f3e-4300-48c0-f94b-11c6799b061f"
docs_train.data[2].split('\n')[0:5]

# + colab={"base_uri": "https://localhost:8080/"} id="5fHlo-HrSXpU" executionInfo={"status": "ok", "timestamp": 1635264767764, "user_tz": -420, "elapsed": 28, "user": {"displayName": "l\u<NAME>\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}} outputId="432f3ffe-165b-45d2-874d-98229d1dde4f"
docs_train.target

# + [markdown] id="NWfUIp6EVKJb"
# # II. Extracting features from text files

# + [markdown] id="pDmsEdARaWlM"
# # 1. SVM

# + id="82Lqq0xdfJiC" executionInfo={"status": "ok", "timestamp": 1635264767765, "user_tz": -420, "elapsed": 20, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}}
pipeline_list = [('bow', CountVectorizer(stop_words='english')),
                 ('tfidf', TfidfTransformer(sublinear_tf=True, norm='l2')),
                 ('svm', SGDClassifier(loss='hinge', penalty='l2', alpha=1e-3, max_iter=5, random_state=42, tol=None))]

parameters_svm = {'bow__ngram_range': [(1, 1), (1, 2)],
                  'tfidf__use_idf': (True, False),
                  'svm__alpha': (1e-2, 1e-3)}

# + id="LxIto8sCWCdw" executionInfo={"status": "ok", "timestamp": 1635264767766, "user_tz": -420, "elapsed": 20, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}}
# define model's pipeline 
text_clf = Pipeline(pipeline_list)
gs_text_clf = GridSearchCV(text_clf, parameters_svm, n_jobs=-1)

# + colab={"base_uri": "https://localhost:8080/"} id="6ePVA34xaf6x" executionInfo={"status": "ok", "timestamp": 1635265016241, "user_tz": -420, "elapsed": 248494, "user": {"displayName": "l\u00<NAME>\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}} outputId="cf3c03ed-0f80-46cd-f221-3acd34da016b"
print("[INFO] training SVM model ...")
gs_text_clf.fit(docs_train.data, docs_train.target)

# + colab={"base_uri": "https://localhost:8080/"} id="eqHA0RGXoZyy" executionInfo={"status": "ok", "timestamp": 1635265016242, "user_tz": -420, "elapsed": 11, "user": {"displayName": "l\u<NAME>\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}} outputId="29631091-526e-435f-912f-9f112863cb90"
gs_text_clf.best_score_
gs_text_clf.best_params_

# + id="LLFCDNJ0b8cS" executionInfo={"status": "ok", "timestamp": 1635265019323, "user_tz": -420, "elapsed": 3088, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}}
# predict on the test documents
predicted_svm = gs_text_clf.predict(docs_test.data)

# + colab={"base_uri": "https://localhost:8080/"} id="_vjaIwsJbMuU" executionInfo={"status": "ok", "timestamp": 1635265019324, "user_tz": -420, "elapsed": 18, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}} outputId="3835f566-a7f3-4677-d1ee-e7<PASSWORD>"
print("[INFO] evaluating SVM model...")
print(classification_report(predicted_svm, docs_test.target, target_names=class_names))

# + [markdown] id="_hdSxP5daN6X"
# # 2. Random Forest

# + id="TtpbC3bvdFAO" executionInfo={"status": "ok", "timestamp": 1635265019325, "user_tz": -420, "elapsed": 17, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}}
# Thes best parameters for two first model 
pipeline_list = [('bow', CountVectorizer(stop_words='english', ngram_range=(1, 2))),
                 ('tfidf', TfidfTransformer(sublinear_tf=True, norm='l2')),
                 ('rf', RandomForestClassifier(n_estimators=400, max_depth=45, random_state=42))] # Chưa Grid Search tối ưu =))

# + id="surMWl0ZMjZ5" executionInfo={"status": "ok", "timestamp": 1635265019326, "user_tz": -420, "elapsed": 17, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}}
# define model's pipeline 
text_clf = Pipeline(pipeline_list)

# + colab={"base_uri": "https://localhost:8080/"} id="kOER1foiMqe8" executionInfo={"status": "ok", "timestamp": 1635265091056, "user_tz": -420, "elapsed": 71746, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}} outputId="304419ba-a834-4dcd-f5ae-a8a7bd586ae7"
print("[INFO] training Random Forest model ...")
text_clf.fit(docs_train.data, docs_train.target)

# + id="KwHZCHH7Okoi" executionInfo={"status": "ok", "timestamp": 1635265100137, "user_tz": -420, "elapsed": 9086, "user": {"displayName": "l\u00<NAME>\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}}
predicted_rf = text_clf.predict(docs_test.data)

# + colab={"base_uri": "https://localhost:8080/"} id="3TGDT5zCM6-N" executionInfo={"status": "ok", "timestamp": 1635265100140, "user_tz": -420, "elapsed": 16, "user": {"displayName": "l\u00e2m nguy\u1ec5n \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}} outputId="90e07d29-bcdf-4f55-fce6-10b6f9ffde47"
print("[INFO] evaluating Random Forest model...")
print(classification_report(predicted_rf, docs_test.target, target_names=class_names))

# + id="kKTLf1LzOsum" executionInfo={"status": "ok", "timestamp": 1635265100141, "user_tz": -420, "elapsed": 14, "user": {"displayName": "<NAME> \u0111\u00ecnh", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GgveoyIIIA5GiG5cYJYZfzYHaCvdw8TQH6Hix6q=s64", "userId": "00806905388804607111"}}


StarcoderdataJupyterScriptsDedupFiltered