<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amila Viraj on Medium]]></title>
        <description><![CDATA[Stories by Amila Viraj on Medium]]></description>
        <link>https://medium.com/@amilalunuwila?source=rss-9fef35d88eea------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*dXnGV5G_keRxPhu8q-AWEg.png</url>
            <title>Stories by Amila Viraj on Medium</title>
            <link>https://medium.com/@amilalunuwila?source=rss-9fef35d88eea------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 06:56:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amilalunuwila/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How To Build Your Own Chatbot Using Deep Learning]]></title>
            <link>https://medium.com/data-science/how-to-build-your-own-chatbot-using-deep-learning-bb41f970e281?source=rss-9fef35d88eea------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb41f970e281</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Amila Viraj]]></dc:creator>
            <pubDate>Sat, 31 Oct 2020 18:30:13 GMT</pubDate>
            <atom:updated>2020-10-31T18:56:30.719Z</atom:updated>
            <content:encoded><![CDATA[<h4>A comprehensive step-by-step guide to implementing an intelligent chatbot solution</h4><p>If you are interested in developing chatbots, you can find out that there are a lot of powerful bot development frameworks, tools, and platforms that can use to implement intelligent chatbot solutions. How about developing a simple, intelligent chatbot from scratch using deep learning rather than using any bot development framework or any other platform. In this tutorial, you can learn how to develop an end-to-end domain-specific intelligent chatbot solution using deep learning with Keras.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Uw73g_s3MI5RVoMAifeYA.png" /><figcaption>Photo by <a href="https://www.flickr.com/photos/botreetech/">Sam Wood</a> from <a href="https://www.flickr.com/photos/botreetech/45874656231/">flickr</a></figcaption></figure><h3>Concept</h3><p>Before jumping into the coding section, first, we need to understand some design concepts. Since we are going to develop a deep learning based model, we need data to train our model. But we are not going to gather or download any large dataset since this is a simple chatbot. We can just create our own dataset in order to train the model. To create this dataset, we need to understand what are the intents that we are going to train. An “<strong>intent”</strong> is the intention of the user interacting with a chatbot or the intention behind each message that the chatbot receives from a particular user. According to the domain that you are developing a chatbot solution, these intents may vary from one chatbot solution to another. Therefore it is important to understand the right intents for your chatbot with relevance to the domain that you are going to work with.</p><p>Then why it needs to define these intents? That’s a very important point to understand. In order to answer questions, search from domain knowledge base and perform various other tasks to continue conversations with the user, your chatbot really needs to understand what the users say or what they intend to do. That’s why your chatbot needs to understand intents behind the user messages (to identify user’s intention).</p><p>How can you make your chatbot understand intents in order to make users feel like it knows what they want and provide accurate responses. The strategy here is to define different intents and make training samples for those intents and train your chatbot model with those training sample data as model training data (X) and intents as model training categories (Y).</p><h3>Implementation</h3><h4>Required Packages</h4><p>The required python packages are as follows, (here I mentioned the packages with versions that I have used for the developments)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3da1788abb8571ea3350b664edba43ab/href">https://medium.com/media/3da1788abb8571ea3350b664edba43ab/href</a></iframe><h4>Define Intents</h4><p>I will define few simple intents and bunch of messages that corresponds to those intents and also map some responses according to each intent category. I will create a JSON file named “intents.json” including these data as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73ca857744260f924f0695534b092422/href">https://medium.com/media/73ca857744260f924f0695534b092422/href</a></iframe><h4>Data Preparation</h4><p>First we need to import all the required packages</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3464e0ee87cd8916f0e432e9d92a5e63/href">https://medium.com/media/3464e0ee87cd8916f0e432e9d92a5e63/href</a></iframe><p>Now we load the json file and extract the required data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c1255080484d5e1a49f45781afc5e1e/href">https://medium.com/media/1c1255080484d5e1a49f45781afc5e1e/href</a></iframe><p>The variable “<em>training_sentences</em>” holds all the training data (which are the sample messages in each intent category) and the “<em>training_labels</em>” variable holds all the target labels correspond to each training data.</p><p>Then we use “<em>LabelEncoder()</em>” function provided by scikit-learn to convert the target labels into a model understandable form.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cae6171c0c459142d290de3245364d1a/href">https://medium.com/media/cae6171c0c459142d290de3245364d1a/href</a></iframe><p>Next, we vectorize our text data corpus by using the “<em>Tokenizer</em>” class and it allows us to limit our vocabulary size up to some defined number. When we use this class for the text pre-processing task, by default all punctuations will be removed, turning the texts into space-separated sequences of words, and these sequences are then split into lists of tokens. They will then be indexed or vectorized. We can also add “oov_token” which is a value for “out of token” to deal with out of vocabulary words(tokens) at inference time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b83bfb5ab5728511aa3bfd879c8b0e5/href">https://medium.com/media/5b83bfb5ab5728511aa3bfd879c8b0e5/href</a></iframe><p>The “<em>pad_sequences</em>” method is used to make all the training text sequences into the same size.</p><h4>Model Training</h4><p>Let’s define our Neural Network architecture for the proposed model and for that we use the “<em>Sequential</em>” model class of <strong>Keras.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c76f62f171cdb2f9a3bce561633288a4/href">https://medium.com/media/c76f62f171cdb2f9a3bce561633288a4/href</a></iframe><p>Our model architecture looks as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TpeGTIoKmRdJ33tj_aj1Pw.png" /></figure><p>Now we are ready to train our model. Simply we can call the “<em>fit</em>” method with training data and labels.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19a21909f999f4ea45a79b1ca1978cef/href">https://medium.com/media/19a21909f999f4ea45a79b1ca1978cef/href</a></iframe><p>After training, it is better to save all the required files in order to use it at the inference time. So that we save the trained model, fitted tokenizer object and fitted label encoder object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e6cfd9f876d46097ba46ca50f66dd40/href">https://medium.com/media/7e6cfd9f876d46097ba46ca50f66dd40/href</a></iframe><h4>Inference</h4><p>Okay!!!! now it’s time to check how our model performs. 😊</p><p>We are going to implement a chat function to engage with a real user. When a new user message is received, the chatbot will calculate the similarity between the new text sequence and training data. Considering the confidence scores got for each category, it categorizes the user message to an intent with the highest confidence score.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/526d5e2b93f084f5935f7140403829cb/href">https://medium.com/media/526d5e2b93f084f5935f7140403829cb/href</a></iframe><p>You can see that it’s working perfectly!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*Gh2GJmV4a5oda20o8M65JA.gif" /></figure><h4>Integration With Chat Applications</h4><p>Also, you can integrate your trained chatbot model with any other chat application in order to make it more effective to deal with real world users.</p><p>I have already developed an application using flask and integrated this trained chatbot model with that application.</p><p>The final solution looks like this,</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FwwYBfHSipAI%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DwwYBfHSipAI&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FwwYBfHSipAI%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3a1b69235728fbbd76bbe58c8fc576d/href">https://medium.com/media/d3a1b69235728fbbd76bbe58c8fc576d/href</a></iframe><h4>Final Thoughts</h4><p>We discussed how to develop a chatbot model using deep learning from scratch and how we can use it to engage with real users. With these steps, anyone can implement their own chatbot relevant to any domain.</p><p>As further improvements you can try different tasks to enhance performance and features.</p><ul><li><strong>Use more data to train:</strong> You can add more data to the training dataset. A large dataset with a good number of intents can lead to making a powerful chatbot solution.</li><li><strong>Apply different NLP techniques:</strong> You can add more NLP solutions to your chatbot solution like <strong>NER</strong> (Named Entity Recognition) in order to add more features to your chatbot. With having a NER model along with your chatbot, you can easily find out any entity that appeared in user chat messages and use it for further conversations. And also you can add a <strong>Sentiment</strong> <strong>Analysis</strong> model to identify different sentiment tones behind user messages and it will exactly give some additional colors to your chatbot.</li><li><strong>Try different neural network architectures:</strong> You can also try different neural network architectures with different hyperparameters.</li><li><strong>Add emojis:</strong> You can also consider emojis when building your models.</li></ul><p>You can find the source codes for this article from the <a href="https://github.com/amilavm/Chatbot_Keras"><strong>Github</strong></a> repository.</p><p>Hope you enjoyed this article and stay tuned for another interesting article. Also, I’ll be happy to hear your feedback.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb41f970e281" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/how-to-build-your-own-chatbot-using-deep-learning-bb41f970e281">How To Build Your Own Chatbot Using Deep Learning</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multi-Class Text Classification | Practical Guide To Machine Learning]]></title>
            <link>https://medium.com/analytics-vidhya/multi-class-text-classification-practical-guide-to-machine-learning-6002bcadbd51?source=rss-9fef35d88eea------2</link>
            <guid isPermaLink="false">https://medium.com/p/6002bcadbd51</guid>
            <category><![CDATA[multiclass-classification]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[text-processing]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Amila Viraj]]></dc:creator>
            <pubDate>Mon, 11 May 2020 16:41:09 GMT</pubDate>
            <atom:updated>2020-05-13T16:13:19.481Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UTduqxzY8oZjGmQ2ngFMfg.png" /></figure><p>In this article I will discuss how to perform Multi Class Text Classification task in a practical way in Machine Learning. Here we are also discussing different Natural Language Processing (NLP) techniques to perform our data pre-processing tasks in order to make meaningful data to build our Machine Learning Models. You can try step-by-step guide to learn and understand each an every steps here to perform Multi Class Classification for text data.</p><h4><strong>Dataset</strong></h4><p>Here we will use <a href="https://dataturks.com/projects/Mohan/News%20Classification%20DataSet">News Classification Dataset</a> for the tutorial as it is a manually labeled dataset. This dataset contains articles which are categorized into 4 different classes (Business, SciTech, Sports, World). You can use any dataset in any domain which is relevant to your problem requirement.</p><p>You can download the dataset as a json file from the site. Anyway for the easier use,I prefer creating a csv file including only necessary data from the json file. But you can also use the original json dataset file directly without creating any csv file and it’s up to you as you prefer.</p><pre>import pandas as pd<br>import json</pre><pre>data = []</pre><pre>for line in open(&quot;News Classification DataSet.json&quot;, &quot;r&quot;):<br>    data.append(json.loads(line))</pre><pre>content, label = [], []<br>for each in data:<br>    content.append(each[&#39;content&#39;])<br>    label.append(each[&#39;annotation&#39;][&#39;label&#39;][0])</pre><pre>df = pd.DataFrame([content, label]).T<br>df.columns = [&#39;content&#39;, &#39;label&#39;]<br>print(df.head())</pre><pre>&#39;&#39;&#39;create a csv dataset file using json dataset file&#39;&#39;&#39;<br>df.to_csv(&#39;News_Dataset.csv&#39;, index = False)</pre><p>From this moment onwards, I will use this generated csv dataset file.</p><p>You can use Jupyter notebook or any other preferred IDE to execute and test the code along with the tutorial.</p><h4><strong>Exploring the Data</strong></h4><p>First we load all the necessary libraries needed to implement the entire solution.</p><pre>import pandas as pd<br>import numpy as np<br>import matplotlib.pyplot as plt<br>from sklearn.feature_extraction.text import TfidfVectorizer<br>from sklearn.feature_selection import chi2<br>from IPython.display import display<br>from sklearn.model_selection import train_test_split<br>from sklearn.feature_extraction.text import TfidfTransformer<br>from sklearn.naive_bayes import MultinomialNB<br>from sklearn.linear_model import LogisticRegression<br>from sklearn.ensemble import RandomForestClassifier<br>from sklearn.svm import LinearSVC<br>from sklearn.model_selection import cross_val_score<br>from sklearn.metrics import confusion_matrix<br>from sklearn import metrics</pre><p>Let’s load and visualize the dataset.</p><pre># loading data<br>df = pd.read_csv(&quot;News_Dataset.csv&quot;)<br>print(df.shape)<br>df.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/1*E3HmgWOx4-zhUc_4Jmgn8A.png" /></figure><p>There are total of 7600 news data in the dataset and let’s check whether there are any null values in the dataset.</p><pre># Percentage of news with text<br>total = df[&#39;content&#39;].notnull().sum()<br>round((total/len(df)*100),1)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/82/1*zjMayI5x0xDzS2La0wi2cQ.png" /></figure><p>We got the result as 100 and it means that our dataset hasn’t any null value.</p><p>Let’s find out unique labels from the dataset.</p><pre>pd.DataFrame(df.label.unique())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/123/1*bOXy3cYzCh3Z3G97pTwzyA.png" /></figure><p>You can see there are 4 different classes that are; Business, SciTech, Sports &amp; World.</p><pre># The bar chart to show the number of news per category<br>fig = plt.figure(figsize=(8,6))</pre><pre>df.groupby(&#39;label&#39;).content.count().sort_values().plot.barh(<br>    ylim=0, title= &#39;NUMBER OF NEWS DATA IN EACH CATEGORY\n&#39;)<br>plt.xlabel(&#39;Number Of Ocurrences&#39;, fontsize = 10);<br>plt.savefig(&#39;category_graph.png&#39;, bbox_inches=&#39;tight&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*635uxKhvMHyARlrCpOcW_g.png" /></figure><p>It can be observed that each category contains same amount of news data.</p><h4><strong>Text Pre-Processing</strong></h4><p>This is the most important part of the process and you need a clear understanding of what you do and what you needs to do in this section. Before start text pre processing, it is very important to have a clear understanding of the content of your dataset. Without having better insight of your dataset you might ended up with garbage data even after cleaning the dataset.</p><p>Just take a look at content of some data of our dataset.</p><pre>df.content[0]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/1*zg0jlGjguHCY_QCqQR2ocg.png" /></figure><pre>df.content[1]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*OxBrZUJ2QNd3AgxBf5591g.png" /></figure><p>You can see that there are several unwanted and irrelevant characters/symbols/digits in the data and we need to clean the dataset by removing those texts.</p><pre># clean text</pre><pre>import re<br>from nltk.corpus import stopwords</pre><pre>REPLACE_BY_SPACE_RE = re.compile(&#39;[/(){}\[\]\|@,;]&#39;)<br>BAD_SYMBOLS_RE = re.compile(&#39;[^0-9a-z #+_]&#39;)<br>STOPWORDS = set(stopwords.words(&#39;english&#39;))</pre><pre>def clean_text(text):<br>    &quot;&quot;&quot;<br>        text: a string<br>        <br>        return: modified string<br>    &quot;&quot;&quot;<br>    text = str(text).lower() # lowercase text<br>    text = REPLACE_BY_SPACE_RE.sub(&#39; &#39;, text) # replace REPLACE_BY_SPACE_RE symbols by space in text<br>    text = BAD_SYMBOLS_RE.sub(&#39;&#39;, text) # delete symbols which are in BAD_SYMBOLS_RE from text<br>    text = re.sub(&quot; \d+&quot;, &quot; &quot;, text) # remove digits<br>    text = re.sub(&quot; #\d+&quot;, &quot; &quot;, text) # remove digits starting with # symbol<br>    text = &#39; &#39;.join(word for word in text.split() if word not in STOPWORDS) # delete stopwors from text<br>    return text</pre><pre>df[&#39;content&#39;] = df[&#39;content&#39;].apply(clean_text)<br>df.head(10)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*hTXDR1IcUTa0SsL4bzFKsw.png" /></figure><pre>df.content[1]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B06X39bmOQ5y0G2UC8qqvw.png" /></figure><p>We used some standard text cleaning steps such as removing white spaces and numbers, stop words removals, removing symbols &amp; puncuations, convert text to lowercase etc. The text cleaning strategies and steps can be different from dataset to dataset and specific to the content of your dataset. As an example in this dataset; there are numbers starting with # symbols and we used regex to remove those texts and we remove some specific urls with html content. Likewise according to the nature of your dataset you should have a clear understanding of the content of your dataset and depend on that you have to perform specific text processing / cleaning steps.</p><p>Now we need to represent each class as a number, so that our predictive model can better understand the different categories.</p><pre># Create a new column &#39;label_id&#39; with encoded categories <br>df[&#39;label_id&#39;] = df[&#39;label&#39;].factorize()[0]<br>new_df = df[[&#39;label&#39;, &#39;label_id&#39;]].drop_duplicates()</pre><pre># Dictionaries for future use<br>category_to_id = dict(new_df.values)<br>id_to_category = dict(new_df[[&#39;label_id&#39;, &#39;label&#39;]].values)</pre><pre># New dataframe<br>df.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/447/1*8JlSe0dlvOrmRDgb_KaDsg.png" /></figure><h4><strong>Feature Extraction</strong></h4><p>Here we create text features to train and build our classification model.</p><p><em>TfidfVectorizer</em> class can be initialized with the following parameters:</p><ul><li><em>min_df </em>: remove the words from the vocabulary which have occurred in less than ‘min_df’ number of files.</li><li><em>max_df </em>: remove the words from the vocabulary which have occurred in more than ‘max_df’ total number of files in corpus.</li><li><em>sublinear_tf </em>: set to True to scale the term frequency in logarithmic scale.</li><li><em>stop_words</em>: remove the predefined stop words in ‘english’.</li><li><em>use_idf </em>: weight factor must use inverse document frequency.</li><li><em>ngram_range </em>: (1, 2) to indicate that unigrams and bigrams will be considered.</li></ul><pre>tfidf = TfidfVectorizer(sublinear_tf=True, min_df=5,<br>                        ngram_range=(1, 2), <br>                        stop_words=&#39;english&#39;)</pre><pre># We transform each news data into a vector<br>features = tfidf.fit_transform(df.content).toarray()</pre><pre>labels = df.label_id</pre><pre>print(&quot;Each of the %d news data is represented by %d features (TF-IDF score of unigrams and bigrams)&quot; %(features.shape))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*t9q20nxJld8QDcNOE-RKRQ.png" /></figure><p>Next we will find the three most correlated terms with each of news category with the following step.</p><pre>N = 3<br>for Category, category_id in sorted(category_to_id.items()):<br>  features_chi2 = chi2(features, labels == category_id)<br>  indices = np.argsort(features_chi2[0])<br>  feature_names = np.array(tfidf.get_feature_names())[indices]<br>  unigrams = [v for v in feature_names if len(v.split(&#39; &#39;)) == 1]<br>  bigrams = [v for v in feature_names if len(v.split(&#39; &#39;)) == 2]<br>  print(&quot;\n==&gt; &quot;, (Category), &quot;:&quot;)<br>  print(&quot;  * Most Correlated Unigrams are: %s&quot; %(&#39;, &#39;.join(unigrams[-N:])))<br>  print(&quot;  * Most Correlated Bigrams are: %s&quot; %(&#39;, &#39;.join(bigrams[-N:])))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*0En2u-NJ0hGgOMmmv2GbIg.png" /></figure><h4><strong>Split the Dataset into Training &amp; Testing Sets</strong></h4><p>For the data splitting task, we use <em>train_test_split </em>function from sklearn.</p><pre>X = df[&#39;content&#39;] # collection of news data<br>y = df[&#39;label&#39;] # labels(i.e.,for the 4 different news categories)</pre><pre>X_train, X_test, y_train, y_test = train_test_split(X, y, <br>                                                    test_size=0.25,<br>                                                    random_state = 0)</pre><pre>print(&quot;shape of x_train set :&quot;, X_train.shape)<br>print(&quot;shape of y_train set :&quot;,y_train.shape)<br>print(&quot;shape of x_test set :&quot;,X_test.shape)<br>print(&quot;shape of y_test set :&quot;,y_test.shape)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*sy54AILFxJcQ3JaUWX3FWQ.png" /></figure><p>As you can see, we are about to use 5700 data for training and 1900 data for testing.</p><h4><strong>Model Selection</strong></h4><p>Now we have data to train and it’s time to build the model. Then which machine learning algorithm to use? As starting point we can try different standard machine learning classifiers and according to their performance with the dataset, we can select a better model to use as the solution.</p><p>We select the following 4 classifiers for the experiment:</p><ul><li>Random Forest Classifier</li><li>Linear Support Vector Machine</li><li>Multinomial Naive Bayes</li><li>Logistic Regression</li></ul><pre>models = [<br>    RandomForestClassifier(n_estimators=100, max_depth=5, random_state=0),<br>    LinearSVC(),<br>    MultinomialNB(),<br>    LogisticRegression(random_state=0),<br>]</pre><pre># Cross-validation<br>CV = 5<br>cv_df = pd.DataFrame(index=range(CV * len(models)))</pre><pre>entries = []<br>for model in models:<br>    model_name = model.__class__.__name__<br>    accuracies = cross_val_score(model, features, labels, scoring=&#39;accuracy&#39;, cv=CV)<br>    for fold_idx, accuracy in enumerate(accuracies):<br>        entries.append((model_name, fold_idx, accuracy))<br>    <br>cv_df = pd.DataFrame(entries, columns=[&#39;model_name&#39;, &#39;fold_idx&#39;, &#39;accuracy&#39;])</pre><p>We used 5 fold cross-validation to get the performance for each model.</p><pre>mean_accuracy = cv_df.groupby(&#39;model_name&#39;).accuracy.mean()<br>std_accuracy = cv_df.groupby(&#39;model_name&#39;).accuracy.std()</pre><pre>acc = pd.concat([mean_accuracy, std_accuracy], axis= 1, <br>          ignore_index=True)<br>acc.columns = [&#39;Mean Accuracy&#39;, &#39;Standard deviation&#39;]<br>acc</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/401/1*3QkG7fpA0DZUVaN2xbTiuw.png" /></figure><p>According to the stats, we will select Logistic Regression for the final solution.</p><p>Let’s see how to build a Logistic Regression model and evaluate it using standard evaluation techniques.</p><h4><strong>Build Model &amp; Evaluate</strong></h4><pre>X_train, X_test, y_train, y_test,indices_train,indices_test = train_test_split(features, <br>                                                               labels, <br>                                                               df.index, test_size=0.25, <br>                                                               random_state=1)<br>model = LogisticRegression(random_state=0)<br>model.fit(X_train, y_train)<br>y_pred = model.predict(X_test)</pre><pre># Classification report<br>print(&#39;\t\t\tCLASSIFICATIION REPORT\n&#39;)<br>print(metrics.classification_report(y_test, y_pred, <br>                                    target_names= df[&#39;label&#39;].unique()))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*Jq55A0bwjKgePxuoMf41nA.png" /></figure><p>According to the generated classification report, it reached better values for precion, recall and f1-score metrics.</p><pre>print(&quot;Accuracy:&quot;,metrics.accuracy_score(y_test, y_pred))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/226/1*tL3QJzzv19nI_bfeUrDFHg.png" /></figure><p>Also it is clear that we have test accuracy around 86.57%.</p><p>Next we will get the confusion matrix.</p><pre>import seaborn as sns<br>conf_mat = confusion_matrix(y_test, y_pred)<br>fig, ax = plt.subplots(figsize=(8,8))<br>sns.heatmap(conf_mat, annot=True, cmap=&quot;Blues&quot;, fmt=&#39;d&#39;,<br>            xticklabels=new_df.label.values, <br>            yticklabels=new_df.label.values)<br>plt.ylabel(&#39;Actual&#39;)<br>plt.xlabel(&#39;Predicted&#39;)<br>plt.title(&quot;CONFUSION MATRIX - Logistic Regression\n&quot;, size=14)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*dq4vSuSFuiOh1uAeIJCrZA.png" /></figure><p>Finally we can check the misclassified scenarios.</p><pre>for predicted in new_df.label_id:<br>    for actual in new_df.label_id:<br>        if predicted != actual and conf_mat[actual, predicted] &gt;= 20:<br>            print(&quot;actual class &#39;{}&#39; predicted as &#39;{}&#39; : {} examples.&quot;.format(id_to_category[actual], <br>                                                           id_to_category[predicted], <br>                                                           conf_mat[actual, predicted]))<br>    <br>            display(df.loc[indices_test[(y_test == actual) &amp; (y_pred == predicted)]][[&#39;label&#39;, <br>                                                                &#39;content&#39;]])<br>            print(&#39;&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*nk_5SwQuw-JAy0LVUEAlxQ.png" /></figure><p>I only attached only one output scenario as above misclassified issue. But you should be able to see all the scenarios once you run the above step.</p><h4><strong>Save Model</strong></h4><p>If you want to save your built model for further use you can easily save it and load from saved path. And also remember that you need to save <em>fitted vectorizer</em> and <em>category_to_id </em>&amp; <em>id_to_category </em>dictionaries along with the model.</p><pre># save the model<br>model_filename = open(&#39;finalized_model.sav&#39;, &#39;wb&#39;)<br>pickle.dump(model, model_filename)<br>model_filename.close()</pre><pre># save fitted vectorizer to use for the prediction time<br>tf_idf_output = open(&#39;fitted_vectorizer.pickle&#39;, &#39;wb&#39;)<br>pickle.dump(fitted_vectorizer, tf_idf_output)<br>tf_idf_output.close()</pre><pre># save the dictionaries<br>output1 = open(&#39;category_to_id.pkl&#39;, &#39;wb&#39;)<br>pickle.dump(category_to_id, output1)<br>output1.close()</pre><pre>output2 = open(&#39;id_to_category.pkl&#39;, &#39;wb&#39;)<br>pickle.dump(id_to_category, output2)<br>output2.close()</pre><h4><strong>Summary</strong></h4><p>We successfully built a solution for multiclass classification in Machine Learning. You can try this with different datasets and performing some advanced text processing. Also trying different machine learning models/classifiers will enable you to gain better understanding and experience. Here we used tf-idf vectors as word features but you can use word embeddings to use as the features.</p><p>If you are interested in Deep Learning, you can try out this kind of tasks using deep learning as well. You can try using different deep learning frameworks like TensorFlow, Keras, PyTorch etc.</p><p>Hope you enjoyed the article and stay tuned with another interesting article. Also I’ll be happy to hear your feedbacks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6002bcadbd51" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/multi-class-text-classification-practical-guide-to-machine-learning-6002bcadbd51">Multi-Class Text Classification | Practical Guide To Machine Learning</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>