diff --git a/Machine Learning/SupportTickect_Classification_ML.ipynb b/Machine Learning/Machine Learning/SupportTickect_Classification_ML.ipynb similarity index 84% rename from Machine Learning/SupportTickect_Classification_ML.ipynb rename to Machine Learning/Machine Learning/SupportTickect_Classification_ML.ipynb index 8757b2f..28d2202 100644 --- a/Machine Learning/SupportTickect_Classification_ML.ipynb +++ b/Machine Learning/Machine Learning/SupportTickect_Classification_ML.ipynb @@ -3,8 +3,7 @@ "nbformat_minor": 0, "metadata": { "colab": { - "name": "Copy of TickectClassificationML.ipynb", - "version": "0.3.2", + "name": "Copy of Copy of TickectClassificationML.ipynb", "provenance": [], "collapsed_sections": [] }, @@ -19,14 +18,15 @@ "metadata": { "id": "s34hKA4TfQsg", "colab_type": "code", - "outputId": "0262b84d-1166-4c4e-8f6f-0e91158eb32c", + "outputId": "f6a3e78d-64b8-4c8a-81b3-c0ace1cbc342", "colab": { "base_uri": "https://localhost:8080/", - "height": 52 + "height": 85 } }, "source": [ "import nltk\n", + "nltk.download('punkt')\n", "import re\n", "from gensim import models, corpora\n", "from nltk import word_tokenize\n", @@ -39,13 +39,15 @@ "import re\n", "from sklearn import preprocessing" ], - "execution_count": 2, + "execution_count": 1, "outputs": [ { "output_type": "stream", "text": [ + "[nltk_data] Downloading package punkt to /root/nltk_data...\n", + "[nltk_data] Unzipping tokenizers/punkt.zip.\n", "[nltk_data] Downloading package brown to /root/nltk_data...\n", - "[nltk_data] Package brown is already up-to-date!\n" + "[nltk_data] Unzipping corpora/brown.zip.\n" ], "name": "stdout" } @@ -56,17 +58,17 @@ "metadata": { "id": "9p2jq05Oi418", "colab_type": "code", - "outputId": "469c1428-f0e7-4bb8-93f8-47910cc1138d", + "outputId": "b1f73cd4-9cbe-457c-a050-763d6ce45f59", "colab": { "base_uri": "https://localhost:8080/", - "height": 197 + "height": 204 } }, "source": [ - "datasource=pd.read_csv('upload_your_file')\n", - "datasource.head()\n" + "datasource=pd.read_csv('https://raw.githubusercontent.com/Killer2499/Support-Tickets-Topic-Classification/master/Deep%20Learning/latest_ticket_data.csv')\n", + "datasource.head()" ], - "execution_count": 3, + "execution_count": 2, "outputs": [ { "output_type": "execute_result", @@ -136,7 +138,7 @@ "metadata": { "tags": [] }, - "execution_count": 3 + "execution_count": 2 } ] }, @@ -148,8 +150,8 @@ "colab": {} }, "source": [ - "features='Features Over Here'\n", - "labels='Labels Over Here'\n" + "features=datasource['Description']\n", + "labels=datasource['Category']\n" ], "execution_count": 0, "outputs": [] @@ -159,7 +161,7 @@ "metadata": { "id": "rgBWLrvOlQS8", "colab_type": "code", - "outputId": "6b21068d-cd68-4251-8908-00315858a754", + "outputId": "d331e5b5-842c-494e-e5b2-049b067c182b", "colab": { "base_uri": "https://localhost:8080/", "height": 34 @@ -296,7 +298,7 @@ " \n", "expandContractions(\"I ain't going anywhere\")" ], - "execution_count": 5, + "execution_count": 4, "outputs": [ { "output_type": "execute_result", @@ -308,7 +310,7 @@ "metadata": { "tags": [] }, - "execution_count": 5 + "execution_count": 4 } ] }, @@ -657,7 +659,7 @@ "metadata": { "id": "0xOq8-urlXYh", "colab_type": "code", - "outputId": "ec3827d9-77be-4064-b795-78a93acb5984", + "outputId": "7230e887-0578-4d55-89d7-85bf0718fc6d", "colab": { "base_uri": "https://localhost:8080/", "height": 34 @@ -680,7 +682,7 @@ "x=stemSentence(\"I am loving it\")\n", "print(x)\n" ], - "execution_count": 19, + "execution_count": 8, "outputs": [ { "output_type": "stream", @@ -696,17 +698,17 @@ "metadata": { "id": "KAOd4FKLloy1", "colab_type": "code", - "outputId": "bb84d83a-dde7-44a8-842e-3412d02f6142", + "outputId": "924626ae-ef97-4d2c-f21c-a31911a046c6", "colab": { "base_uri": "https://localhost:8080/", - "height": 70 + "height": 68 } }, "source": [ "import nltk\n", "nltk.download('punkt')\n" ], - "execution_count": 10, + "execution_count": 9, "outputs": [ { "output_type": "stream", @@ -726,7 +728,7 @@ "metadata": { "tags": [] }, - "execution_count": 10 + "execution_count": 9 } ] }, @@ -770,11 +772,47 @@ "cv_vect = CountVectorizer(binary=True,ngram_range=(1,2),stop_words=stop_words)\n", "\n", "cv_vect.fit(features)\n", - "cleaned_data_vector= cv_vect.transform(features)" + "cleaned_data_vector= cv_vect.transform(features)\n", + "labels = labels.factorize()[0]" ], "execution_count": 0, "outputs": [] }, + { + "cell_type": "code", + "metadata": { + "id": "oJzcspVGK1tc", + "colab_type": "code", + "outputId": "94097a6c-ef5c-466f-842f-fb8e7169a22b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 102 + } + }, + "source": [ + "print(labels[:100])\n", + "#print(labels.factorize()[1])\n", + "print(labels.shape)\n", + "y = np.bincount(labels[:600])\n", + "ii = np.nonzero(y)[0]\n", + "for a,b in zip(ii,y[ii]):\n", + " print(a,b)" + ], + "execution_count": 47, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", + " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", + " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", + "(3000,)\n", + "0 600\n" + ], + "name": "stdout" + } + ] + }, { "cell_type": "markdown", "metadata": { @@ -835,7 +873,7 @@ "source": [ "text_classifier_RF= RandomForestClassifier(n_estimators=200, random_state=0) \n", "rf_scores=[]\n", - "cv = KFold(n_splits=5, random_state=42, shuffle=False)\n", + "cv = KFold(n_splits=5, random_state=42,shuffle = True)\n", "for train_index, test_index in cv.split(cleaned_data_vector):\n", " #print(\"Train Index: \", train_index, \"\\n\")\n", " #print(\"Test Index: \", test_index)\n", @@ -852,13 +890,25 @@ "metadata": { "id": "kKjIq5SLsvQH", "colab_type": "code", - "colab": {} + "outputId": "2cac99a5-a879-49e3-c45e-4a24182d5565", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } }, "source": [ "print(rf_scores)" ], - "execution_count": 0, - "outputs": [] + "execution_count": 65, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0.72, 0.6983333333333334, 0.735, 0.725, 0.6883333333333334]\n" + ], + "name": "stdout" + } + ] }, { "cell_type": "markdown", @@ -875,32 +925,33 @@ "metadata": { "id": "YbJO51ETf1gs", "colab_type": "code", + "outputId": "e753ddaa-2194-4a16-bd46-e0313c198a2c", "colab": { "base_uri": "https://localhost:8080/", - "height": 52 - }, - "outputId": "4d7ef75d-33e6-40ba-9b99-0c56d93d0378" + "height": 51 + } }, "source": [ "text_classifier_SVC= LinearSVC(C=1)\n", "svc_scores=[]\n", - "cv = KFold(n_splits=5, random_state=42, shuffle=False)\n", + "cv = KFold(n_splits=5, random_state=42, shuffle=True)\n", "\n", "for train_index, test_index in cv.split(cleaned_data_vector):\n", " #print(\"Train Index: \", train_index, \"\\n\")\n", " #print(\"Test Index: \", test_index)\n", "\n", - " X_train, X_test, y_train, y_test = cleaned_data_vector[train_index], cleaned_data_vector[test_index], labels[train_index], labels[test_index]\n", + " X_train, X_test = cleaned_data_vector[train_index], cleaned_data_vector[test_index]\n", + " y_train, y_test =labels[train_index], labels[test_index]\n", " text_classifier_SVC.fit(X_train, y_train)\n", " svc_scores.append(text_classifier_SVC.score(X_test, y_test))\n", "\n" ], - "execution_count": 14, + "execution_count": 66, "outputs": [ { "output_type": "stream", "text": [ - "/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:929: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.\n", + "/usr/local/lib/python3.6/dist-packages/sklearn/svm/_base.py:947: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.\n", " \"the number of iterations.\", ConvergenceWarning)\n" ], "name": "stderr" @@ -912,14 +963,27 @@ "metadata": { "id": "8tBuw1Wv6AlR", "colab_type": "code", - "colab": {} + "outputId": "b982e769-6c6b-4278-dfd2-1337262495a1", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } }, "source": [ "print(sum(svc_scores) / len(svc_scores))\n", "print (svc_scores)\n" ], - "execution_count": 0, - "outputs": [] + "execution_count": 67, + "outputs": [ + { + "output_type": "stream", + "text": [ + "0.7276666666666667\n", + "[0.7233333333333334, 0.7466666666666667, 0.73, 0.7083333333333334, 0.73]\n" + ], + "name": "stdout" + } + ] }, { "cell_type": "markdown", @@ -936,57 +1000,50 @@ "metadata": { "id": "WPFfKBunoSj9", "colab_type": "code", - "outputId": "35c561e1-b52a-44b4-87c3-23917a914d2e", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 214 - } + "colab": {} }, "source": [ "text_classifier_SVM= SVC()\n", "svm_scores=[]\n", - "cv = KFold(n_splits=5, random_state=42, shuffle=False)\n", + "cv = KFold(n_splits=5, random_state=42, shuffle=True)\n", "\n", "for train_index, test_index in cv.split(cleaned_data_vector):\n", - " #print(\"Train Index: \", train_index, \"\\n\")\n", - " #print(\"Test Index: \", test_index)\n", + " # print(\"Train Index: \", train_index, \"\\n\")\n", + " # print(\"Test Index: \", test_index)\n", "\n", " X_train, X_test, y_train, y_test = cleaned_data_vector[train_index], cleaned_data_vector[test_index], labels[train_index], labels[test_index]\n", " text_classifier_SVM.fit(X_train, y_train)\n", " svm_scores.append(text_classifier_SVM.score(X_test, y_test))\n" ], "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.\n", - " \"avoid this warning.\", FutureWarning)\n", - "/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.\n", - " \"avoid this warning.\", FutureWarning)\n", - "/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.\n", - " \"avoid this warning.\", FutureWarning)\n", - "/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.\n", - " \"avoid this warning.\", FutureWarning)\n", - "/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.\n", - " \"avoid this warning.\", FutureWarning)\n" - ], - "name": "stderr" - } - ] + "outputs": [] }, { "cell_type": "code", "metadata": { "id": "HUo7hbjovlod", "colab_type": "code", - "colab": {} + "outputId": "8e9c2cae-d171-426c-e6e8-80be179597c9", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } }, "source": [ + "print(sum(svm_scores) / len(svm_scores))\n", "print(svm_scores)" ], - "execution_count": 0, - "outputs": [] + "execution_count": 76, + "outputs": [ + { + "output_type": "stream", + "text": [ + "0.6803333333333333\n", + "[0.6866666666666666, 0.6683333333333333, 0.6866666666666666, 0.675, 0.685]\n" + ], + "name": "stdout" + } + ] }, { "cell_type": "markdown", @@ -1009,7 +1066,7 @@ "from sklearn.naive_bayes import GaussianNB \n", "text_classifier_NB = GaussianNB() \n", "nb_scores=[]\n", - "cv = KFold(n_splits=5, random_state=42, shuffle=False)\n", + "cv = KFold(n_splits=5, random_state=42, shuffle=True)\n", "\n", "for train_index, test_index in cv.split(cleaned_data_vector):\n", " #print(\"Train Index: \", train_index, \"\\n\")\n", @@ -1027,13 +1084,27 @@ "metadata": { "id": "ppGIRkjsvYlj", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "c0133a98-fe2a-4605-c83a-8cfc647b5984" }, "source": [ + "print(sum(nb_scores) / len(nb_scores))\n", "print(nb_scores)" ], - "execution_count": 0, - "outputs": [] + "execution_count": 78, + "outputs": [ + { + "output_type": "stream", + "text": [ + "0.6976666666666667\n", + "[0.7133333333333334, 0.7133333333333334, 0.71, 0.6733333333333333, 0.6783333333333333]\n" + ], + "name": "stdout" + } + ] }, { "cell_type": "code", @@ -1046,7 +1117,7 @@ "from sklearn.naive_bayes import MultinomialNB\n", "text_classifier_MNB = MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True) \n", "mnb_scores=[]\n", - "cv = KFold(n_splits=5, random_state=42, shuffle=False)\n", + "cv = KFold(n_splits=5, random_state=42, shuffle=True)\n", "\n", "for train_index, test_index in cv.split(cleaned_data_vector):\n", " #print(\"Train Index: \", train_index, \"\\n\")\n", @@ -1064,13 +1135,27 @@ "metadata": { "id": "bRuFHslD1hCl", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "6f9d7796-801f-40ca-ead4-4367bb205ae0" }, "source": [ + "print(sum(mnb_scores)/len(mnb_scores))\n", "print(mnb_scores)" ], - "execution_count": 0, - "outputs": [] + "execution_count": 80, + "outputs": [ + { + "output_type": "stream", + "text": [ + "0.7443333333333333\n", + "[0.7533333333333333, 0.7466666666666667, 0.7683333333333333, 0.7066666666666667, 0.7466666666666667]\n" + ], + "name": "stdout" + } + ] }, { "cell_type": "markdown", @@ -1087,16 +1172,12 @@ "metadata": { "id": "S7lkFdJPorrY", "colab_type": "code", - "outputId": "ab8a67c9-f70b-4520-d4b6-bdbe8a4cb039", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 194 - } + "colab": {} }, "source": [ "text_classifier_LR= LogisticRegression(solver='lbfgs', multi_class='multinomial',random_state=1)\n", "lr_scores=[]\n", - "cv = KFold(n_splits=5, random_state=42, shuffle=False)\n", + "cv = KFold(n_splits=5, random_state=42, shuffle=True)\n", "\n", "for train_index, test_index in cv.split(cleaned_data_vector):\n", " #print(\"Train Index: \", train_index, \"\\n\")\n", @@ -1107,37 +1188,34 @@ " lr_scores.append(text_classifier_LR.score(X_test, y_test))" ], "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.\n", - " \"of iterations.\", ConvergenceWarning)\n", - "/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.\n", - " \"of iterations.\", ConvergenceWarning)\n", - "/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.\n", - " \"of iterations.\", ConvergenceWarning)\n", - "/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.\n", - " \"of iterations.\", ConvergenceWarning)\n", - "/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.\n", - " \"of iterations.\", ConvergenceWarning)\n" - ], - "name": "stderr" - } - ] + "outputs": [] }, { "cell_type": "code", "metadata": { "id": "WkUcvE6mt4ZS", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "cfc8ec65-404b-4b14-9329-5bc50054ad48" }, "source": [ + "print(sum(lr_scores)/len(lr_scores))\n", "print(lr_scores)" ], - "execution_count": 0, - "outputs": [] + "execution_count": 81, + "outputs": [ + { + "output_type": "stream", + "text": [ + "0.7473333333333334\n", + "[0.7483333333333333, 0.755, 0.7616666666666667, 0.7183333333333334, 0.7533333333333333]\n" + ], + "name": "stdout" + } + ] }, { "cell_type": "code", @@ -1171,13 +1249,30 @@ "metadata": { "id": "U2NghXuXris7", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "abf54acb-c31f-4f08-981c-32ebcf82756f" }, "source": [ "text_classifier_LR.predict(cleaned_input_vector)" ], - "execution_count": 0, - "outputs": [] + "execution_count": 63, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([0, 0])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 63 + } + ] }, { "cell_type": "code", @@ -1219,4 +1314,4 @@ "outputs": [] } ] -} \ No newline at end of file +}