{"id":3455,"date":"2016-11-17T06:00:43","date_gmt":"2016-11-17T11:00:43","guid":{"rendered":"http:\/\/fgiasson.com\/blog\/?p=3455"},"modified":"2016-11-17T13:38:09","modified_gmt":"2016-11-17T18:38:09","slug":"dynamic-machine-learning-using-the-kbpedia-knowledge-graph-part-1","status":"publish","type":"post","link":"https:\/\/fgiasson.com\/blog\/index.php\/2016\/11\/17\/dynamic-machine-learning-using-the-kbpedia-knowledge-graph-part-1\/","title":{"rendered":"Dynamic Machine Learning Using the KBpedia Knowledge Graph &#8211; Part 1"},"content":{"rendered":"<p>In my previous blog post, <a href=\"https:\/\/fgiasson.com\/blog\/index.php\/2016\/10\/24\/create-a-domain-text-classifier-using-cognonto\/\">Create a Domain Text Classifier Using Cognonto<\/a>, I explained how one can use the KBpedia Knowledge Graph to automatically create positive and negative training corpuses for different machine learning tasks. I explained how SVM classifiers could be trained and used to check if an input text belongs to the defined domain or not.<\/p>\n<p>This article is the first of two articles.In first part I will extend on this idea to explain how the KBpedia Knowledge Graph can be used, along with other machine learning techniques, to cope with different situations and use cases. I will cover the concepts of feature selection, hyperparameter optimization, and ensemble learning (in part 2 of this series). The emphasis here is on the testing and refining of machine learners, versus the set up and configuration times that dominate other approaches.<\/p>\n<p>Depending on the domain of interest, and depending on the required <code>precision<\/code> or <code>recall<\/code>, different strategies and techniques can lead to better predictions. More often than not, multiple different training corpuses, learners and hyperparameters need to be tested before ending up with the initial best possible prediction model. This is why I will strongly emphasize the fact that the KBpedia Knowledge Graph and <a href=\"http:\/\/cognonto.com\">Cognonto<\/a> can be used to automate fully the creation of a wide range of different training corpuses, to create models, to optimize their hyperparameters, and to evaluate those models.<\/p>\n<p>[extoc]<\/p>\n<p><!--more--><\/p>\n<div id=\"outline-container-org29ff4da\" class=\"outline-2\">\n<h2 id=\"org29ff4da\">New Knowledge Graph and Reasoning<\/h2>\n<div id=\"text-org29ff4da\" class=\"outline-text-2\">\n<p>For this article, I will use the latest version of the KBpedia Knowledge Graph <code>version 1.10<\/code> that we <a href=\"http:\/\/cognonto.com\/resources\/news\/kbpedia-knowledge-graph-version-1.10-released\/\">just released<\/a>. A knowledge graph such as KBpedia is not static. It constantly evolves, gets fixed, and improves. New concepts are created, deprecated concepts are removed, new linkage to external data sources are created, etc. This growth means that any of these changes can have a [positive] impact on the creation of the positive and negative training sets. Applications based on KBpedia should be tested against any new knowledge graph that is released to see if its models will improve. Better concepts, better structure, and more linkages will often lead to better training sets as well.<\/p>\n<p>Such growth in KBpedia is also why automating, and more importantly testing, this process is crucial. Upon the release of major new versions we are able to automate all of these steps to see the final impacts of upgrading the knowledge graph:<\/p>\n<ol class=\"org-ol\">\n<li>Aggregate all the reference concepts that scope the specified domain (by inference)<\/li>\n<li>Create the positive and negative training corpuses<\/li>\n<li>Prune the training corpuses<\/li>\n<li>Configure the classifier (in this case, create the semantic vectors for ESA)<\/li>\n<li>Train the model (in this case, the SVM model)<\/li>\n<li>Optimize the hyperparameters of the algorithm (in this case, the linear SVM hyperparameters), and<\/li>\n<li>Evaluate the model on multiple gold standards.<\/li>\n<\/ol>\n<p>Because each of these steps belongs to an automated workflow, we can easily check the impact of updating the KBpedia Knowledge Graph on our models.<\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-orgc52b579\" class=\"outline-2\">\n<h2 id=\"orgc52b579\">Reasoning Over The Knowledge Graph<\/h2>\n<div id=\"text-orgc52b579\" class=\"outline-text-2\">\n<p>A new step I am adding in this current use case is to use a reasoner to reason over the KBpedia Knowledge Graph. The reasoner is used when we define the scope of the domain to classify. We will browse the knowledge graph to see which <i>seed<\/i> reference concepts we should add to the scope. Then we will use a reasoner to extend the models to include any new sub-classes relevant to the scope of the domain. This means that we may add further specific features to the final model.<\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-org4ede2bc\" class=\"outline-2\">\n<h2 id=\"org4ede2bc\">Update Domain Training Corpus Using KBpedia 1.10 and a Reasoner<\/h2>\n<div id=\"text-org4ede2bc\" class=\"outline-text-2\">\n<p>Recall our prior <a href=\"https:\/\/fgiasson.com\/blog\/index.php\/2016\/10\/24\/create-a-domain-text-classifier-using-cognonto\/\">use case<\/a> used <i>Music<\/i> as its domain scope. The first step is to use the new KBpedia version <code>1.10<\/code> along with a reasoner to create the full scope of this updated Music domain.<\/p>\n<p>The result of using this new version and a reasoner is that we now end up with <code>196<\/code> features (reference documents) instead of <code>64<\/code>. This also means that we will have 196 documents in our positive training set if we only use the Wikipedia pages linked to these reference concepts (and not their related named entities).<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span>use '<span style=\"color: #66d9ef;\">cognonto-esa.core<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<span style=\"color: #ae81ff;\">(<\/span>require '<span style=\"color: #66d9ef;\">[<\/span><span style=\"color: #66d9ef;\">cognonto-owl.core<\/span> <span style=\"color: #ae81ff;\">:as<\/span> owl<span style=\"color: #66d9ef;\">]<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<span style=\"color: #ae81ff;\">(<\/span>require '<span style=\"color: #66d9ef;\">[<\/span><span style=\"color: #66d9ef;\">cognonto-owl.reasoner<\/span> <span style=\"color: #ae81ff;\">:as<\/span> reasoner<span style=\"color: #66d9ef;\">]<\/span><span style=\"color: #ae81ff;\">)<\/span>\n\n<span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #f92672;\">def<\/span> <span style=\"color: #fd971f;\">kbpedia-manager<\/span> <span style=\"color: #66d9ef;\">(<\/span><span style=\"color: #66d9ef;\">owl<\/span><span style=\"color: #66d9ef;\">\/<\/span>make-ontology-manager<span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #f92672;\">def<\/span> <span style=\"color: #fd971f;\">kbpedia<\/span> <span style=\"color: #66d9ef;\">(<\/span><span style=\"color: #66d9ef;\">owl<\/span><span style=\"color: #66d9ef;\">\/<\/span>load-ontology <span style=\"color: #e6db74;\">\"resources\/kbpedia_reference_concepts_linkage.n3\"<\/span>\n                                <span style=\"color: #ae81ff;\">:manager<\/span> kbpedia-manager<span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #f92672;\">def<\/span> <span style=\"color: #fd971f;\">kbpedia-reasoner<\/span> <span style=\"color: #66d9ef;\">(<\/span><span style=\"color: #66d9ef;\">reasoner<\/span><span style=\"color: #66d9ef;\">\/<\/span>make-reasoner kbpedia<span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span>\n\n<span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #f92672;\">define-domain-corpus<\/span> <span style=\"color: #66d9ef;\">[<\/span><span style=\"color: #e6db74;\">\"http:\/\/kbpedia.org\/kko\/rc\/Music\"<\/span>\n                       <span style=\"color: #e6db74;\">\"http:\/\/kbpedia.org\/kko\/rc\/Musician\"<\/span>\n                       <span style=\"color: #e6db74;\">\"http:\/\/kbpedia.org\/kko\/rc\/MusicPerformanceOrganization\"<\/span>\n                       <span style=\"color: #e6db74;\">\"http:\/\/kbpedia.org\/kko\/rc\/MusicalInstrument\"<\/span>\n                       <span style=\"color: #e6db74;\">\"http:\/\/kbpedia.org\/kko\/rc\/Album-CW\"<\/span>\n                       <span style=\"color: #e6db74;\">\"http:\/\/kbpedia.org\/kko\/rc\/Album-IBO\"<\/span>\n                       <span style=\"color: #e6db74;\">\"http:\/\/kbpedia.org\/kko\/rc\/MusicalComposition\"<\/span>\n                       <span style=\"color: #e6db74;\">\"http:\/\/kbpedia.org\/kko\/rc\/MusicalText\"<\/span>\n                       <span style=\"color: #e6db74;\">\"http:\/\/kbpedia.org\/kko\/rc\/PropositionalConceptualWork-MusicalGenre\"<\/span>\n                       <span style=\"color: #e6db74;\">\"http:\/\/kbpedia.org\/kko\/rc\/MusicalPerformer\"<\/span><span style=\"color: #66d9ef;\">]<\/span>\n  kbpedia\n  <span style=\"color: #e6db74;\">\"resources\/domain-corpus-dictionary.csv\"<\/span>\n  <span style=\"color: #ae81ff;\">:reasoner<\/span> kbpedia-reasoner<span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"outline-container-orgd30686b\" class=\"outline-2\">\n<h2 id=\"orgd30686b\">Create Training Corpuses<\/h2>\n<div id=\"text-orgd30686b\" class=\"outline-text-2\">\n<p>The next step is to create the actual training corpuses: the general and domain ones. We have to load the dictionaries we created in the previous step, and then to locally cache and normalize the corpuses. Remember that the normalization steps are:<\/p>\n<ol class=\"org-ol\">\n<li>Defluff the raw HTML page. We convert the HTML into text, and we only keep the body of the page<\/li>\n<li>Normalize the text with the following rules:\n<ol class=\"org-ol\">\n<li>remove diacritics characters<\/li>\n<li>remove everything between brackets like: [edit] [show]<\/li>\n<li>remove punctuation<\/li>\n<li>remove all numbers<\/li>\n<li>remove all invisible control characters<\/li>\n<li>remove all [math] symbols<\/li>\n<li>remove all words with 2 characters or fewer<\/li>\n<li>remove line and paragraph seperators<\/li>\n<li>remove anything that is not an alpha character<\/li>\n<li>normalize spaces<\/li>\n<li>put everything in lower case, and<\/li>\n<li>remove stop words.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span>load-dictionaries <span style=\"color: #e6db74;\">\"resources\/general-corpus-dictionary.csv\"<\/span> <span style=\"color: #e6db74;\">\"resources\/domain-corpus-dictionary.csv\"<\/span><span style=\"color: #ae81ff;\">)<\/span>\n\n<span style=\"color: #ae81ff;\">(<\/span>cache-corpus<span style=\"color: #ae81ff;\">)<\/span>\n\n<span style=\"color: #ae81ff;\">(<\/span>normalize-cached-corpus <span style=\"color: #e6db74;\">\"resources\/corpus\/\"<\/span> <span style=\"color: #e6db74;\">\"resources\/corpus-normalized\/\"<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"outline-container-org68f1358\" class=\"outline-2\">\n<h2 id=\"org68f1358\">Create New Gold Standard<\/h2>\n<div id=\"text-org68f1358\" class=\"outline-text-2\">\n<p>Because we never have enough instances in our gold standards to test against, let&#8217;s create a third one, but this time adding a music related news feed that will add more positive examples to the gold standard.<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #f92672;\">defn<\/span> <span style=\"color: #a6e22e;\">create-gold-standard-from-feeds<\/span>\n  <span style=\"color: #66d9ef;\">[<\/span>name<span style=\"color: #66d9ef;\">]<\/span>\n  <span style=\"color: #66d9ef;\">(<\/span><span style=\"color: #f92672;\">let<\/span> <span style=\"color: #a6e22e;\">[<\/span>feeds <span style=\"color: #e6db74;\">[<\/span><span style=\"color: #e6db74;\">\"http:\/\/www.music-news.com\/rss\/UK\/news\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/topstories.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/world.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/canada.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/politics.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/business.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/health.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/arts.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/technology.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/offbeat.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/www.cbc.ca\/cmlink\/rss-cbcaboriginal\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/sports.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/canada-britishcolumbia.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/canada-calgary.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/canada-montreal.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/canada-pei.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/canada-ottawa.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/canada-toronto.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/canada-north.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/rss.cbc.ca\/lineup\/canada-manitoba.xml\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/news\/artsculture\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/reuters\/businessNews\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/reuters\/entertainment\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/reuters\/companyNews\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/reuters\/lifestyle\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/reuters\/healthNews\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/reuters\/MostRead\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/reuters\/peopleNews\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/reuters\/scienceNews\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/reuters\/technologyNews\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/Reuters\/domesticNews\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/Reuters\/worldNews\"<\/span>\n               <span style=\"color: #e6db74;\">\"http:\/\/feeds.reuters.com\/reuters\/USmediaDiversifiedNews\"<\/span><span style=\"color: #e6db74;\">]<\/span><span style=\"color: #a6e22e;\">]<\/span>\n\n    <span style=\"color: #a6e22e;\">(<\/span><span style=\"color: #f92672;\">with-open<\/span> <span style=\"color: #e6db74;\">[<\/span>out-file <span style=\"color: #fd971f;\">(<\/span><span style=\"color: #66d9ef;\">io<\/span><span style=\"color: #66d9ef;\">\/<\/span>writer <span style=\"color: #f92672;\">(<\/span>str <span style=\"color: #e6db74;\">\"resources\/\"<\/span> name <span style=\"color: #e6db74;\">\".csv\"<\/span><span style=\"color: #f92672;\">)<\/span><span style=\"color: #fd971f;\">)<\/span><span style=\"color: #e6db74;\">]<\/span>\n      <span style=\"color: #e6db74;\">(<\/span><span style=\"color: #66d9ef;\">csv<\/span><span style=\"color: #66d9ef;\">\/<\/span>write-csv out-file <span style=\"color: #fd971f;\">[<\/span><span style=\"color: #f92672;\">[<\/span><span style=\"color: #e6db74;\">\"class\"<\/span> <span style=\"color: #e6db74;\">\"title\"<\/span> <span style=\"color: #e6db74;\">\"url\"<\/span><span style=\"color: #f92672;\">]<\/span><span style=\"color: #fd971f;\">]<\/span><span style=\"color: #e6db74;\">)<\/span>\n      <span style=\"color: #e6db74;\">(<\/span><span style=\"color: #f92672;\">doseq<\/span> <span style=\"color: #fd971f;\">[<\/span>feed-url feeds<span style=\"color: #fd971f;\">]<\/span>\n        <span style=\"color: #fd971f;\">(<\/span><span style=\"color: #f92672;\">doseq<\/span> <span style=\"color: #f92672;\">[<\/span>item <span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #ae81ff;\">:entries<\/span> <span style=\"color: #66d9ef;\">(<\/span><span style=\"color: #66d9ef;\">feed<\/span><span style=\"color: #66d9ef;\">\/<\/span>parse-feed feed-url<span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span><span style=\"color: #f92672;\">]<\/span>\n          <span style=\"color: #f92672;\">(<\/span><span style=\"color: #66d9ef;\">csv<\/span><span style=\"color: #66d9ef;\">\/<\/span>write-csv out-file <span style=\"color: #e6db74;\">\"\"<\/span> <span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #ae81ff;\">:title<\/span> item<span style=\"color: #ae81ff;\">)<\/span> <span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #ae81ff;\">:link<\/span> item<span style=\"color: #ae81ff;\">)<\/span> <span style=\"color: #ae81ff;\">:append<\/span> <span style=\"color: #ae81ff;\">true<\/span><span style=\"color: #f92672;\">)<\/span><span style=\"color: #fd971f;\">)<\/span><span style=\"color: #e6db74;\">)<\/span><span style=\"color: #a6e22e;\">)<\/span><span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<p>This routine creates this third gold standard. Remember, we use the gold standard to evaluate different methods and models to classify an input text to see if it belongs to the domain or not.<\/p>\n<p>For each piece of news aggregated that way, I manually determined if the candidate document belongs to the domain or not. This task can be tricky, and requires a clear understanding of the proper scope for the domain. In this example, I consider an article to belong to the <i>music domain<\/i> if it mentions music concepts such as musical albums, songs, multiple music related topics, etc. If only a singer is mentioned in an article because he broke up with his girlfriend, without further mention of anything related to music, I won&#8217;t tag it as being part of the domain.<\/p>\n<p>[However, under a different interpretation of what should be in the domain wherein any mention of a singer qualifies, then we could extend the classification process to include named entities (the singer) extraction to help properly classify those articles. This revised scope is not used in this article, but it does indicate how your exact domain needs should inform such scoping decisions.]<\/p>\n<p>You can download this new <a href=\"https:\/\/fgiasson.com\/blog\/wp-content\/uploads\/2016\/11\/gold-standard-3.csv\">third gold standard from here<\/a>.<\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-org6f96b8a\" class=\"outline-2\">\n<h2 id=\"org6f96b8a\">Evaluate Initial Domain Model<\/h2>\n<div id=\"text-org6f96b8a\" class=\"outline-text-2\">\n<p>Now that we have updated the training corpuses using the updated scope of the domain compared to the <a href=\"https:\/\/fgiasson.com\/blog\/index.php\/2016\/10\/24\/create-a-domain-text-classifier-using-cognonto\/\">previous tests<\/a>, let&#8217;s analyze the impact of using a new version of KBpedia and to use a reasoner to increase the number of features in our model. Let&#8217;s run our automatic process to evaluate the new models. The remaining steps that needs to be run are:<\/p>\n<ol class=\"org-ol\">\n<li>Configure the classifier (in this case, create the semantic vectors for ESA)<\/li>\n<li>Train the model (in this case, the SVM model), and<\/li>\n<li>Evaluate the model on multiple gold standards.<\/li>\n<\/ol>\n<p>Note: the see the full explanation of how ESA and the SVM classifiers works, please refer to the <a href=\"https:\/\/fgiasson.com\/blog\/index.php\/2016\/10\/24\/create-a-domain-text-classifier-using-cognonto\/\">Create a Domain Text Classifier<br \/>\nUsing Cognonto<\/a> article for more background information.<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #75715e; font-style: italic;\">;; <\/span><span style=\"color: #75715e; font-style: italic;\">Load positive and negative training corpuses<\/span>\n<span style=\"color: #ae81ff;\">(<\/span>load-dictionaries <span style=\"color: #e6db74;\">\"resources\/general-corpus-dictionary.csv\"<\/span> <span style=\"color: #e6db74;\">\"resources\/domain-corpus-dictionary.csv\"<\/span><span style=\"color: #ae81ff;\">)<\/span>\n\n<span style=\"color: #75715e; font-style: italic;\">;; <\/span><span style=\"color: #75715e; font-style: italic;\">Build the ESA semantic interpreter <\/span>\n<span style=\"color: #ae81ff;\">(<\/span>build-semantic-interpreter <span style=\"color: #e6db74;\">\"base\"<\/span> <span style=\"color: #e6db74;\">\"resources\/semantic-interpreters\/base\/\"<\/span> <span style=\"color: #66d9ef;\">(<\/span>distinct <span style=\"color: #a6e22e;\">(<\/span>concat <span style=\"color: #e6db74;\">(<\/span>get-domain-pages<span style=\"color: #e6db74;\">)<\/span> <span style=\"color: #e6db74;\">(<\/span>get-general-pages<span style=\"color: #e6db74;\">)<\/span><span style=\"color: #a6e22e;\">)<\/span><span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span>\n\n<span style=\"color: #75715e; font-style: italic;\">;; <\/span><span style=\"color: #75715e; font-style: italic;\">Build the vectors to feed to a SVM classifier using ESA<\/span>\n<span style=\"color: #ae81ff;\">(<\/span>build-svm-model-vectors <span style=\"color: #e6db74;\">\"resources\/svm\/base\/\"<\/span> <span style=\"color: #ae81ff;\">:corpus-folder-normalized<\/span> <span style=\"color: #e6db74;\">\"resources\/corpus-normalized\/\"<\/span><span style=\"color: #ae81ff;\">)<\/span>\n\n<span style=\"color: #75715e; font-style: italic;\">;; <\/span><span style=\"color: #75715e; font-style: italic;\">Train the SVM using the best parameters discovered in the previous tests<\/span>\n<span style=\"color: #ae81ff;\">(<\/span>train-svm-model <span style=\"color: #e6db74;\">\"svm.w50\"<\/span> <span style=\"color: #e6db74;\">\"resources\/svm\/base\/\"<\/span>\n                 <span style=\"color: #ae81ff;\">:weights<\/span> <span style=\"color: #66d9ef;\">{<\/span>1 50.0<span style=\"color: #66d9ef;\">}<\/span>\n                 <span style=\"color: #ae81ff;\">:v<\/span> <span style=\"color: #ae81ff;\">nil<\/span>\n                 <span style=\"color: #ae81ff;\">:c<\/span> 1\n                 <span style=\"color: #ae81ff;\">:algorithm<\/span> <span style=\"color: #ae81ff;\">:l2l2<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<p>Let&#8217;s evaluate this model using our three gold standards:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span>evaluate-model <span style=\"color: #e6db74;\">\"svm.goldstandard.1.w50\"<\/span> <span style=\"color: #e6db74;\">\"resources\/gold-standard-1.csv\"<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<pre class=\"example\">True positive:  21\nFalse positive:  3\nTrue negative:  306\nFalse negative:  6\n\nPrecision:  0.875\nRecall:  0.7777778\nAccuracy:  0.97321427\nF1:  0.8235294<\/pre>\n<p>The performance changes related to the previous results (using KBpedia <code>1.02<\/code>) are:<\/p>\n<ul class=\"org-ul\">\n<li>Precision: <code>+10.33%<\/code><\/li>\n<li>Recall: <code>-12.16%<\/code><\/li>\n<li>Accuracy: <code>+0.31%<\/code><\/li>\n<li>F1: <code>+0.26%<\/code><\/li>\n<\/ul>\n<p>The results for the second gold standard are:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span>evaluate-model <span style=\"color: #e6db74;\">\"svm.goldstandard.2.w50\"<\/span> <span style=\"color: #e6db74;\">\"resources\/gold-standard-2.csv\"<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<pre class=\"example\">True positive:  16\nFalse positive:  3\nTrue negative:  317\nFalse negative:  9\n\nPrecision:  0.84210527\nRecall:  0.64\nAccuracy:  0.9652174\nF1:  0.72727275<\/pre>\n<p>The performances changes related to the previous results (using KBpedia <code>1.02<\/code>) are:<\/p>\n<ul class=\"org-ul\">\n<li>Precision: <code>+6.18%<\/code><\/li>\n<li>Recall: <code>-29.35%<\/code><\/li>\n<li>Accuracy: <code>-1.19%<\/code><\/li>\n<li>F1: <code>-14.63%<\/code><\/li>\n<\/ul>\n<p>What we can say is that the new scope for the domain greatly improved the <code>precision<\/code> of the model. This happens because the new model is probably more complex and better scoped, which leads it to be more selective. However, because of this the <code>recall<\/code> of the model suffers since some of the positive case of our gold standard are not considered to be positive but negative, which now creates new <code>false positives<\/code>. As you can see, there is almost always a tradeoff between <code>precision <\/code>and <code>recall<\/code>. However, you could have 100% <code>precision<\/code> by only having one result right, but then the <code>recall<\/code> would suffer greatly. This is why the <a href=\"https:\/\/en.wikipedia.org\/wiki\/F1_score\">F1<\/a> score is important since it is a weighted average of the <code>precision<\/code> <b>and<\/b> the <code>recall<\/code>.<\/p>\n<p>Now let&#8217;s look at the results of our new gold standard:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span>evaluate-model <span style=\"color: #e6db74;\">\"svm.goldstandard.3.w50\"<\/span> <span style=\"color: #e6db74;\">\"resources\/gold-standard-3.csv\"<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<pre class=\"example\">True positive:  28\nFalse positive:  3\nTrue negative:  355\nFalse negative:  22\n\nPrecision:  0.9032258\nRecall:  0.56\nAccuracy:  0.9387255\nF1:  0.69135803<\/pre>\n<p>Again, with this new gold standard, we can see the same pattern: the <code>precision<\/code> is pretty good, but the <code>recall<\/code> is not that great since about half the <code>true positives<\/code> did not get noticed by the model.<\/p>\n<p>Now, what could we do to try to improve this situation? The next thing we will investigate is to use feature selection and pruning.<\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-org5441239\" class=\"outline-2\">\n<h2 id=\"org5441239\">Features Selection Using Pruning and Training Corpus Pruning<\/h2>\n<div id=\"text-org5441239\" class=\"outline-text-2\">\n<p>A new method that we will investigate to try to improve the performance of the models is called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Feature_selection\">feature selection<\/a>. As its name says, what we are doing is to select specific features to create our prediction model. The idea here is that not all features are born equal and different features may have different (positive or negative) impacts on the model.<\/p>\n<p>In our specific use case, we want to do feature selection using a pruning technique. What we will do is to count the number of tokens for each of our features, and each of the Wikipedia page related to these features. If the number of tokens in an article is too small (below 100), then we will drop that feature.<\/p>\n<p>[Note: feature selection is a complex topic; other options and nuances are not further discussed here.]<\/p>\n<p>The idea here is not to give undue importance to a feature for which we lack proper positive documents in the training corpus. Depending on the feature, it may, or may not, have an impact on the overall model&#8217;s performance.<\/p>\n<p>Pruning the general and domain specific dictionaries is really simple. We only have to read the current dictionaries, to read each of the documents mentioned in the dictionary from the cache, to calculate the number of tokens in each, and then to keep them or to drop them if they reach a certain threshold. Finally we write a new dictionary with the pruned features and documents:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #f92672;\">defn<\/span> <span style=\"color: #a6e22e;\">create-pruned-pages-dictionary-csv<\/span>\n  <span style=\"color: #66d9ef;\">[<\/span>dictionary-file prunned-file normalized-corpus-folder &amp; <span style=\"color: #a6e22e;\">{<\/span><span style=\"color: #ae81ff;\">:keys<\/span> <span style=\"color: #e6db74;\">[<\/span>min-tokens<span style=\"color: #e6db74;\">]<\/span>\n                                                            <span style=\"color: #ae81ff;\">:or<\/span> <span style=\"color: #e6db74;\">{<\/span>min-tokens 100<span style=\"color: #e6db74;\">}<\/span><span style=\"color: #a6e22e;\">}<\/span><span style=\"color: #66d9ef;\">]<\/span>\n  <span style=\"color: #66d9ef;\">(<\/span><span style=\"color: #f92672;\">let<\/span> <span style=\"color: #a6e22e;\">[<\/span>dictionary <span style=\"color: #e6db74;\">(<\/span>rest\n                    <span style=\"color: #fd971f;\">(<\/span><span style=\"color: #f92672;\">with-open<\/span> <span style=\"color: #f92672;\">[<\/span>in-file <span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #66d9ef;\">io<\/span><span style=\"color: #66d9ef;\">\/<\/span>reader dictionary-file<span style=\"color: #ae81ff;\">)<\/span><span style=\"color: #f92672;\">]<\/span>\n                      <span style=\"color: #f92672;\">(<\/span><span style=\"color: #f92672;\">doall<\/span>\n                       <span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #66d9ef;\">csv<\/span><span style=\"color: #66d9ef;\">\/<\/span>read-csv in-file<span style=\"color: #ae81ff;\">)<\/span><span style=\"color: #f92672;\">)<\/span><span style=\"color: #fd971f;\">)<\/span><span style=\"color: #e6db74;\">)<\/span><span style=\"color: #a6e22e;\">]<\/span>\n    <span style=\"color: #a6e22e;\">(<\/span><span style=\"color: #f92672;\">with-open<\/span> <span style=\"color: #e6db74;\">[<\/span>out-file <span style=\"color: #fd971f;\">(<\/span><span style=\"color: #66d9ef;\">io<\/span><span style=\"color: #66d9ef;\">\/<\/span>writer prunned-file<span style=\"color: #fd971f;\">)<\/span><span style=\"color: #e6db74;\">]<\/span>\n      <span style=\"color: #e6db74;\">(<\/span><span style=\"color: #66d9ef;\">csv<\/span><span style=\"color: #66d9ef;\">\/<\/span>write-csv out-file <span style=\"color: #fd971f;\">(<\/span><span style=\"color: #f92672;\">-&gt;&gt;<\/span> dictionary\n                                   <span style=\"color: #f92672;\">(<\/span>mapv <span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #f92672;\">fn<\/span> <span style=\"color: #66d9ef;\">[<\/span><span style=\"color: #a6e22e;\">[<\/span>title rc<span style=\"color: #a6e22e;\">]<\/span><span style=\"color: #66d9ef;\">]<\/span>\n                                           <span style=\"color: #66d9ef;\">(<\/span><span style=\"color: #f92672;\">when<\/span> <span style=\"color: #a6e22e;\">(<\/span><span style=\"color: #f92672;\">.exists<\/span> <span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #66d9ef;\">io<\/span><span style=\"color: #66d9ef;\">\/<\/span>as-file <span style=\"color: #66d9ef;\">(<\/span>str normalized-corpus-folder title <span style=\"color: #e6db74;\">\".txt\"<\/span><span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span><span style=\"color: #a6e22e;\">)<\/span>\n                                             <span style=\"color: #a6e22e;\">(<\/span><span style=\"color: #f92672;\">when<\/span> <span style=\"color: #ae81ff;\">(<\/span>&gt; <span style=\"color: #66d9ef;\">(<\/span><span style=\"color: #f92672;\">-&gt;&gt;<\/span> <span style=\"color: #a6e22e;\">(<\/span>slurp <span style=\"color: #e6db74;\">(<\/span>str normalized-corpus-folder title <span style=\"color: #e6db74;\">\".txt\"<\/span><span style=\"color: #e6db74;\">)<\/span><span style=\"color: #a6e22e;\">)<\/span>\n                                                           tokenize\n                                                           count<span style=\"color: #66d9ef;\">)<\/span> min-tokens<span style=\"color: #ae81ff;\">)<\/span>\n                                               <span style=\"color: #ae81ff;\">[<\/span><span style=\"color: #66d9ef;\">[<\/span>title rc<span style=\"color: #66d9ef;\">]<\/span><span style=\"color: #ae81ff;\">]<\/span><span style=\"color: #a6e22e;\">)<\/span><span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span><span style=\"color: #f92672;\">)<\/span>\n                                   <span style=\"color: #f92672;\">(<\/span>apply concat<span style=\"color: #f92672;\">)<\/span>\n                                   <span style=\"color: #f92672;\">(<\/span>into <span style=\"color: #ae81ff;\">[<\/span><span style=\"color: #ae81ff;\">]<\/span><span style=\"color: #f92672;\">)<\/span><span style=\"color: #fd971f;\">)<\/span><span style=\"color: #e6db74;\">)<\/span><span style=\"color: #a6e22e;\">)<\/span><span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<p>Then we can prune the general and domain specific dictionaries using this simple function:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span>create-pruned-pages-dictionary-csv <span style=\"color: #e6db74;\">\"resources\/general-corpus-dictionary.csv\"<\/span>\n                                    <span style=\"color: #e6db74;\">\"resources\/general-corpus-dictionary.pruned.csv\"<\/span> \n                                    <span style=\"color: #e6db74;\">\"resources\/corpus-normalized\/\"<\/span>\n                                    min-tokens 100<span style=\"color: #ae81ff;\">)<\/span>\n\n<span style=\"color: #ae81ff;\">(<\/span>create-pruned-pages-dictionary-csv <span style=\"color: #e6db74;\">\"resources\/domain-corpus-dictionary.csv\"<\/span>\n                                    <span style=\"color: #e6db74;\">\"resources\/domain-corpus-dictionary.pruned.csv\"<\/span> \n                                    <span style=\"color: #e6db74;\">\"resources\/corpus-normalized\/\"<\/span>\n                                    min-tokens 100<span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<p>As a result of this specific pruning approach, the number of features drops from <code>197<\/code> to <code>175<\/code>.<\/p>\n<\/div>\n<div id=\"outline-container-orge5c6e49\" class=\"outline-3\">\n<h3 id=\"orge5c6e49\">Evaluating Pruned Training Corpuses and Selected Features<\/h3>\n<div id=\"text-orge5c6e49\" class=\"outline-text-3\">\n<p>Now that the training corpuses have been pruned, let&#8217;s load them and then evaluate their performance on the gold standards.<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #75715e; font-style: italic;\">;; <\/span><span style=\"color: #75715e; font-style: italic;\">Load positive and negative pruned training corpuses<\/span>\n<span style=\"color: #ae81ff;\">(<\/span>load-dictionaries <span style=\"color: #e6db74;\">\"resources\/general-corpus-dictionary.pruned.csv\"<\/span> <span style=\"color: #e6db74;\">\"resources\/domain-corpus-dictionary.pruned.csv\"<\/span><span style=\"color: #ae81ff;\">)<\/span>\n\n<span style=\"color: #75715e; font-style: italic;\">;; <\/span><span style=\"color: #75715e; font-style: italic;\">Build the ESA semantic interpreter <\/span>\n<span style=\"color: #ae81ff;\">(<\/span>build-semantic-interpreter <span style=\"color: #e6db74;\">\"base\"<\/span> <span style=\"color: #e6db74;\">\"resources\/semantic-interpreters\/base-pruned\/\"<\/span> <span style=\"color: #66d9ef;\">(<\/span>distinct <span style=\"color: #a6e22e;\">(<\/span>concat <span style=\"color: #e6db74;\">(<\/span>get-domain-pages<span style=\"color: #e6db74;\">)<\/span> <span style=\"color: #e6db74;\">(<\/span>get-general-pages<span style=\"color: #e6db74;\">)<\/span><span style=\"color: #a6e22e;\">)<\/span><span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span>\n\n<span style=\"color: #75715e; font-style: italic;\">;; <\/span><span style=\"color: #75715e; font-style: italic;\">Build the vectors to feed to a SVM classifier using ESA<\/span>\n<span style=\"color: #ae81ff;\">(<\/span>build-svm-model-vectors <span style=\"color: #e6db74;\">\"resources\/svm\/base-pruned\/\"<\/span> <span style=\"color: #ae81ff;\">:corpus-folder-normalized<\/span> <span style=\"color: #e6db74;\">\"resources\/corpus-normalized\/\"<\/span><span style=\"color: #ae81ff;\">)<\/span>\n\n<span style=\"color: #75715e; font-style: italic;\">;; <\/span><span style=\"color: #75715e; font-style: italic;\">Train the SVM using the best parameters discovered in the previous tests<\/span>\n<span style=\"color: #ae81ff;\">(<\/span>train-svm-model <span style=\"color: #e6db74;\">\"svm.w50\"<\/span> <span style=\"color: #e6db74;\">\"resources\/svm\/base-pruned\/\"<\/span>\n                 <span style=\"color: #ae81ff;\">:weights<\/span> <span style=\"color: #66d9ef;\">{<\/span>1 50.0<span style=\"color: #66d9ef;\">}<\/span>\n                 <span style=\"color: #ae81ff;\">:v<\/span> <span style=\"color: #ae81ff;\">nil<\/span>\n                 <span style=\"color: #ae81ff;\">:c<\/span> 1\n                 <span style=\"color: #ae81ff;\">:algorithm<\/span> <span style=\"color: #ae81ff;\">:l2l2<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<p>Let&#8217;s evaluate this model using our three gold standards:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span>evaluate-model <span style=\"color: #e6db74;\">\"svm.pruned.goldstandard.1.w50\"<\/span> <span style=\"color: #e6db74;\">\"resources\/gold-standard-1.csv\"<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<pre class=\"example\">True positive:  21\nFalse positive:  2\nTrue negative:  307\nFalse negative:  6\n\nPrecision:  0.9130435\nRecall:  0.7777778\nAccuracy:  0.97619045\nF1:  0.84000003<\/pre>\n<p>The performances changes related to the initial results (using KBpedia <code>1.02<\/code>) are:<\/p>\n<ul class=\"org-ul\">\n<li>Precision: <code>+18.75%<\/code><\/li>\n<li>Recall: <code>-12.08%<\/code><\/li>\n<li>Accuracy: <code>+0.61%<\/code><\/li>\n<li>F1: <code>+2.26%<\/code><\/li>\n<\/ul>\n<p>In this case, compared with the previous results (non-pruned with KBpedia <code>1.10<\/code>), we improved the <code>precision<\/code> without decreasing the <code>recall<\/code> which is the ultimate goal. This means that the <code>F1<\/code> score increased by <code>2.26%<\/code> just by pruning, for this gold standard.<\/p>\n<p>The results for the second gold standard are:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span>evaluate-model <span style=\"color: #e6db74;\">\"svm.goldstandard.2.w50\"<\/span> <span style=\"color: #e6db74;\">\"resources\/gold-standard-2.csv\"<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<pre class=\"example\">True positive:  16\nFalse positive:  3\nTrue negative:  317\nFalse negative:  9\n\nPrecision:  0.84210527\nRecall:  0.64\nAccuracy:  0.9652174\nF1:  0.72727275<\/pre>\n<p>The performances changes related to the previous results (using KBpedia <code>1.02<\/code>) are:<\/p>\n<ul class=\"org-ul\">\n<li>Precision: <code>+6.18%<\/code><\/li>\n<li>Recall: <code>-29.35%<\/code><\/li>\n<li>Accuracy: <code>-1.19%<\/code><\/li>\n<li>F1: <code>-14.63%<\/code><\/li>\n<\/ul>\n<p>In this case, the results are identical (with non-pruned with KBpedia <code>1.10<\/code>). Pruning did not change anything. Considering the relatively small size of the gold standard, this is to be expected since the model also did not drastically change.<\/p>\n<p>Now let&#8217;s look at the results of our new gold standard:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span>evaluate-model <span style=\"color: #e6db74;\">\"svm.goldstandard.3.w50\"<\/span> <span style=\"color: #e6db74;\">\"resources\/gold-standard-3.csv\"<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<pre class=\"example\">True positive:  27\nFalse positive:  7\nTrue negative:  351\nFalse negative:  23\n\nPrecision:  0.7941176\nRecall:  0.54\nAccuracy:  0.9264706\nF1:  0.64285713<\/pre>\n<p>Now let&#8217;s check how these compare to the non-pruned version of the training corpus:<\/p>\n<ul class=\"org-ul\">\n<li>Precision: <code>-12.08%<\/code><\/li>\n<li>Recall: <code>-3.7%<\/code><\/li>\n<li>Accuracy: <code>-1.31%<\/code><\/li>\n<li>F1: <code>-7.02%<\/code><\/li>\n<\/ul>\n<p>Both <code>false positives<\/code> and <code>false negatives<\/code> increased with this change, which also led to a decrease in the overall metrics. What happened?<\/p>\n<p>Different things may have happened in fact. Maybe the new set of features are not optimal, or maybe the hyperparameters of the SVM classifier are offset. This is what we will try to figure out by working with two new methods that we will use to try to continue to improve our model: hyperparameters optimization using grid search and using ensembles learning.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"outline-container-org6d5f5e6\" class=\"outline-2\">\n<h2 id=\"org6d5f5e6\">Hyperparameters Optimization Using Grid Search<\/h2>\n<div id=\"text-org6d5f5e6\" class=\"outline-text-2\">\n<p>Hyperparameters are parameters that are not learned by the estimators. They are a kind of configuration option for an algorithm. In the case of a linear SVM, hyperparameters are <code>C<\/code>, <code>epsilon<\/code>, <code>weight<\/code> and the <code>algorithm<\/code> used. Hyperparameter optimization is the task of trying to find the right parameter values in order to optimize the performance of the model.<\/p>\n<p>There are multiple different strategies that we can use to try to find the best values for these hyperparameters, but the one we will use is called the grid search, which exhaustively searches across a manually defined subset of possible hyperparameter values.<\/p>\n<p>The grid search function we want to define will enable us to specify the <code>algorithm(s)<\/code>, the <code>weight(s)<\/code>, <code>C<\/code> and the <code>stopping tolerence<\/code>. Then we will want the grid search to keep the hyperparameters that optimize the score of the metric we want to focus on. We also have to specify the gold standard we want to use to evaluate the performance of the different models.<\/p>\n<p>Here is the function that implements that grid search algorithm:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #f92672;\">defn<\/span> <span style=\"color: #a6e22e;\">svm-grid-search<\/span>\n  <span style=\"color: #66d9ef;\">[<\/span>name model-path gold-standard &amp; <span style=\"color: #a6e22e;\">{<\/span><span style=\"color: #ae81ff;\">:keys<\/span> <span style=\"color: #e6db74;\">[<\/span>grid-parameters selection-metric<span style=\"color: #e6db74;\">]<\/span>\n                                    <span style=\"color: #ae81ff;\">:or<\/span> <span style=\"color: #e6db74;\">{<\/span>grid-parameters <span style=\"color: #fd971f;\">[<\/span><span style=\"color: #f92672;\">{<\/span><span style=\"color: #ae81ff;\">:c<\/span> <span style=\"color: #ae81ff;\">[<\/span>1 2 4 16 256<span style=\"color: #ae81ff;\">]<\/span>\n                                                           <span style=\"color: #ae81ff;\">:e<\/span> <span style=\"color: #ae81ff;\">[<\/span>0.001 0.01 0.1<span style=\"color: #ae81ff;\">]<\/span>\n                                                           <span style=\"color: #ae81ff;\">:algorithm<\/span> <span style=\"color: #ae81ff;\">[<\/span><span style=\"color: #ae81ff;\">:l2l2<\/span><span style=\"color: #ae81ff;\">]<\/span>\n                                                           <span style=\"color: #ae81ff;\">:weight<\/span> <span style=\"color: #ae81ff;\">[<\/span>1 15 30<span style=\"color: #ae81ff;\">]<\/span><span style=\"color: #f92672;\">}<\/span><span style=\"color: #fd971f;\">]<\/span>\n                                         selection-metric <span style=\"color: #ae81ff;\">:f1<\/span><span style=\"color: #e6db74;\">}<\/span><span style=\"color: #a6e22e;\">}<\/span><span style=\"color: #66d9ef;\">]<\/span>\n  <span style=\"color: #66d9ef;\">(<\/span><span style=\"color: #f92672;\">let<\/span> <span style=\"color: #a6e22e;\">[<\/span>best <span style=\"color: #e6db74;\">(<\/span>atom <span style=\"color: #fd971f;\">{<\/span><span style=\"color: #ae81ff;\">:gold-standard<\/span> gold-standard\n                    <span style=\"color: #ae81ff;\">:selection-metric<\/span> selection-metric\n                    <span style=\"color: #ae81ff;\">:score<\/span> 0.0\n                    <span style=\"color: #ae81ff;\">:c<\/span> <span style=\"color: #ae81ff;\">nil<\/span>\n                    <span style=\"color: #ae81ff;\">:e<\/span> <span style=\"color: #ae81ff;\">nil<\/span>\n                    <span style=\"color: #ae81ff;\">:algorithm<\/span> <span style=\"color: #ae81ff;\">nil<\/span>\n                    <span style=\"color: #ae81ff;\">:weight<\/span> <span style=\"color: #ae81ff;\">nil<\/span><span style=\"color: #fd971f;\">}<\/span><span style=\"color: #e6db74;\">)<\/span>\n        model-vectors <span style=\"color: #e6db74;\">(<\/span>read-string <span style=\"color: #fd971f;\">(<\/span>slurp <span style=\"color: #f92672;\">(<\/span>str model-path <span style=\"color: #e6db74;\">\"model.vectors\"<\/span><span style=\"color: #f92672;\">)<\/span><span style=\"color: #fd971f;\">)<\/span><span style=\"color: #e6db74;\">)<\/span><span style=\"color: #a6e22e;\">]<\/span>\n    <span style=\"color: #a6e22e;\">(<\/span><span style=\"color: #f92672;\">doseq<\/span> <span style=\"color: #e6db74;\">[<\/span>parameters grid-parameters<span style=\"color: #e6db74;\">]<\/span>\n      <span style=\"color: #e6db74;\">(<\/span><span style=\"color: #f92672;\">doseq<\/span> <span style=\"color: #fd971f;\">[<\/span>algo <span style=\"color: #f92672;\">(<\/span><span style=\"color: #ae81ff;\">:algorithm<\/span> parameters<span style=\"color: #f92672;\">)<\/span><span style=\"color: #fd971f;\">]<\/span>\n        <span style=\"color: #fd971f;\">(<\/span><span style=\"color: #f92672;\">doseq<\/span> <span style=\"color: #f92672;\">[<\/span>weight <span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #ae81ff;\">:weight<\/span> parameters<span style=\"color: #ae81ff;\">)<\/span><span style=\"color: #f92672;\">]<\/span>\n          <span style=\"color: #f92672;\">(<\/span><span style=\"color: #f92672;\">doseq<\/span> <span style=\"color: #ae81ff;\">[<\/span>e <span style=\"color: #66d9ef;\">(<\/span><span style=\"color: #ae81ff;\">:e<\/span> parameters<span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">]<\/span>\n            <span style=\"color: #ae81ff;\">(<\/span><span style=\"color: #f92672;\">doseq<\/span> <span style=\"color: #66d9ef;\">[<\/span>c <span style=\"color: #a6e22e;\">(<\/span><span style=\"color: #ae81ff;\">:c<\/span> parameters<span style=\"color: #a6e22e;\">)<\/span><span style=\"color: #66d9ef;\">]<\/span>\n              <span style=\"color: #66d9ef;\">(<\/span>train-svm-model name model-path\n                               <span style=\"color: #ae81ff;\">:weights<\/span> <span style=\"color: #a6e22e;\">{<\/span>1 <span style=\"color: #ae81ff;\">(<\/span>double weight<span style=\"color: #ae81ff;\">)<\/span><span style=\"color: #a6e22e;\">}<\/span>\n                               <span style=\"color: #ae81ff;\">:v<\/span> <span style=\"color: #ae81ff;\">nil<\/span>\n                               <span style=\"color: #ae81ff;\">:c<\/span> c\n                               <span style=\"color: #ae81ff;\">:e<\/span> e\n                               <span style=\"color: #ae81ff;\">:algorithm<\/span> algo\n                               <span style=\"color: #ae81ff;\">:model-vectors<\/span> model-vectors<span style=\"color: #66d9ef;\">)<\/span>\n              <span style=\"color: #66d9ef;\">(<\/span><span style=\"color: #f92672;\">let<\/span> <span style=\"color: #a6e22e;\">[<\/span>results <span style=\"color: #ae81ff;\">(<\/span>evaluate-model name gold-standard <span style=\"color: #ae81ff;\">:output<\/span> <span style=\"color: #ae81ff;\">false<\/span><span style=\"color: #ae81ff;\">)<\/span><span style=\"color: #a6e22e;\">]<\/span>              \n                <span style=\"color: #a6e22e;\">(<\/span>println <span style=\"color: #e6db74;\">\"Algorithm:\"<\/span> algo<span style=\"color: #a6e22e;\">)<\/span>\n                <span style=\"color: #a6e22e;\">(<\/span>println <span style=\"color: #e6db74;\">\"C:\"<\/span> c<span style=\"color: #a6e22e;\">)<\/span>\n                <span style=\"color: #a6e22e;\">(<\/span>println <span style=\"color: #e6db74;\">\"Epsilon:\"<\/span> e<span style=\"color: #a6e22e;\">)<\/span>\n                <span style=\"color: #a6e22e;\">(<\/span>println <span style=\"color: #e6db74;\">\"Weight:\"<\/span> weight<span style=\"color: #a6e22e;\">)<\/span>\n                <span style=\"color: #a6e22e;\">(<\/span>println selection-metric <span style=\"color: #e6db74;\">\":\"<\/span> <span style=\"color: #ae81ff;\">(<\/span>get results selection-metric<span style=\"color: #ae81ff;\">)<\/span><span style=\"color: #a6e22e;\">)<\/span>\n                <span style=\"color: #a6e22e;\">(<\/span>println<span style=\"color: #a6e22e;\">)<\/span>\n\n                <span style=\"color: #a6e22e;\">(<\/span><span style=\"color: #f92672;\">when<\/span> <span style=\"color: #ae81ff;\">(<\/span>&gt; <span style=\"color: #66d9ef;\">(<\/span>get results selection-metric<span style=\"color: #66d9ef;\">)<\/span> <span style=\"color: #66d9ef;\">(<\/span><span style=\"color: #ae81ff;\">:score<\/span> @best<span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span>\n                  <span style=\"color: #ae81ff;\">(<\/span>reset! best <span style=\"color: #66d9ef;\">{<\/span><span style=\"color: #ae81ff;\">:gold-standard<\/span> gold-standard\n                                <span style=\"color: #ae81ff;\">:selection-metric<\/span> selection-metric\n                                <span style=\"color: #ae81ff;\">:score<\/span> <span style=\"color: #a6e22e;\">(<\/span>get results selection-metric<span style=\"color: #a6e22e;\">)<\/span>\n                                <span style=\"color: #ae81ff;\">:c<\/span> c\n                                <span style=\"color: #ae81ff;\">:e<\/span> e\n                                <span style=\"color: #ae81ff;\">:algorithm<\/span> algo\n                                <span style=\"color: #ae81ff;\">:weight<\/span> weight<span style=\"color: #66d9ef;\">}<\/span><span style=\"color: #ae81ff;\">)<\/span><span style=\"color: #a6e22e;\">)<\/span><span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span><span style=\"color: #f92672;\">)<\/span><span style=\"color: #fd971f;\">)<\/span><span style=\"color: #e6db74;\">)<\/span><span style=\"color: #a6e22e;\">)<\/span>\n    @best<span style=\"color: #66d9ef;\">)<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<p>The possible algorithms are:<\/p>\n<ol class=\"org-ol\">\n<li><code>:l2lr_primal<\/code><\/li>\n<li><code>:l2l2<\/code><\/li>\n<li><code>:l2l2_primal<\/code><\/li>\n<li><code>:l2l1<\/code><\/li>\n<li><code>:multi<\/code><\/li>\n<li><code>:l1l2_primal<\/code><\/li>\n<li><code>:l1lr<\/code><\/li>\n<li><code>:l2lr<\/code><\/li>\n<\/ol>\n<p>To simplify things a little bit for this task, we will merge the three gold standards we have into one. We will use that gold standard moving forward. The merged gold standard can be <a href=\"https:\/\/fgiasson.com\/blog\/wp-content\/uploads\/2016\/11\/gold-standard-full.csv\">downloaded from here<\/a>. We now have a single gold standard with 1017 manually vetted web pages.<\/p>\n<p>Now that we have a new consolidated gold standard, let&#8217;s calculate the performance of the models when the training corpuses are pruned and not. This will become the new basis to compare the subsequent results in this article. The metrics when they training corpuses <b>are<\/b> pruned:<\/p>\n<blockquote><p>True positive: 56<br \/>\nfalse positive: 10<br \/>\nTrue negative: 913<br \/>\nFalse negative: 38<\/p>\n<p>Precision: 0.8484849<br \/>\nRecall: 0.59574467<br \/>\nAccuracy: 0.95280236<br \/>\nF1: 0.7<\/p><\/blockquote>\n<p>Now, let&#8217;s run the grid search that will try to optimize the <code>F1<\/code> score of the model using the pruned training corpuses and using the full gold standard:<\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-clojure\"><span style=\"color: #ae81ff;\">(<\/span>svm-grid-search <span style=\"color: #e6db74;\">\"grid-search-base-pruned-tests\"<\/span> \n                 <span style=\"color: #e6db74;\">\"resources\/svm\/base-pruned\/\"<\/span> \n                 <span style=\"color: #e6db74;\">\"resources\/gold-standard-full.csv\"<\/span>\n                 <span style=\"color: #ae81ff;\">:selection-metric<\/span> <span style=\"color: #ae81ff;\">:f1<\/span>\n                 <span style=\"color: #ae81ff;\">:grid-parameters<\/span> <span style=\"color: #66d9ef;\">[<\/span><span style=\"color: #a6e22e;\">{<\/span><span style=\"color: #ae81ff;\">:c<\/span> <span style=\"color: #e6db74;\">[<\/span>1 2 4 16 256<span style=\"color: #e6db74;\">]<\/span>\n                                    <span style=\"color: #ae81ff;\">:e<\/span> <span style=\"color: #e6db74;\">[<\/span>0.001 0.01 0.1<span style=\"color: #e6db74;\">]<\/span>\n                                    <span style=\"color: #ae81ff;\">:algorithm<\/span> <span style=\"color: #e6db74;\">[<\/span><span style=\"color: #ae81ff;\">:l2l2<\/span><span style=\"color: #e6db74;\">]<\/span>\n                                    <span style=\"color: #ae81ff;\">:weight<\/span> <span style=\"color: #e6db74;\">[<\/span>1 15 30<span style=\"color: #e6db74;\">]<\/span><span style=\"color: #a6e22e;\">}<\/span><span style=\"color: #66d9ef;\">]<\/span><span style=\"color: #ae81ff;\">)<\/span>\n<\/pre>\n<\/div>\n<pre class=\"example\">{:gold-standard \"resources\/gold-standard-full.csv\"\n :selection-metric :f1\n :score 0.7096774\n :c 2\n :e 0.001\n :algorithm :l2l2\n :weight 30}<\/pre>\n<p>With a simple subset of the possible hyperparameter space, we found that by increasing the <code>c<\/code> parameter to 2 we could improve the performance of the <code>F1<\/code> score on the gold standard by <code>1.37%<\/code>. It is not a huge gain, but it is still an appreciable gain given the miinimal effort invested so far (basically: waiting for the grid search to finish). Subsequently we could tweak the subset of parameters to try to improve a little further. Let&#8217;s try with <code>c = [ 1.5 , 2 , 2.5 ]<\/code> and <code>weight = [30, 40]<\/code>. Let&#8217;s also try to check with other algorithms as well like <code>L2-regularized L1-loss support vector regression (dual)<\/code>.<\/p>\n<p>The goal here is to configure the initial grid search with general parameters with a wide range of possible values. Then subsequently we can use that tool to fine tune some of the parameters that were returning good results. In any case, the more computer power and time you have, the more tests you will be able to perform.<\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-org36a6d55\" class=\"outline-2\">\n<h2 id=\"org36a6d55\">Part 2<\/h2>\n<div id=\"text-org36a6d55\" class=\"outline-text-2\">\n<p><a href=\"https:\/\/fgiasson.com\/blog\/index.php\/2016\/11\/17\/dynamic-machine-learning-using-the-kbpedia-knowledge-graph-part-2\/\">Continue with part 2<\/a>\u00e2\u20ac\u00a6<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In my previous blog post, Create a Domain Text Classifier Using Cognonto, I explained how one can use the KBpedia Knowledge Graph to automatically create positive and negative training corpuses for different machine learning tasks. I explained how SVM classifiers could be trained and used to check if an input text belongs to the defined [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[293,287,84],"tags":[263,296,289,231],"class_list":["post-3455","post","type-post","status-publish","format-standard","hentry","category-artificial-intelligence","category-cognonto","category-semantic-web","tag-ai","tag-knowledgegraph","tag-machinelearning","tag-semanticweb"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3455"}],"version-history":[{"count":11,"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3455\/revisions"}],"predecessor-version":[{"id":3493,"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3455\/revisions\/3493"}],"wp:attachment":[{"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}