{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.4"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":35887,"databundleVersionId":3659326,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 📓✨ Text Classification Project ✨📓\n\n## 📜 Importing Libraries 📜\n- 🔢 **Numpy** for numerical operations.\n- 🧮 **Pandas** for data manipulation.\n- 🚀 **Resample** from **sklearn.utils** for resampling the dataset.\n- 📝 **TfidfVectorizer** from **sklearn.feature_extraction.text** for transforming text to feature vectors.\n- 🌲 **RandomForestClassifier** from **sklearn.ensemble** for classification.\n- 📈 **LogisticRegression** from **sklearn.linear_model** for classification.\n- 🦸‍♂️ **XGBClassifier** from **xgboost** for classification.\n- 🌿 **PorterStemmer** from **nltk.stem** for stemming words.\n- 🏷️ **LabelEncoder** from **sklearn.preprocessing** for encoding labels.\n- 🧩 **String** for string operations.\n- 🔍 **Re** for regular expressions.\n- 🎲 **Train_test_split** from **sklearn.model_selection** for splitting the dataset.\n- 🏆 **Classification_report**, **accuracy_score**, and **precision_score** from **sklearn.metrics** for evaluating models.\n- 💾 **Joblib** for saving and loading models.\n\n## 🗂️ Data Preparation 🗂️\n- 📥 **Loading the dataset** using Pandas.\n- 🔄 **Handling missing values** and **cleaning the data**.\n- 📊 **Exploring the dataset** to understand its structure and content.\n- 🔍 **Preprocessing text** data by removing punctuation, stop words, and stemming.\n\n## ✨ Feature Extraction ✨\n- 📝 **Transforming text** to feature vectors using **TfidfVectorizer**.\n\n## 📊 Model Training 📊\n- 🔀 **Splitting the dataset** into training and testing sets.\n- 🌲 **Training a Random Forest Classifier**.\n- 📈 **Training a Logistic Regression Classifier**.\n- 🦸‍♂️ **Training an XGBoost Classifier**.\n\n## 🏆 Model Evaluation 🏆\n- 📊 **Evaluating models** using **classification report**, **accuracy score**, and **precision score**.\n- 📉 **Comparing model performance**.\n\n## 💾 Saving and Loading Models 💾\n- 💾 **Saving trained models** using **Joblib**.\n- 📂 **Loading saved models** for future use.\n\n## 🔍 Conclusion 🔍\n- 📜 **Summarizing the findings** and **model performance**.\n- 💡 **Future work** and **improvements** for the project.\n\n","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.utils import resample\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.linear_model import LogisticRegression\nfrom xgboost import XGBClassifier\nfrom nltk.stem import PorterStemmer\nfrom sklearn.preprocessing import LabelEncoder\nimport string as s\nimport re as r\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report,accuracy_score,precision_score\nimport joblib as j","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n# 📜 Data Loading and Exploration 📜\n\n- 📥 **Loading the dataset** from a CSV file named **'Language Detection.csv'**.\n- 📐 **Checking the shape** of the dataset to see the number of rows and columns: **(10337, 2)**.\n- 🌍 **Identifying unique languages** in the dataset using the **'Language'** column.\n- 📊 **Counting the number of instances** for each language in the dataset:\n\n  - 🇬🇧 **English**: 1385\n  - 🇫🇷 **French**: 1014\n  - 🇪🇸 **Spanish**: 819\n  - 🇵🇹 **Portugeese**: 739\n  - 🇮🇹 **Italian**: 698\n  - 🇷🇺 **Russian**: 692\n  - 🇸🇪 **Swedish**: 676\n  - 🇮🇳 **Malayalam**: 594\n  - 🇳🇱 **Dutch**: 546\n  - 🇦🇪 **Arabic**: 536\n  - 🇹🇷 **Turkish**: 474\n  - 🇩🇪 **German**: 470\n  - 🇮🇳 **Tamil**: 469\n  - 🇩🇰 **Danish**: 428\n  - 🇮🇳 **Kannada**: 369\n  - 🇬🇷 **Greek**: 365\n  - 🇮🇳 **Hindi**: 63\n","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv('Language Detection.csv')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(3)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Language'].unique()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Language'].value_counts()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📊 Upsampling the Dataset for Language Balancing 📊\n\n- 📈 **Calculating the maximum count** of instances among all languages: **1385**.\n- 🔄 **Creating an empty DataFrame** `df_upsampled` to store the upsampled data.\n\n- 🔁 **Iterating through each unique language** in the dataset:\n  - 📊 **Subsetting the dataset** for each language.\n  \n  - ⚖️ **Checking if the count of instances** for the current language is less than the maximum count (1385):\n    - 🔀 **Resampling** the data to match the maximum count using `sklearn.utils.resample`.\n    - 📄 **Concatenating** the upsampled data to `df_upsampled`.\n\n  - 🔄 **Concatenating** the original data if the count meets or exceeds 1385.\n\n- 🔄 **Resetting the index** of `df_upsampled` for consistency.\n\n- 📊 **Counting the number of instances** for each language in the upsampled dataset:\n  - 📊 **Displaying the count** of each language.\n  \n- 🔍 **Checking the shape** of the upsampled dataset: **(23545, 2)**.\n\n- 📄 **Displaying a random sample** of 15 rows from `df_upsampled`.\n","metadata":{}},{"cell_type":"code","source":"max_count=df['Language'].value_counts().max()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_count","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_upsampled = pd.DataFrame()\n\nfor language in df['Language'].unique():\n    language_df = df[df['Language'] == language]\n    \n    if not language_df.empty:\n        count = language_df['Language'].value_counts().values[0]\n        \n        if count < 1385:\n            upsampled_df = resample(language_df, \n                                    replace=True, \n                                    n_samples=1385, \n                                    random_state=42)\n            df_upsampled = pd.concat([df_upsampled, upsampled_df])\n        else:\n            df_upsampled = pd.concat([df_upsampled, language_df])\n\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf_upsampled.reset_index(drop=True, inplace=True)\n\ndf_upsampled['Language'].value_counts()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_upsampled.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_upsampled.sample(15)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s.punctuation","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pt=PorterStemmer()\ndef preprocessing(x):\n    pun = s.punctuation  \n    t = []\n    m = x.split()\n    for i in m:\n        \n        word = ''.join(char for char in i if char not in pun)\n        if word:  \n            stemmed_word = pt.stem(word)\n            t.append(stemmed_word)\n    return \" \".join(t)            \n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_upsampled['Text']=df_upsampled['Text'].apply(preprocessing)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_upsampled","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📊 Model Training and Evaluation 📊\n\n### 🌱 Random Forest Classifier 🌱\n\n- 📚 **Splitting the dataset** into training and testing sets using `train_test_split`.\n- 📊 **Vectorizing** the text data using `TfidfVectorizer`.\n- 🌳 **Initializing a Random Forest Classifier** and **fitting** it on the training data.\n- 📊 **Predicting** the labels for the test data and evaluating performance:\n\n  - 🎯 **Accuracy Score:** 0.922\n  - 🎯 **Precision Score:** 0.952\n  \n- 📄 **Printing the Training Data Scores**:\n\n  - 📈 **Accuracy Score:** 0.998\n  - 📈 **Precision Score:** 0.998\n\n### 📈 Logistic Regression 📈\n\n- 🌱 **Initializing a Logistic Regression model** and **fitting** it on the training data.\n- 📊 **Predicting** the labels for the test data and evaluating performance:\n\n  - 🎯 **Accuracy Score:** 0.964\n  - 🎯 **Precision Score:** 0.970\n  \n- 📄 **Printing the Training Data Scores**:\n\n  - 📈 **Accuracy Score:** 0.994\n  - 📈 **Precision Score:** 0.994\n","metadata":{}},{"cell_type":"code","source":"x=df['Text']\ny=df['Language']","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=42,test_size=0.2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vector=TfidfVectorizer()\nx_train_tidf=vector.fit_transform(x_train)\nx_test_tidf=vector.transform(x_test)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf=RandomForestClassifier()\nrf.fit(x_train_tidf,y_train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred=rf.predict(x_test_tidf)\ny_tpre=rf.predict(x_train_tidf)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The Training Data Score....\")\nprint()\nprint(\"accuracy_score: \",accuracy_score(y_test,ypred))\nprint(\"precision_score: \",precision_score(y_test,ypred,average=\"weighted\"))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The Training Data Score....\")\nprint()\nprint(\"accuracy_score: \",accuracy_score(y_train,y_tpre))\nprint(\"precision_score: \",precision_score(y_train,y_tpre,average=\"weighted\"))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr=LogisticRegression()\nlr.fit(x_train_tidf,y_train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=lr.predict(x_test_tidf)\ny_tpre=lr.predict(x_train_tidf)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The Test Data Score....\")\nprint()\nprint(\"accuracy_score: \",accuracy_score(y_test,y_pred))\nprint(\"precision_score: \",precision_score(y_test,y_pred,average=\"weighted\"))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The Training Data Score....\")\nprint()\nprint(\"accuracy_score: \",accuracy_score(y_train,y_tpre))\nprint(\"precision_score: \",precision_score(y_train,y_tpre,average=\"weighted\"))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 📦 Model Serialization and Prediction 📦\n\n### 📥 Saving Models 📥\n\n- 📄 **Saving** the `TfidfVectorizer` object as **\"vectorizer.jbl\"** using `joblib.dump`.\n- 📄 **Saving** the Logistic Regression model as **\"model.pkl\"** using `joblib.dump`.\n\n### 🔄 Loading Models 🔄\n\n- 📄 **Loading** the Logistic Regression model from **\"model.pkl\"** using `joblib.load`.\n- 📄 **Loading** the `TfidfVectorizer` object from **\"vectorizer.jbl\"** using `joblib.load`.\n\n### 🧠 Model Prediction 🧠\n\n- 📝 **Predicting the language** for different texts using the loaded model and vectorizer:\n\n  1. **English Text:**\n     ```python\n     text = \"hello how are you\"\n     tran_text = vector.transform([text])\n     model.predict(tran_text)[0]  # Output: 'English'\n     ```\n\n  2. **Arabic Text:**\n     ```python\n     text = \"مرحباً، كيف حالك؟\"\n     tran_text = vector.transform([text])\n     model.predict(tran_text)[0]  # Output: 'Arabic'\n     ```\n\n  3. **Danish Text:**\n     ```python\n     text = \"Hej, hvordan har du det?\"\n     tran_text = vector.transform([text])\n     model.predict(tran_text)[0]  # Output: 'Danish'\n     ```\n\n  4. **Hindi Text:**\n     ```python\n     text = \"नमस्ते, आप कैसे हैं?\"\n     tran_text = vector.transform([text])\n     model.predict(tran_text)[0]  # Output: 'Hindi'\n     ```\n","metadata":{}},{"cell_type":"code","source":"j.dump(vector,\"vectorizer.jbl\")\nj.dump(lr,\"model.pkl\")\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=j.load(\"model.pkl\")\nvector=j.load(\"vectorizer.jbl\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text=\"hello how are you\"\ntran_text=vector.transform([text])\nmodel.predict(tran_text)[0]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text=\"مرحباً، كيف حالك؟\"\ntran_text=vector.transform([text])\nmodel.predict(tran_text)[0]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntext=\"Hej, hvordan har du det?\"\ntran_text=vector.transform([text])\nmodel.predict(tran_text)[0]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntext=\"नमस्ते, आप कैसे हैं?\"\ntran_text=vector.transform([text])\nmodel.predict(tran_text)[0]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" \ntext=\"Γεια σου, πώς είσαι;\"\ntran_text=vector.transform([text])\nmodel.predict(tran_text)[0]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text='ഹലോ, നിങ്ങൾക്ക് എങ്ങനെയുണ്ട്?'\ntran_text=vector.transform([text])\nmodel.predict(tran_text)[0]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 📝 Conclusion 📝\n\n- 🎯 **Overall Performance:** The model has demonstrated strong performance across multiple languages, achieving high accuracy and precision scores during training and testing.\n\n- 🌟 **Conclusion:** The model is performing very well in accurately predicting languages based on text inputs, showcasing effective use of text vectorization and logistic regression for classification tasks.\n\n- 🙏 **Thank you for visiting this notebook!** 🙏\n","metadata":{}}]}