<?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 Himanshu Swamy on Medium]]></title>
        <description><![CDATA[Stories by Himanshu Swamy on Medium]]></description>
        <link>https://medium.com/@Hswamy?source=rss-8a4ee0427b4e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*KivwMsvIqDSS7q59TU94sg.png</url>
            <title>Stories by Himanshu Swamy on Medium</title>
            <link>https://medium.com/@Hswamy?source=rss-8a4ee0427b4e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 21:15:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Hswamy/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[Agents are for real…Lets use them for some recommendations! …Movies..?]]></title>
            <link>https://medium.com/@Hswamy/agents-are-for-real-lets-use-them-for-some-recommendations-movies-7bf4b5dab8e0?source=rss-8a4ee0427b4e------2</link>
            <guid isPermaLink="false">https://medium.com/p/7bf4b5dab8e0</guid>
            <category><![CDATA[genai]]></category>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Himanshu Swamy]]></dc:creator>
            <pubDate>Sun, 19 Jan 2025 21:36:06 GMT</pubDate>
            <atom:updated>2025-01-19T21:36:06.262Z</atom:updated>
            <content:encoded><![CDATA[<h3>Agents are for real…Lets use them for some Analysis &amp; recommendations! …Movies..?</h3><p>This article explores a proof of concept for an Agent driven movie recommendation system that showcases fundamental aspects of agency. The system leverages the advanced capabilities of GPT-3.5 TURBO, a 175-billion parameter language model developed by OpenAI. This methodology facilitates enhanced community engagement in its development and deployment, thereby democratizing access to powerful AI tools.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4EeyFzXxgm6r8YCpek_RKg.png" /><figcaption>Workflow for an Agentic solution</figcaption></figure><p>Let’s dive into the world of AI agents…?</p><p>So what is an agent?</p><p>When engaging with a sophisticated language model like OpenAI’s GPT-3 or GPT-4, the process involves submitting a query, such as “How many continents are there?” The query is processed by the language model, which has been extensively trained on vast datasets. Leveraging its comprehensive training, the model is adept at providing accurate and contextually relevant responses to such inquiries.Suppose you decide to ask a question like, “How old is my car?” In this case, a large language model (LLM), such as the one we’re discussing, is likely to face difficulties in providing the correct response. This is because the model lacks the specific context or background knowledge required to answer your question accurately. The LLM does not know which car you are referring to, as it was not designed or trained on personal or specific information about individual users or their possessions. Its training is based on a vast dataset of general knowledge, which doesn’t include your car’s details or any private or unique information about you.</p><p>As a result, the model may end up providing a generic response, which could either be incomplete, irrelevant, or outright incorrect. In some cases, the LLM might “hallucinate” an answer, which means it could generate a plausible-sounding but entirely fabricated response based on patterns it has learned. For instance, it might guess an answer like “10 years old,” even though it has no actual basis for that claim. This type of response is problematic because it undermines trust and utility, especially when users are seeking accurate and reliable answers.</p><p>This is where the concept of <strong>AI agents</strong> becomes invaluable. AI agents are specialized tools or systems that can bridge this gap by providing the LLM with access to additional resources or capabilities. For example, an agent could be integrated with the LLM to act as an intermediary that can fetch the necessary information to answer your question. In the case of the car, the agent might be programmed to access relevant databases, search the web, or query other external tools that store information about your vehicle — such as a car registration database or an online service where you’ve logged details about your car.Once the agent retrieves the information — such as the car’s make, model, and registration year — it can feed this back to the LLM, which can then formulate an accurate and contextually appropriate response. This process transforms the LLM from a static repository of general knowledge into a dynamic and interactive system capable of solving specific and personalized problems.In essence, an AI agent is a software entity that leverages artificial intelligence techniques to autonomously perform tasks, gather information, and make decisions on behalf of a user. By combining the general reasoning abilities of a language model with the specialized capabilities of an agent, you create a much more robust system that can handle complex, context-dependent questions. This synergy allows the LLM to move beyond its limitations, enhancing its utility and reliability across a wide range of applications.</p><p>An <strong>agent</strong> is a sophisticated software entity specifically designed to interact with its environment, process inputs from various sources, and provide meaningful outputs or take specific actions based on the data it receives and the objectives it is programmed to accomplish. This interaction makes agents highly dynamic and adaptable to the tasks they are designed to perform.To break this down further, let us consider how an agent operates. When a user provides a query or a task — such as asking a question or requesting specific information — the agent serves as the intermediary that bridges the gap between the user’s needs and the resources or tools required to fulfill those needs. The query or input is first processed by the agent, which analyzes the request, understands the context, and determines how best to address it. Unlike static systems, an agent is capable of reasoning about the problem and selecting the appropriate course of action.</p><p>In the case of a query being passed to an agent, the agent is equipped with the capability to interact seamlessly with a <strong>large language model (LLM)</strong>, such as GPT. The agent understands how to work with the LLM, leveraging its advanced natural language understanding and generation abilities to interpret complex user queries, generate responses, or solve problems. However, the agent does not rely solely on the LLM’s knowledge base, which is limited to what it was trained on during its development. Instead, the true power of an agent lies in its ability to augment the LLM with additional tools and resources.</p><p>These tools act as extensions of the agent’s capabilities, enabling it to go beyond the limitations of the language model. For instance, if the LLM does not have the necessary information to accurately answer a query, the agent can employ one or more of its tools to gather the required data. Examples of such tools include:</p><p>1. <strong>Search Engines or APIs</strong>: The agent might use a web search or query an API to retrieve up-to-date or domain-specific information. For example, if you ask for the age of a car, the agent could consult a car database or registration service.</p><p>2. <strong>Databases</strong>: The agent may access structured databases or private repositories where information is stored, ensuring it retrieves precise and relevant data for a user’s query.</p><p>3. <strong>External Applications</strong>: An agent can interact with external software, such as spreadsheet programs, email services, or even IoT devices, to complete actions or retrieve data.</p><p>4. <strong>Custom Knowledge Bases</strong>: If the organization or individual using the agent has a proprietary knowledge base, the agent can be programmed to fetch and utilize that data.</p><p>For example, imagine you’re using an online travel agency to book flights. This travel agency uses an agent connected to a large language model to help answer questions and suggest places to visit. The agent can pull in information from different sources, like doing a Google search or checking Wikipedia, to give personalized recommendations. It can even look at a user’s past trips to figure out their preferences — like if they usually travel to Asian countries in July. All of this helps the language model make better suggestions and create a smoother experience for the user when booking a trip. In simple terms, the agent acts as a helper that knows how to gather the right tools and information to work with the language model, making the whole process smarter and more personalised.</p><p>Imagine you have an agent that can connect to different tools and systems, like APIs, to gather all the information needed to complete a task and give the right answer back to the user. Think of the agent as a helper that combines all these tools with a big language model to reason through the problem and get things done — something the language model alone couldn’t do on its own.</p><p>For example, let’s say you want a movie recommendation. The agent can look at things like past movie ratings, audience feedback, the plot, characters, music, and more to make a suggestion. It compares all this data, checks what’s out there, and uses the information to find the perfect movie for you. Here’s where it gets a little more complex: the agent can trigger a chain of events. For instance, it might start by observing your preferences through a “watcher” agent. Then, it might pass that information to another agent that prioritizes tasks, followed by an “executor” agent that takes action — whether that’s fetching data or making decisions. All of these agents work together, with each one handling a specific job, and they communicate with the language model to get the best outcome for the user.</p><figure><img alt="View of the analysis &amp; recommendation from an Agent" src="https://cdn-images-1.medium.com/max/1024/1*39AFsAjZ-I1Qwfc6eDIVxw.png" /></figure><p><strong><em>An Initial Analysis &amp; recommendation by an agent</em></strong></p><p>So in short, an agent is a system that combines different tools, helps solve problems, and uses a large language model to create smart, helpful results for users.</p><p>Let’s break down the key features of agents.</p><p>1. <strong>Autonomy</strong>: Agents can work on their own without needing humans to constantly help. The idea is that agents can handle tasks and solve problems by themselves. However, you can still step in and guide them if needed — it’s not like they’re completely cut off from human input, but they’re mainly designed to work independently.</p><p>2. <strong>Learning and Adapting</strong>: Many AI agents can learn from their experiences and get better over time. This is done using machine learning, which helps agents improve their decision-making as they interact with the world around them.</p><p>3. <strong>Interaction</strong>: Agents don’t just work in isolation — they can interact with their environment. This includes other software, databases, devices, and even people. They can communicate through different methods, like chatting with humans, sending API requests, or querying databases. This interaction helps them gather information and get the job done.</p><p>4. <strong>Goal-driven</strong>: Every agent is designed to achieve specific goals. Whether it’s answering questions, solving a problem, or completing a task, agents make decisions and take actions that help them reach their goal.</p><p>Now, let’s look at a few real-life examples of where agents are used:</p><p>· <strong>Customer Service Chatbots</strong>: These are agents that interact with customers to answer questions or solve problems. Instead of a human representative, a chatbot agent can help customers quickly and efficiently.</p><p>· <strong>Personal Assistants</strong>: Imagine an agent that’s like your personal assistant. It can handle all sorts of tasks for you, like scheduling, reminders, or making recommendations.</p><p>There are many more uses for AI agents, but these examples show how they’re designed to be helpful, independent, and goal-oriented.</p><p>Here is the codebase for the above agent:- <a href="https://github.com/himswamy/Agent_for_all">https://github.com/himswamy/Agent_for_all</a></p><p>Happy Learning!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7bf4b5dab8e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Learning & Artificial Intelligence to Reduce Financial Crimes]]></title>
            <link>https://pub.towardsai.net/machine-learning-artificial-intelligence-to-reduce-financial-crimes-766dcfc1bd47?source=rss-8a4ee0427b4e------2</link>
            <guid isPermaLink="false">https://medium.com/p/766dcfc1bd47</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[finance]]></category>
            <category><![CDATA[finserv]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[mls]]></category>
            <dc:creator><![CDATA[Himanshu Swamy]]></dc:creator>
            <pubDate>Tue, 20 Sep 2022 20:02:05 GMT</pubDate>
            <atom:updated>2022-09-20T20:02:05.227Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>Insurance Business Overview</strong></h4><p>One of the major components of the economy is the insurance industry, it protects against the financial risks that are present at all stages of people’s lives and businesses. Insurers protect against loss — of a car, a house, even a life — and pay the policyholder or designee a benefit in the event of that loss. The number of premiums it collects, the scale of its investment, and Insurance play an essential social and economic role by covering personal and business risks. Despite daunting macroeconomic and structural challenges, fierce competition, and ongoing tech-driven disruptions in the last few years, insurers have shown they can undertake large-scale change at a faster pace than many industry veterans thought possible and can deal with unexpected development. All aspects of life, including exposure to risk, can be Individuals and businesses. Whether we accept the consequences of a possible loss or seek insurance coverage in the event of a loss, reducing the exposure to risk depends on us. People who obtain coverage succeed in “transferring the risk” to another organization, i.e., the insurance company.</p><p>Purchasing insurance is the most common risk transfer mechanism for the majority of people and organizations. The money paid from the insured is known as the premium. In return, the insurer agrees to pay a designated benefit in the event of the agreed-upon loss. In 2020 the gross premiums saw a slowdown, but the insurance industry saw a rebound in growth in 2021, especially in the Life sector. If we look at the data, it indicates that insurance companies experienced a rise in gross premiums by 7.3%. Insurance premiums also grew in the non-life sector by 1.7% on average.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*TOJG1GrFTKnwkFEMK2gBHw.png" /><figcaption><strong>Subrogation</strong></figcaption></figure><p>Insured individuals may be able to sue other parties for the losses they have suffered, and a potential source of compensation is paid. Such situations are omnipresent: an insured driver whose car is damaged in an accident may be able to sue another driver who caused the accident for repair costs; an insured homeowner whose house burns down due to fire may be able to get compensation from an Insurance company, or a person covered by health insurance who slips and falls in a store may be able to sue the proprietor for medical expenses. Insurance policies not only promise to compensate insureds for their losses, they also usually include what is known as subrogation provisions that give the insurer the right to step into the shoes of an insured and to sue a party who caused the insured’s losses.</p><p>In Some countries, subrogation provisions are a common feature of property insurance, liability insurance, health and medical insurance, and disability insurance policies. During past years subrogation was the stepchild of the insurance market, but this image is changing. Today, in an environment of flat investment returns, subrogation is being used as a way for insurers to add to the bottom line. In recent times, the insurance industry is getting more sophisticated in analyzing and exploiting subrogation recoveries. The insurance industry has engaged in benchmark studies to help it obtain stronger statistical analyses for its subrogation program. Initial studies were created in the 1990s, but over the past 20 years, insurance companies have vacillated between the industry trend of centralizing and decentralizing their subrogation efforts. With proper analysis of flies closed with no recovery, the carriers can either become more creative or more aggressive, reducing the percentage of files closed without recovery. For subrogation, the areas of focus include motor, property, workers&#39; compensation, and healthcare. Each of these areas has a special focus and strategy, often dependent on the laws of the different countries. For example, in motor recovery, some countries permit recovery of medical expenses, while other countries permit recovery only of property damage. Further, some cities are enacting anti­-subrogation legislation, which will severely limit the ability of insurance companies to recover. The recent research on Personal Auto Collisions — indicated a high­ performing carrier would collect about 24%, while the average is about 11.5%. More recent studies show the standard recovery for paid collisions is up to 27%, while non­standard carriers have a recovery rate of 14.5%. These numbers are a significant increase since the original benchmarks.</p><p>Most insurers are investing in modern systems, advanced data practice, and cutting-edge Machine learning &amp; AI solutions that are flexible and robust to drive transformation. The implementation of the latest Artificial intelligence &amp; Machine learning solutions enables employees to work more efficiently and bring focus to the customer relationship, thus improving the quality of individual interactions. Such investments have positioned these insurers to pursue new markets and advance their growth strategy.</p><p>With advancements made in Artificial intelligence &amp; Machine learning, there is a unique opportunity for insurers to incorporate analytic scores within the decisioning engine to create a smart system. For any Insurance company to become completely data-driven requires significant efforts with more feeds from data and predictive models. The solution needs to be business-friendly and should support fact-based decisions.</p><p>To leverage AI &amp; ML successfully, a few aspects should be considered</p><p>• Clearly defined Business problem with the proper target outcome</p><p>• Integrate predictive scores into the core system with detailed functional design</p><p>• Technical detail design that includes data acquisition, model building, and core system integration</p><h3><strong>Our Current Problem statement</strong></h3><p>Looking at the above facts and changing economies, Insurance companies have realized subrogation can be a carrier’s leading revenue stream during periods of difficult financial markets. Post-Covid, the economic slowdown in the auto insurance sector and particularly on uninsured claimants, can be observed. In the USA itself, 32+ million uninsured vehicles have a 35x spike in subrogation workload leading to 15% missed opportunities of subrogation (deteriorating) &amp; 32% of recoverable paid claims are lost. If anyone of us gets into a situation when we just know the accident wasn’t our fault, but we couldn’t really do anything, even though it was the opposite party’s fault? This is exactly where we could benefit from something we call Subrogation in Insurance. The opportunity to pursue subrogation occurs in less than 10% of claims. Subrogation remains a manual process for most insurers, resulting in missed clues and costing some claims departments up to two percentage points of loss ratio.</p><h3><strong>Why do we need Subrogation?</strong></h3><p>Automated subrogation settlements around the world are drastically increasing, with countries such as the UK and Australia having automated settlement rates as high as 40%. However, the US automated settlement rate is between 10 and 15%, low compared to its global counterparts.</p><p>An Intelligent system driven by AI &amp; ML will raise alerts to adjusters for those claims that are most likely to have a subrogation opportunity and provide an estimate of the recoverable amount with integration in the claims processing workflow. This new information would prevent adjusters from closing claims flagged by predictive models and refer them to subrogation professionals. Finding and recovering lost subrogation opportunities may appear to be negligible when compared to all other losses and expenses — yet this can result in a significant increase in underwriting profit. Some insurance companies add the deductible amount, too, in the case of a subrogation. So, in case of such a situation where the damage is done by the third party, people get their claim amount plus the deductible once the third party pays the compensation to the insurance company.</p><p>The solution is based on Machine learning and will lead to: -</p><p>· Elimination of leakage associated with subrogation identification.</p><p>· Reduce false positives up to 36%.</p><p>· Enable productivity gains up to 20%.</p><p>· Improve cycle time by over 30%.</p><h4><strong>The overall Benefits to Customers &amp; Insurer will be: -</strong></h4><p>· Reduced fraud</p><p>· Improved customer retention</p><p>· Improves customer trust and experience</p><p>· Increased customer engagement</p><p>· Improves employee productivity</p><p>· Reduced costs and response times</p><h4><strong>A real-life example, a core aspect of this use case</strong></h4><p>To understand things easily, connect them with a story or an example. Suppose one fine day, your car happens to hurt after a reckless driver bumps into your car. Now, the back of your car is damaged, and this guy doesn’t even seem to accept his fault. You have no time to fight this out or argue further, so you move on with your damaged car and instead get your comprehensive car insurance to pay for the damages. Your insurance company here will hold the third-party responsible for the damage and reimburse both the amount of money you spent from your pocket plus the amount of money your insurance company paid for due to the damages and losses caused.</p><h4><strong>On the above aspect, how can analytics contribute to the value chain ..?</strong></h4><p>Our initial approach involves a standard model of insurance for accident risk and is supported by the ability of insured individuals to bring suit for harm that they suffer. We assume that identical risk-averse individuals face a chance of suffering harm from an accident; that they purchase insurance policies that maximize their expected utility, and that if an accident occurs, they will be able to sue a potentially liable party for compensation, where the suit would involve a litigation cost and result in success only with a probability.</p><h3><strong>Heuristic Model</strong></h3><blockquote>Notation</blockquote><p>y = initial wealth of an individual</p><p>U(∙) = utility of an individual from wealth, where U′ &gt; 0 and U″ &lt; 0</p><p>p = probability of an accident; p 0 (0, 1)</p><p>h = harm if an accident occurs; h &gt; 0</p><p>k = cost of bringing a suit; k ≥ 0</p><p>q = probability of winning a suit, resulting in an award of h; q 0 [0, 1].</p><p>π = insurance premium</p><p>c = insurance coverage c ≥ 0</p><p>N = instruction whether the insurer sues under a subrogation provision; if N = 0, suit is not brought; if N = 1, suit is brought</p><p>s = insured’s share of the award h under a subrogation provision if a suit is successful; s 0 [0, h]</p><p>Under a pure insurance policy, the premium constraint is</p><p><strong><em>π = pc</em></strong></p><p>If the insured would bring a suit in that case the expected utility would be</p><p><strong><em>EU = (1 — p)U(y — π) + p[q U(y — π + c — k) + (1 — q)U(y — π — h + c — k)]</em></strong></p><h3><strong>Analytical Approach</strong></h3><p>Our next approach to Machine learning will make use of text data and Algorithms. This will help to lower fraud, detect semantic relation patterns, and automatic categorization. It enables us to prioritize claims by scoring claims files as High, Medium, or Low based on recoverability likelihood. The scored cases will be sent to appropriate investigation and recovery experts. All this ensures that subrogation actions will be better informed and help assist insurers to eliminate instances of false or excessive pay-outs in the event of major claims. The goal is to learn pattern characteristics for claims with high subrogation potential through the analysis of textual notes of historical claims with known outcomes. To perform a joint analysis of patterns extracted from textual notes and structured attributes associated with the claim, a model is developed for predicting the subrogation potential of each claim. The created model enables the automated ranking of claims with respect to their subrogation potential. The ability to focus on claims with the highest subrogation potential enhances the efficiency of the work of human analysts and increases the recovery rate. An automated subrogation prediction system ensures the consistency of the analysis across all claims and across time. It helps minimize the number of missed subrogation opportunities. A good subrogation prediction model can discover a significant number of good subrogation candidate claims, which may be missed in the manual analysis.</p><figure><img alt="Process flow on how &amp; when each step will get executed" src="https://cdn-images-1.medium.com/max/1024/1*Mdc_g5U7yno8CTT97OnZAA.png" /><figcaption>Standard low of our approach</figcaption></figure><h4><strong>Superior Data Integration</strong></h4><p>Pulling out data from different sources and transforming it to identify claims with the highest potential for subrogation recovery.</p><p>Variables we can create or use for the model:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*ODNTLTyLtAZSBAC2mlWRgA.png" /><figcaption>Few Variables we can create /use for our Analysis</figcaption></figure><p>Displaying the columns and data from the sample dataset. The data is at the claim id level from 2019 to 2022 with 48 features. (This is just a mock-up dataset created from open sources)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/678/1*G-bg_bTEt6Jz0e0bFtahQg.png" /><figcaption>Mock Dataset</figcaption></figure><p>Let’s have a look at a sample of our unstructured data</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/805/1*h0RpEa5Z0I4l0x1loB5XzQ.png" /><figcaption>Mock Dataset</figcaption></figure><p>We will augment our claims text data to uncover important aspects of the uninsured or underinsured claimant and thus use variables to build a machine learning model to understand the likelihood of paying the subrogation. Looking at the data, we can easily identify some of the keywords directed toward a potential accident.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*J_3-DnCgwj9R7WJxpWJZUQ.png" /><figcaption>Word cloud of most frequently appearing word during Subrogation claims</figcaption></figure><p><strong><em>We will move ahead with only unstructured data at the moment</em></strong></p><h3>AI/ML Model Build and Explainability (XAI)</h3><p>We are now ready to begin modeling. The approach we like to take is to build a simple model as a baseline right away, then use that Model to compare more complicated methods. If we can’t achieve significant improvement over the baseline, it isn’t fruitful to use more complex models.</p><p>Here, we will start with the Logistic Regression model &amp; some hyperparameters to see the performance. From that result, we’ll move to BERT and check if we have a superior model to play around with.</p><p>Our models work by collecting a plethora of different data inputs ranging. These inputs are then run through the models to detect anomalies within the claim, and also, we can apply the ran business rule to detect if high subrogation instances occurred automatically.</p><h4>a. Logistic Regression</h4><p>The Algorithm commonly deals with the issue of how likely an observation is to belong to each group. This model is commonly used to predict the likelihood of an event occurring with the logit function. This is a useful model to take advantage of for this problem because we are interested in predicting whether a claim is for subrogation (1) or not (0). Logistic regression predicts the outcome of the response variable (turnover) through a set of other explanatory variables, also called predictors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*31hVdGVLu0UbkYc8C_cYxQ.png" /><figcaption>Initial Model</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*nDlERk30gGRZtVNoEa1Rdw.png" /><figcaption>LR Output</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*8ZHbqDFnFdo-DMI6d20H8Q.png" /><figcaption>Model performance metrics (ROC)</figcaption></figure><p><strong>b.</strong> <strong>BERT (Bidirectional Encoder Representations)</strong></p><p>This deep learning model has given state-of-the-art results on a wide variety of natural language processing tasks. It stands for <strong>Bidirectional Encoder Representations for Transformers</strong>. It has been pre-trained on Wikipedia and BooksCorpus and requires (only) task-specific fine-tuning. It is basically a bunch of Transformer encoders stacked together (not the whole Transformer architecture but just the encoder). The concept of bidirectionality is the key differentiator between BERT and its predecessor.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/1*DeZh6OhzkMZsA3-TpzY1lw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/1*MJwP0xJUdPVjElF7x9-MmQ.png" /><figcaption>BERT Output</figcaption></figure><p>Each line of the dataset is composed of the Claims report text and its label. Data preprocessing consists of transforming text to BERT input features: <em>input_word_ids, </em><em>input_mask, </em><em>segment_ids/input_type_ids</em></p><p>· <strong><em>Input Word Ids:</em></strong><em> Output of our tokenizer, converting each sentence into a set of token ids.</em></p><p><em>· </em><strong><em>Input Masks:</em></strong><em> Since we are padding all the sequences, it is important that we create some sort of mask to make sure those paddings do not interfere with the actual text tokens. Therefore we need a generate input mask blocking the paddings. The mask has 1 for real tokens and 0 for padding tokens. Only real tokens are attended to.</em></p><p><em>· </em><strong><em>Segment Ids:</em></strong><em> For our task of text classification, since there is only one sequence, the segment_ids is essentially just a vector of 0s.</em></p><h4>Explainability Of BERT</h4><p>If we assume that our model can understand the context of the text very well with respect to what we are trying to predict, it will be wonderful. So, when we use models like BERT to understand the context of our text columns and investigate what aspect of language BERT is learning. It helps in verifying the robustness of our model. We will know what words are deemed important by our model in our text with models like BERT.</p><p>Let&#39;s look at a simple method of attribution approach, It scores the input data based on the predictions our model makes, i.e., it attributes the predictions to its input signals or features, using scores for each feature.</p><p>Integrated Gradients use the approach of <strong><em>feature * gradient</em></strong>. The gradient is the signal that tells the neural network how much to increase or decrease a certain weight/coefficient in the network during backpropagation. It relies heavily on the input features to do so. Therefore, the gradient associated with each input feature with respect to the output can help us get a clue about how important a feature is. Integrated Gradients allow us to attribute the selected output feature from the BERT model to its inputs.</p><p>We can generate word importance for each of the output features as below:-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/439/1*dDJw-7FVM4KNf2yOD8ai1Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*yciYpVxuMYj2tfMexHE-Qw.png" /><figcaption>This allows us to understand which words led to the feature (which is one of the feature vectors from BERT) they’re looking at.</figcaption></figure><h3><strong>How do we move forward from here .?</strong></h3><p>We would like to integrate various data sources and research possible ways to improve the model performance and get results closer to real-life scenarios. By using Google Street View (based on Collison date time &amp; location) &amp; Digital Identity Mapping for the Claimant. The model performance will be compared w.r.t holdout data and gauged the performance against one another to determine the most trustworthy predictors of claim subrogation.</p><p>At an enterprise level, we need to work with leaders &amp; Experts from the claims department to introduce our model output into the claims process. It is usually for insurers to assign alerts to adjusters and to the subrogation specialists when the probability of subrogation exceeds a predetermined limit. Claims with slightly lower probabilities (5–20%) will usually be assigned a “watch” status, while claims with even lower probabilities (most claims) will get no message because a message on these claims is often seen as a distraction.</p><p>An API will be created to score claims, claims center will use the HTTPClient to call the REST Service. Also, configuration needs to be done for triggering events that initiate these calls.</p><p>Please feel free to share your opinions and thoughts.</p><p>Happy learning!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=766dcfc1bd47" width="1" height="1" alt=""><hr><p><a href="https://pub.towardsai.net/machine-learning-artificial-intelligence-to-reduce-financial-crimes-766dcfc1bd47">Machine Learning &amp; Artificial Intelligence to Reduce Financial Crimes</a> was originally published in <a href="https://pub.towardsai.net/">Towards AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Learning & Artificial Intelligence add new wings to Amusement Parks]]></title>
            <link>https://medium.com/engineered-publicis-sapient/machine-learning-artificial-intelligence-add-new-wings-to-amusement-parks-28ad7c55b876?source=rss-8a4ee0427b4e------2</link>
            <guid isPermaLink="false">https://medium.com/p/28ad7c55b876</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Himanshu Swamy]]></dc:creator>
            <pubDate>Thu, 01 Oct 2020 10:09:56 GMT</pubDate>
            <atom:updated>2020-10-01T10:09:56.218Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Amusement Parks Business Overview</strong></p><p>In the global entertainment and leisure industry, amusement parks play a vital role. These recreational facilities offer a wide variety of entertainment options and act as a one-stop-shop solution. Based on industry speculations, the global amusement parks market is expected to recover and grow at a CAGR of 9% from 2021 and reach $89.5 billion in 2023. It seems North America accounts for 33% of the market share in 2019, followed by Asia-Pacific with 29%. For years, there has been a relatively stable share of adults, teens and children to theme parks. A growing population resulted in the growth of the market, but the same visitors may be going back to theme parks year after year. The issue lies in a theme park’s ability to not only capture new visitors, but also recapture lapsed ones. Without them, the market may not reach its full potential. It has been observed that parks that are reaching capacity on their busiest days should also consider how they can have smooth attendance throughout the year. This is possible with simple applications of Machine Learning and Artificial Intelligence.</p><p>Machine Learning and Artificial Intelligence have found their way in different areas in the entertainment industry. Some theme parks have given the vision for the future which involves introduction of AI robots of their most beloved cartoon characters, wrist-band (a park-wide pass) that tracks customers’ movements, analyzes purchasing habits, and reports real-time data, recommendation engines, content creation and delivery to predict and adapt according to consumer tastes, preferences to delight users with every interaction. It recognizes patterns of good stories and predicts what will resonate with consumers, programs that can analyze viewer facial expressions to predict engagement with content and Deep Learning speech animation processes. The use of Virtual and Augmented Reality technologies within amusement parks provides an immersive experience to customers. However, in order to make all of this a success, the major questions to be answered are: Where is the largest and fastest growing market for amusement parks? How does the market relate to the overall economy, demography and other similar markets? What forces will shape the market going forward? That is, characteristics, segments, competitive landscape, market shares etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*MCY_pdnR3NBJwXWGW1DL5A.png" /><figcaption>Amusement Park</figcaption></figure><p><strong>Problem Statement</strong></p><p>Following on this conversation, if we think through one of the major concerns for our client in the Leisure &amp; Entertainment industry was: how will be the business post COVID? How many customers are expected to visit? Based on the information w.r.t visitations to the theme parks, informed decisions around marketing efforts, improvement in the efficiency of a supply chain system, revenue and sales prediction, inventory and resource optimization, planning for park operations can be made.</p><p><strong>Why is there a need for Forecasting?</strong></p><p>Footfall forecasting is key for parks to understand the number of visitors in their parks during any given time period. From this data, parks can obtain and comprehend information about the number of people physically in the parks and the number of people leaving without converting to a specific goal. With the integration of other customer-associated data, including demographics, pathway and interest zones, the marketing strategy and parks performance can be substantially enhanced. Besides increasing the conversion ratio, other results to be achieved are:</p><p>· Customer flow management</p><p>· Identifying customer trends</p><p>· Deciding on the best possible opening hours</p><p>· Improving staff planning and optimizing employee expenditures</p><p>· Interpreting the results and effects of promotions or marketing campaigns</p><p>· Endogenous and exogenous factors that will have an effect on customer footfall</p><p>· Look at the busiest hours to optimize the relationship between customer and staff</p><p><strong>Core aspects of this use case</strong></p><p>In Leisure &amp; Entertainment, the mechanisms of thought are often distributed. Historical data plays a vital role. However, at the heart of this view is the fact that where the causal contribution of certain internal elements and the causal contribution of certain external elements are equal in governing behavior. The expected demand can be a function of different kinds of structural variations like trend or seasonality. Regular demand patterns of customers may be affected by price variations and weather. Demand peaks may occur due to promotions and holidays. The seasonality in the daily customer traffic and the effects of holiday at different locations can be used.</p><p><strong>Our approach</strong></p><p>In this case study, we have analyzed the amusement park’s data to understand how historical footfall, promotion, revenue channels, climate variability, digital interactions, events, holidays and economic factors effect customer visitation to parks, and how we can use these features to forecast future footfall. Setting up an appropriate validation framework is extremely important. It enables you to try and experiment various models and objectively compare them. Lag-based features are very useful in providing trends information about the time series data.</p><p>Keeping the above in mind, we started exploring the most popular methods for Multivariate forecasting like Vector Autoregression Moving-Average with Exogenous Regressors (VARMAX), Facebook’s Prophet and Seasonal Autoregressive Integrated Moving-Average with Exogenous Regressors (SARIMAX).</p><p><strong>Data Understanding:</strong></p><p>There are 5 areas of data that impact this problem:</p><p><strong>Internal Data</strong>: Data contained in an EDW, which contains sales history (i.e. tickets purchased, FNB purchased, customer purchase history)</p><p><strong>Digital Data</strong>: This includes data from online activities of visitors</p><p><strong>Economic Data</strong>: Data available that is external to the organization including oil price, Consumer Price Index and other non-traditional data sources</p><p><strong>Environmental Factors</strong>: Data on climatic conditions for specific country like temperature, day length</p><p><strong>Holiday &amp; Events</strong>: Data on vacations, weekends and events for specific countries</p><p>Displaying the columns and data from the dataset. The data is at a daily level from 2014 till 2019 with 13 features. For time series data set, it is imperative to set the datetime as the index for the dataset:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3ghypucjcWWtrSMu3rOCUA.png" /><figcaption>*** <strong>This is just a sample dataset which is very neat, a little modification is required</strong></figcaption></figure><p><strong>Combining Data and Final Visualizations</strong></p><p>This section displays the behavior of each data feature. It ends with a brief discussion on how we selected features and combined the various data sources. The external data was merged with the existing data using the appropriate lag times. In cases where the external data is monthly or weekly, we use linear interpolation to create daily values to be able to combine the data. Note that there are possibly better interpolation approaches, and that could be an area of improvement in the future.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vhn2eKff6bv1EWfQtxXrsg.png" /><figcaption>Features over time</figcaption></figure><p>Clearly, there are some issues when the data is grouped this way. In particular, there are several series that seem to have little or no recent data; those will present a problem in the model. We generated a summary by each variable to investigate those that have very unusual time history. It can be seen that for the purpose of this analysis, Promotion Driven Visitors and New Online Users are essentially irrelevant, so we won’t include them.</p><p>Also, FNB Spend seems to have low sales and almost nothing on some days, so we exclude it as well, at least initially. Note for the purpose here, we will press on to some modeling. But before that, let’s look at our target variable as well.</p><p>Although we can see a fairly clear trend in historical footfall, the data is noisy, and it’s not evident there are patterns that can be modeled. There are big dips at the end of each year as well as some possibly recurring patterns within the year. We chose to move forward with modeling the smoothed data as the target output. Stationary is a very important factor on time series. To check if the data is stationary, we will use Augmented Dickey-Fuller test. It is the most popular statistical method to find if the series is stationary or not. It is also called Unit Root Test. As we know, stationary time series don’t have change mean or variance over time.</p><p><strong>Modeling</strong></p><p>We are now ready to begin modeling. The approach we like to take is to build a simple model as a baseline right away, then use that to compare more complicated methods. If we can’t achieve significant improvement over the baseline, it isn’t fruitful to use more complex models. Here, I will build a simple <strong>FB Prophet</strong> model with some hyperparameter tuning to see if that improves the model. From that result, we’ll move to a <strong>VARMAX </strong>and <strong>SARIMAX</strong> to try and improve the model performance.</p><p><strong>a. FB Prophet Method</strong></p><p>First, we experimented with Prophet along with additional regressors. Our goal was to check how extra regressor would weigh on forecast calculated by Prophet. Prophet is a procedure for forecasting time series data based on an additive model, where non-linear trends are fitted with yearly, weekly and daily seasonality, plus holiday effects. The Prophet uses a decomposable time series model with three main model components: trend, seasonality and holidays. Additional regressors can be added to the linear part of the model using the add_regressor method or function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*gT_EdDXTiBhzsdPIvGIQOA.png" /><figcaption>Formula</figcaption></figure><p><strong>Forecast and Estimation</strong></p><p>We have used 10 exogenous series along with historical footfall data at daily level:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*besiKzs2RemwYEKz0NVwpg.png" /></figure><p>Prophet gives us upper and lower bounds, so we can make plans based on these values as well. It also decomposes output to trend and seasonal components. You can see the filtered output below, and upper and lower bounds are removed for better transparency. Our forecast is plotted in the graph below. If you look carefully, the red line (our prediction) leads the blue line (actual). This is not good. It means our forecast is always ahead of reality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ac8JLC3_CCBsFpcHwCQ0UA.png" /><figcaption>Comparison of Actual vs Forecast footfall</figcaption></figure><p>In terms of the evaluation metric, the root mean squared error (RMSE) was used to assess the predictive power of each of the approaches.</p><p>By analyzing these results, we can conclude that, in general, the best size for the historical window to use is 30 days. In fact, with this value, the results obtained are generally better in terms of RMSE. We can also see that Prophet failed to produce good forecasts for the datasets used with RMSE = 39 %. It did not capture the variation for lower values and certain peaks were also missed. Both time series components and features are key to interpreting the behavior of the time series.</p><p><strong>b. VARMAX Method</strong></p><p>One of the most well-known and widely used families of time-series models includes the Vector Autoregression Moving-Average with Exogenous Regressors (VARMAX). It is a combination of VAR and VMA and a generalized version of ARMA model for multivariate stationary time series. The VARMAX model is generically specified as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*I1EdulYAw97I1Sf5vyx7Ng.png" /><figcaption>Formula</figcaption></figure><p>We have used 10 exogenous series. Note that we needed to allow for more iterations than the default (which is maxiter=50) in order for the likelihood estimation to converge. This is not unusual in VAR models which have to estimate a large number of parameters, often on a relatively small number of time series.</p><p><strong>Forecast and Estimation</strong></p><p>We observe that VARMAX is able to capture the variations on smaller peaks but couldn’t capture high peaks. This model has better approximated the real values with RMSE = 28% when altered certain hyperparameters. We’ve given our model the ability to course correct a bit by allowing it to consider the magnitudes and directions of its errors. This comes at the expense of a longer model estimation process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2F2Rj4rpWyrkkDH9gfdlzg.png" /></figure><p>From the estimated VARMAX model, we have plotted the response functions of the endogenous variables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EIUJyGCoqwqLK5dIbI_pYg.png" /><figcaption>Comparison of Actual vs Forecast footfall</figcaption></figure><p>Just like with Prophet model, the only flaw we noticed here, it is not supporting the intercorrelations between multiple variables to forecast some output.</p><p><strong>c. SARIMAX Method</strong></p><p>The Seasonal Autoregressive Moving Average with Exogenous Input, or SARIMAX model is an approach for modeling time series data that may contain trend and seasonal components. This model has hyperparameters that control the nature of the model performed for the series. We are experimenting SARIMAX with Exogenous factors that will have an effect on the time series prediction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EltKG_82O7I6A97tkBJnDQ.png" /><figcaption>Formula</figcaption></figure><p><strong>Forecast and Estimation</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*7Z98OJoq2hTzCJ4kLmxKhw.png" /></figure><p>The inclusion of weekly seasonality and holiday effects at different time periods revealed that SARIMAX models are better frameworks. Over here SARIMAX models the seasonal element in Multivariate data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w577ADE81QFwB9axbWiaKw.png" /><figcaption>Comparison of Actual vs Forecast footfall</figcaption></figure><p>With Model Extension Framework, we quickly fine-tuned parameters and compared the results against the one from Prophet and VARMAX with minimal parameter setting. We found that the SARIMAX with fine-tuned parameters produced better results for this data.</p><p>That said, SARIMAX seems to be the best performing methods overall, with RMSE = 24%.</p><p><strong>Evaluating the models</strong></p><p>We have use Root Mean Square Error (RMSE) for evaluating the model’s performance. It has been observed that on long time scales, the models are not yet predicting all the behaviors seen in the target data, although most of the large changes are modeled fairly well. The table shows that even the simple Prophet model does better on the periods targeted, and the SARMIAX improves on that by multiple percentage points.</p><p>Results indicate that, although a one-size-fits-all approach does not exist, traditional statistical method such as SARIMAX overall prevails over other approaches.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*4mMOGaA8OPNUGNFp32IiiA.png" /><figcaption>Performance of all 3 Models</figcaption></figure><p><strong>Conclusions and Next Steps</strong></p><p>We have seen that FB Prophet model, VARMAX and SARIMAX can be effective to model complex time-series data. Advantages of this approach include the ability to integrate business and external factors as well as historical data as predictors. With all the strong hypothesis made throughout the document, the final forecast is good and describes the series accurately.</p><p>Tracks of improvement to this method could be investigated further. It is clear that there are processes that are not yet incorporated into our model. For example, there may be additional factors that could be incorporated as external data that drives visitors like hotel bookings, incoming flights, school holidays. In addition, we believe some of the large swings at mid-year cycles are due to business practices–as half-year targets approach, sales is pushed to sell more tickets or pull in opportunities. There may be customer incentives in play to drive some of that. These factors can be integrated if a model of the actual business practices can be defined.</p><p>Another area of work would be to investigate and improve modeling of components of the forecast, such as modeling individual feature. Although the model is built with that level of granularity, we may have noticed in the baseline model that some coefficients are negative. I think that is due to interactions or other factors unaccounted for. But it means that the model may not work well if used to predict one category. This could be tackled by modeling each desired category separately, and then combining those models.</p><p>Please feel free to share your opinions and thoughts.</p><p>Happy learning!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=28ad7c55b876" width="1" height="1" alt=""><hr><p><a href="https://medium.com/engineered-publicis-sapient/machine-learning-artificial-intelligence-add-new-wings-to-amusement-parks-28ad7c55b876">Machine Learning &amp; Artificial Intelligence add new wings to Amusement Parks</a> was originally published in <a href="https://medium.com/engineered-publicis-sapient">Engineered @ Publicis Sapient</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Deploy a Logistic Regression model on GCP]]></title>
            <link>https://medium.com/analytics-vidhya/insiders-view-on-logistic-regression-and-how-do-we-deploy-regression-model-in-gcp-as-batch-c62a64563210?source=rss-8a4ee0427b4e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c62a64563210</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[logistic-regression]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Himanshu Swamy]]></dc:creator>
            <pubDate>Mon, 05 Aug 2019 19:35:50 GMT</pubDate>
            <atom:updated>2019-08-06T12:28:02.341Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>How to Deploy a Logistic Regression Model in GCP</strong></h3><h4>An Insider’s View on Logistic Regression and How do we Deploy a Logistic Regression Model in GCP as a Batch prediction…</h4><p>Over the past two decades Machine Learning has become one of the mainstays of information technology and with that, a rather central, albeit usually hidden, part of our life. With the ever increasing amounts of data becoming available there is good reason to believe that smart data analysis will become even more pervasive as a necessary ingredient for technological progress. This post will give an inside look into Logistic Regression and a problem that most businesses face: <strong>predicting customer default</strong>.</p><h3><strong>Introduction</strong></h3><p>Logistic Regression is a type of regression technique used to study the relation between a dependent and one or more independent variables, when the dependent variable is <strong>categorical.</strong> Like all regression analyses, the logistic regression is a predictive analysis. Logistic regression is used to describe data and to explain the relationship between one dependent binary variable and one or more nominal, ordinal, interval or ratio-level independent variables.</p><figure><img alt="Sigmoid or Logistic Curve" src="https://cdn-images-1.medium.com/max/714/1*HS0bPOWIlci3z2_jogZMwg.png" /><figcaption>Sigmoid or Logistic Curve</figcaption></figure><p><strong>Logistic</strong> <strong>Regression Equation</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NdYsmT9ygeASFDo60rjmjg.png" /></figure><p><strong>Types of Logistic Regression</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g08eZkys_dNAAv1rB_4slA.png" /><figcaption><strong>Binary and Multinomial</strong></figcaption></figure><h3><strong>Why Logistic Regression?</strong></h3><blockquote>Why can’t we use OLS Linear Regression technique to model a BINARY Dependent Variable?</blockquote><blockquote>Linear Probability Model is defined as :</blockquote><blockquote>pi =β0 +β1Xi</blockquote><blockquote>where pi =probability of occurrence of event</blockquote><h4>Two main reasons why OLS Linear Regression does not work with a binary target:</h4><ol><li><strong>Technical concern: It Violates Assumptions <em>— </em></strong>A binary (i.e. dichotomous) dependent variable in a linear regression model violates assumptions of:</li></ol><p>— Homoscedasticity</p><p>— Normality of the Error Term</p><p>2. <strong>Fundamental Issue: Bounded Probabilities —</strong></p><p>The Linear Probability Model is given by: pi = β0 +β1Xi</p><p>— If X has no upper or lower bound, then for any value of β there are values of X for which either pi &gt; 1 or pi &lt; 0</p><p>— This is contradictory, as the true values of probabilities should lie within the (0,1) interval</p><p><strong><em>How do we solve the issue of Bounded Probabilities?</em></strong></p><p><strong>2.a) Use Odds instead of Probability of Event — </strong>Odds is defined as:</p><p><strong>Odds </strong>= <strong>pi / 1 − p i</strong> = <strong>probability of event / probability of non-event</strong></p><ul><li>As probability of event ranges from 0 to 1, odds ranges from 0 to ∞</li><li>Transforming probabilities to odds removes the upper bound</li></ul><p><strong>2.b) Take Natural Logarithm of Odds</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*i24Xza8kpGbm4x4TBnxT5g.png" /></figure><h3>Some Key components to remember:</h3><p><strong>1)Sigmoid Function</strong></p><p>1.1) Logistic Regression Model:</p><p><strong>Z = log (p / 1− p) =β0 +β1X1+β2X2…βkXk</strong></p><p>1.2) Probability of Event is therefore estimated from logit (‘model score’) by the following transformation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*SejHY3z7LlezudaJE4ubUg.png" /></figure><p><strong>2) Estimation Method: MLE</strong></p><p>2.1) Construct Likelihood Function, expressing the likelihood of observing values of dependent variable Y for all <em>n</em> observations</p><p>2.2)Create log likelihood function to simplify the equation</p><p>2.3) Choose values of β’s to maximize log likelihood function</p><p><strong>3) Logistic Regression: Some Key Assumptions</strong></p><p>3.1) Dependent variable has to be categorical (dichotomous for binary logistic regression)</p><p>3.2) P(Y=1) is the probability of occurrence of event</p><ul><li>Dependent variable is to be coded accordingly</li><li>For a binary logistic regression, the class 1 of the dependent variable should represent the desired outcome</li></ul><p>3.3) Error terms need to be independent. Logistic regression requires each observation to be independent.</p><p>3.4) Model should have little or no multicollinearity.</p><p>3.5) Logistic regression assumes linearity of independent variables and log odds.</p><p>3.6) Sample size should be large enough.</p><p><strong>4) Odds Ratio</strong></p><p>An odds ratio for a predictor is defined as the relative amount by which the odds of the outcome increase (Odds Ratio &gt; 1) or decrease (Odds Ratio &lt; 1) when the value of the predictor variable is increased by 1 unit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vKhes9OvKq3GQ6-ozBQ6rQ.png" /></figure><p><strong>4.1) Interpretation</strong></p><p>Interpretation of odds ratio depends on the type of predictor: binary or continuous</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*YlarHQgOOkxVKTHoNknJxQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*BZOvuIMqMEu8gLw-2qcSpA.png" /><figcaption>Odds ratio</figcaption></figure><p><strong>5) Known-how of Model Validation</strong></p><p>During the process of model building, the modeler must be constantly concerned with how closely the model reflects the system definition. This process of determining the degree to which a statistical software generated model (based on input data) is an accurate representation of the real world.</p><p><strong>5.1) Why is Validation Needed?</strong></p><p><strong>5.1.1) Generalization</strong></p><p>To ascertain whether predicted values from the model are likely to accurately predict responses on future subjects or subjects not used to develop the model.</p><p><strong>5.1.2) Stability Check</strong></p><p>To test how consistently the model is going to perform over time.</p><p><strong>5.1.3) Robustness Check</strong></p><p>To test whether the model is an appropriate representation of the real world for the stated purpose and whether the model is acceptable for its intended use.</p><p><strong>5.2) Components of Model Validation</strong></p><p><strong>5.2.1) Sampling Strategies</strong></p><ul><li>Sampling strategies are aimed at addressing the uncertainty that can arise in tests using empirical data.</li><li>Examples: Cross Validation, Bootstrapping, Out-of-Sample</li></ul><p><strong>5.2.2) Power-Testing</strong></p><ul><li>Power-testing techniques are aimed at measuring model’s goodness-of-fit.</li><li>Examples: Classification Table, K-S Statistic, AUC and Concordance for a classification model</li></ul><p><strong>5.2.3) Calibration</strong></p><ul><li>Calibration techniques are aimed at assessing how closely the model’s predictions match with the actual (i.e. observed) values.</li><li>Examples: Hosmer-Lemeshow test for a classification model</li></ul><h3><strong>Different Type of Validation Methods</strong></h3><h4>1. Classification Table (Confusion Matrix)</h4><ul><li>2x2 matrix of actual and predicted classes</li><li>Also known as Confusion Matrix or Contingency Table</li><li>Greater the sum of primary diagonal (TP + TN), higher the degree of classification accuracy.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U5vCqoT5eiRr3Z0awcQUFg.png" /><figcaption>Classification/Confusion Matrix</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*FSC00Y-mbduB8Y7shsd3WQ.png" /></figure><h4>2) Concordance and Discordance</h4><ul><li><strong>Concordant : </strong>A pair of an event and a non-event is said to be a concordant pair if the event observation has higher predicted event probability than the non-event observation.</li><li><strong>Discordant : </strong>A pair of an event and a non-event is said to be a discordant pair if the event observation has lower predicted event probability than the non-event observation.</li><li><strong>Tied : </strong>A pair of an event and a non-event is said to be a tied pair if the predicted event probability for both the event and the non-event observations is exactly same.</li></ul><h4>3) Receiver Operating Characteristics (ROC)</h4><p>ROC graph is a 2-dimensional graph in which:</p><ul><li>True positive rate is plotted on the Y-axis.</li><li>False positive rate is plotted on the X-axis.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s2Jl64GbPS5xkhpSUuppBw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*hYjoWXIvSgxT0Ltil9KJfw.png" /><figcaption>ROC Curve</figcaption></figure><h4>4) Gini Coefficient</h4><p>Gini coefficient is a measure of degree of discrimination between goods (non-events) and bads (events).</p><ul><li>Gini coefficient is twice the area between ROC curve and 45° random line of equality</li><li>Gini coefficient varies between 0 and 1</li></ul><p>Gini = 0 implies no discrimination</p><p>Gini = 1 implies perfect discrimination</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*Oh-m3H6LZjiX9cTcjwpX4w.png" /><figcaption>Gini Coefficient</figcaption></figure><h4>5) Kolmogorov-Smirnov (K-S) Statistic</h4><p>K-S statistic is the maximum vertical difference between the cumulative lift curve for events (goods) and the cumulative lift curve for non-events (bads).</p><p>KS curve is shown below. It is drawn by plotting Cumulative % of population. Better the KS, better the model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/386/1*nQTds7bygA7N3cze9D4bfg.png" /></figure><p><strong>Be careful: </strong>— K-S is based on a single point on the good and bad distributions — the point where the cumulative distributions are the most different. It shouldn’t be relied upon without carefully looking at the distributions.</p><h4>6) Decile-wise Event Rate Chart</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*xs05ly_GDCXxSO6ysUcnQA.png" /></figure><p>A decile-wise event rate chart is plotted to gauge if the event rate rank orders well.</p><ul><li>Moving down from Decile 1 to Decile 10, average value of target (i.e. event rate) should ideally fall monotonically.</li><li>Moving down from Decile 1 to Decile 10, average value of target (i.e. event rate) should ideally fall monotonically.</li></ul><h4>7) Hosmer — Lemeshow Test</h4><ul><li>Hosmer - Lemeshow test is a goodness-of-fit test for a binary target variable.</li><li>Unlike many other goodness-of-fit measures, it does not focus on gauging model’s discriminatory power but aims at judging how closely the observed and the predicted values match.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*EObdEIJszF5obDWicvUb7A.png" /></figure><h3><strong>Practical Implementation</strong></h3><p>By now you are aware of the most common and widely used ML Algorithms in the industry. Now let’s apply the ML model on a sample dataset for banking customers and build an end-to-end pipeline in <strong>Google AI platform</strong>.</p><h4><strong>Use Case:</strong></h4><p>The goal is to predict the probability of default based on a loan service dedicated to provided lines of credit (loans) to the unbanked population. Predicting whether or not a client will repay a loan or have difficulty.</p><blockquote><strong><em>The activities around data understanding, cleaning, validation, feature engineering ,model fitting has been separately performed. This part of article is more specific to Model deployment in cloud.</em></strong></blockquote><h4><strong>About the data:</strong></h4><ul><li><strong>SK_ID_CURR — Customer ID</strong></li><li><strong>TARGET — Will Pay or Not</strong></li><li><strong>CNT_CHILDREN — Count of childrens</strong></li><li><strong>AMT_INCOME_TOTAL — Total Income</strong></li><li><strong>AMT_CREDIT — Amount Credited every month</strong></li><li>And other features on customer demographics, Employment and Source of Income.</li></ul><h4><strong>Procedure:</strong></h4><ol><li>Data cleaning: the dataset is very neat, little modification is needed.</li><li>EDA: By looking at the column names, I noticed there are columns with very similar names, which imply a potential multicollinearity problem may exist. So I made some plots of features with similar names, and the plots showed a strong correlation between each other, and that indicates feature selection is needed as the model I intended to use is regression. Here is one plot :</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SJBnPWcfbRz9SpuohtmA9Q.png" /></figure><p>3. Feature engineering: Before fitting my model, there are two things I need to do: remove or combine features and label encoding for my categorical features.</p><p>4. Once the model is finalized with all the necessary validations and we have check the performance of it against the train, test and validation dataset. Now it’s time to deploy in <strong>GCP AI platform as batch prediction</strong>.</p><h3><strong>PROCEDURE</strong></h3><ol><li>Upload the dataset in GCP Bucket along with SQL script. This Sql script will be helpful in creating tables in cloud sql which acts as a data source to Build the model. Prediction on new dataset will be stored as output in cloud sql as well. Please find the below screen-shot.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VhrSBT_Ibv5V19cyl1ZL5g.png" /></figure><p>2. Once we create Database in Cloud Sql , our SQL scripts will create the tables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*k97em0MLo-YagcPXjzrpgA.png" /><figcaption>CREDIT_RISK IS OUR DATABASE</figcaption></figure><p>3. Now we first run our model development script and generate a pickle file. i.e Model_development.py</p><blockquote><strong>The complete script is at </strong>GitHub<strong> please refer at the bottom of the article for the link.</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dQjGMM2nPOnJ0U5f4QqcOQ.png" /></figure><p>4. Now we generate the image of pickle file in AI Platform and create model version. For this we will be using <strong>Bash commands (Refer to GitHub).</strong></p><p>When creating the model version, make sure you specify the directory in your GCP Bucket, where your model.joblib file is in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8WqJNGpVWxRtefaJI6r0ig.png" /><figcaption>Model(Pickle) Image created</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tluXpmeqO3xq1QH3RolqXA.png" /><figcaption>Model Version</figcaption></figure><p>5. Once our model is deployed and ready to use , we will get the below success message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GW9WEn5u6pbEIWOQt5qQqg.png" /></figure><p>6. Our last and final step is to run the deployment script. This will ingest the new data ,make prediction using the model image from AI Platform and store the output in Cloud Sql as shown below.This is how we can get the batch prediction within GCP platform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-MQPLI2AzfTZdbNOUda15w.png" /><figcaption>Deployment Script</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YhSjKw6KJ-EIYTFc1NWdnw.png" /><figcaption>Output within Cloud Console</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aHMuNCzD-5YL83BeCZbFdQ.png" /><figcaption>Sample output In Cloud Sql</figcaption></figure><p>Now that we have set up our model and deployed it with GCP we can run it with the assurance that our user data will be secure.</p><p>Feel free to improvise on this approach. Suggestion are always welcome.</p><p>Full code:- <a href="https://github.com/himswamy/GCP_Batch_Prediction.git">https://github.com/himswamy/GCP_Batch_Prediction.git</a></p><p>Link to the GitHub :- <a href="https://github.com/himswamy/GCP_Batch_Prediction">https://github.com/himswamy/GCP_Batch_Prediction</a></p><h3>Happy Learning!!</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c62a64563210" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/insiders-view-on-logistic-regression-and-how-do-we-deploy-regression-model-in-gcp-as-batch-c62a64563210">How to Deploy a Logistic Regression model on GCP</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>
        <item>
            <title><![CDATA[A Glance at Linear Regression and Execute the Linear Regression model as an API Service]]></title>
            <link>https://medium.com/swlh/a-glance-at-linear-regression-and-execute-the-linear-regression-model-as-an-api-service-ad43b366eb78?source=rss-8a4ee0427b4e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad43b366eb78</guid>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Himanshu Swamy]]></dc:creator>
            <pubDate>Mon, 03 Jun 2019 15:11:06 GMT</pubDate>
            <atom:updated>2019-07-03T05:31:30.530Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello everyone! As Machine Learning and Artificial Intelligence is increasingly adopted by a raft of organizations, the demand for ML &amp; AI professionals is increasing at an astonishing rate making it one of the most sought skill in Tech! People across different disciplines are trying to apply AI to make their tasks a lot easier. For example, economists are using AI to predict future market prices to make a profit, doctors use AI to classify whether a tumor is malignant or benign, meteorologists use AI to predict the weather, HR recruiters use AI to check the resume of applicants to verify if the applicant meets the minimum criteria for the job, etc.</p><p>For anyone who wants to learn ML algorithms but hasn’t gotten their feet wet yet, you are at the right place. The fundamental algorithm that every Machine Learning enthusiast starts with is a linear regression algorithm. Therefore, I shall do the same as it provides a base for us to build on and learn other ML algorithms.</p><p>So, let’s get ourselves accustomed to What Regression is all about!</p><p><strong>Linear Regression</strong> is a statistical method that allows us to summarize and study relationships between a target variable (also known as dependent variable) on one or more predictors (also known as independent variables).</p><p>The objective is to estimate and/or predict the mean value of the dependent variable (continuous) on the basis of the known values of the independent variables. And try to minimize the error as much as possible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*h9jL8DpeRc13E3LBGZ-heg.png" /><figcaption><strong>Linear Regression</strong></figcaption></figure><p><strong><em>Regression Equation</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*wdhvF5Vax_9FULipI4N6Eg.png" /></figure><p><strong>Some of the examples for Linear regression are:</strong></p><p>§ Price of car depends on car weight, fuel efficiency ,manufacturing place and many other features</p><p>§ Average hourly wage depends on education and occupational domain</p><p>§ Imagine you want to estimate the demand of your customer. Or you want to predict the sales of a particular item in the future.</p><p>§ In BPOs/KPOs, we can analyze the relationship between the wait times of caller and the number of complaints</p><p><strong><em>Meaning of Linearity</em></strong></p><p>The term ‘linear’ can be interpreted in two ways:</p><p>§ Linearity in the Variables</p><p>§ Linearity in the Parameters</p><p><strong><em>Examples of Linear Regression Model</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jB1Njzk9WhSRUbQDpPRe4w.png" /></figure><p><strong><em>Examples of Non-Linear Regression Model</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*33rCnqlhCW_eQF9_Rxhqng.png" /></figure><p>The term ‘<strong>linear</strong>’ regression means a regression that is linear in the parameters (β’s). It may or may not be linear in the explanatory variables (X’s).</p><h3>Linear Regression: Key Assumptions</h3><p><strong>Assumption 1: Variability in X feature</strong></p><p><strong>Interpretation</strong></p><p>§ X values in a given sample must not all be the same.</p><p>§ Example: Suppose the modeling data corresponds to a particular year (say, 2018). The ‘year’ variable would take single unique value ‘2018’ for all records. Such a variable won’t add any value in making any prediction.</p><p><strong>Conclusion</strong></p><p>§ No estimation possible for β coefficient</p><p><strong>Assumption 2: Predictor X is non-stochastic</strong></p><p><strong>Interpretation</strong></p><p>§ Values taken by the regressor X are considered fixed in repeated samples. That is, X is non-random</p><p><strong>Conclusion</strong></p><p>§ No serious implication as long as predictor X and disturbance e are uncorrelated, which is yet another assumption of Classical Linear Regression Model</p><p><strong>Assumption 3: Zero mean value of disturbance e=</strong>ε<strong> (Random error term)</strong></p><p><strong>Interpretation</strong></p><p>§ Assumption that E(ei | Xi) = 0 implies that the positive ei values cancel out the negative ei values so that their average or mean effect on Y is zero.</p><p>§ E(ei |Xi)=0 also implies that E(Yi |Xi)=β0+β1X1 +…+βkXk (given that Yi =β0+β1X1 +…+βkXk +ei ).</p><p><strong>Conclusion</strong></p><p>§ No impact on the properties of slope coefficients (β1, β2, …, βk).</p><p>§ If E( (ei | Xi) is a non-zero constant, we get a biased estimate of intercept β0</p><p><strong>Assumption 4: Homoscedasticity</strong></p><p><strong>Interpretation</strong></p><p>§ Given the value of X, the variance of disturbances ei is the same for all observations</p><p>Variance (e<em>i </em>| <em>Xi</em>)= σ 2</p><p><strong>Conclusion</strong></p><p>§ Absence of homoscedasticity implies presence of heteroscedasticity. OLS estimates remain unbiased.</p><p>§ But OLS estimates no longer remain efficient (i.e. there are alternative methods of estimation such as WLS with smaller standard errors) and hence significance tests may not be valid.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*o0kfQQpVZoS99NinZaJwHQ.png" /></figure><p><strong>Assumption 5: No autocorrelation between disturbances (e=</strong>ε<strong>)</strong></p><p><strong>Interpretation</strong></p><p>§ Given any two X values, Xi and Xj (i ≠ j), the correlation between ei and ej (i ≠ j) is zero.</p><p>§ This assumption is more likely to get violated in case of time-series data. Usually, generalized least square (GLS) models are used to tackle this problem.</p><p><strong>Conclusion</strong></p><p>§ OLS estimates remain unbiased.</p><p>§ But OLS estimates no longer remain efficient and hence significance tests may not be valid.</p><p><strong>Assumption 6: Zero covariance between </strong>ε <strong>and X</strong></p><p><strong>Interpretation</strong></p><p>§ X and e are assumed to be uncorrelated, as the definition of PRF requires that X and e have separate (and additive) influence on Y</p><p><strong>Conclusion</strong></p><p>§ OLS estimates not only become biased, but also inconsistent (i.e. as the sample size increases indefinitely, the estimators do no converge to their true population values)</p><p><strong>Assumption 7: n &gt; k + 1</strong></p><p><strong>Interpretation</strong></p><p>§ Number of observations (n) must be greater than the number of parameters to be estimated (k + 1) where k = Number of Independent Variables (X1, X2, …, Xk)Parameters to be estimated include k slope coefficients (β1, β2, …, βk) plus 1 intercept coefficient (β0)</p><p><strong>Conclusion</strong></p><p>§ Regression coefficients can’t be estimated</p><p><strong>Assumption 8: No perfect multicollinearity ( </strong>Inter-correlation analysis and VIF test are popular methods of detecting multicollinearity<strong>)</strong></p><p><strong>Interpretation</strong></p><p>§ There are no perfect linear relationships among the explanatory variables</p><p><strong>Conclusion</strong></p><p>§ Perfect Multicollinearity Case</p><p>− Coefficients are indeterminate and standard errors are not defined</p><p>§ High Multicollinearity Case</p><p>− Estimation of regression coefficients is possible, but standard errors tend to be large</p><p>− Individual variable contribution tends to be less precise as predictors are highly correlated</p><p>− Multicollinearity leads to model over-fitting. The overall measure of goodness of fit can be very high, but the t-ratio of one or more variables may be statistically insignificant.</p><p><strong>Assumption 9: Normality of </strong>ε</p><p><strong>Interpretation</strong></p><p>§ εi follow the normal distribution</p><p><strong>Conclusion</strong></p><p>§ Estimates remain BLUE (An OLS estimator βˆ<em>i </em>is said to be <strong>B</strong>est <strong>L</strong>inear <strong>U</strong>nbiased <strong>E</strong>stimator (BLUE) of β<em>i</em>)</p><p>§ But they are no longer asymptotically efficient (i.e. as sample size grows, estimates are not optimal)</p><h3><strong>Some of the well know Feature Selection /Reduction Methods</strong></h3><p><strong><em>FORWARD</em></strong> (i.e. Forward Selection)</p><p>This technique begins with no variable in the model and then the variables are added one by one to the model based on their F statistics</p><p><em>− For each independent variable, F statistics are computed (reflecting variable contribution to the model)</em></p><p><em>− Variable with the largest F statistic is added to the model if its p-value &lt; defined criteria</em></p><p><em>− Process is repeated until there is no independent variable whose F statistic is more significant than defined criteria</em></p><p><em>− Once a variable is in the model, it stays</em></p><p><strong><em>BACKWARD</em> </strong>(i.e. Backward Elimination)</p><p>This technique begins with all variables in the model and then the variables are deleted one by one from the model based on their F statistics</p><p><em>− For each independent variable, F statistics are computed (reflecting variable contribution to the model)</em></p><p><em>− Variable with the smallest F statistic is deleted from the model if its p-value &gt; defined criteria</em></p><p><em>− Process is repeated until all the variables in the model produce F statistic significant at defined criteria</em></p><p><em>− Once a variable is removed from the model, it is never re-considered for inclusion</em></p><p><strong>STEPWISE </strong>(i.e. Stepwise Elimination)</p><p>This technique is similar to the FORWARD selection technique except that the variables already in the model do not necessarily stay there</p><p><em>− Variables are added one by one to the model and the F statistic for a variable to be added must be significant at the defined criteria</em></p><p><em>− Once a variable is added, stepwise method looks at all the variables in the model and deletes any variable that does not produce an F statistic significant at defined criteria</em></p><p><em>− Variables are thus entered into and removed from the model in such a way that each forward selection step may be followed by one or more backward elimination steps</em></p><p><em>− Stepwise process terminates</em></p><h3><strong>Linear Regression Performance Measures</strong></h3><p>Some of the performance measure are listed below.</p><p>§ MAPE</p><p>§ R2 (R-Square)</p><p>§ Adjusted R2 (R-Square)</p><p>§ Root Mean Squared Error (RMSE)</p><p>§ Coefficient of Variation (COV)</p><p>§ Residual Analysis</p><p><strong><em>Mean absolute percentage error (MAPE)</em> :- </strong>It<strong> </strong>is a statistical measure of how accurate a prediction system is. It measures this accuracy as a percentage, and can be calculated as the average absolute percent error for each time period minus actual values divided by actual values</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*656EglxoL6BfyOThnwHprA.png" /></figure><p>n= Number of fitted points</p><p>y` =Predicted values</p><p>y = Actual values</p><p><strong><em>R2 (Coefficient of Determination)</em></strong></p><p><em>k</em>-Variable Linear Regression Equation</p><p><strong>Observed: </strong><em>Y</em>= β0+ β1<em>X</em>1+…+ β’<em>kXk </em>+e</p><p><strong>Model : </strong><em>Y</em>= β0+ β1<em>X</em>1+…+ β<em>kXk</em></p><p><strong>R2 Interpretation</strong></p><p>Proportion of variation in target variable ( <em>Y </em>) explained by the model ( <em>Y</em>ˆ )</p><p>R2 is a goodness-of-fit measure, which is also known as coefficient of determination</p><p><strong>R2 Definition 1</strong></p><p><em>R</em>2 = <em>ESS / TSS </em>=1− <em>RSS / TSS</em></p><p><em>ESS </em><strong>= ∑ </strong>(<em>Y </em>− <em>Y </em>`) ^2 = Explained sum of square (Also known as Regression sum of Squares</p><p><em>RSS </em>= <strong>∑ </strong>(<em>Y </em>− <em>Y </em>`) ^2 = Residual Sum of Squares</p><p><em>TSS </em>= <strong>∑ </strong>(<em>Y </em>− <em>Y</em> `)= Total Sum of Squares = ESS + RSS</p><p><strong>R2 Definition 2</strong></p><p><strong>R2 </strong>= (<em>correlation</em>(<em>Y</em>,<em>Y</em>))^2</p><p><strong>Adjusted R2</strong></p><p>Adjusted R2 is a modification of R2 that adjusts for the number of explanatory terms in the model</p><p>§ Unlike R2, adjusted R2 increases only if the new term improves the model more than expected by chance</p><p>§ Adjusted R2 can be negative</p><p>§ Adjusted R2 &lt; R2</p><p>Where</p><p><em>R</em>2 =UnadjustedR-Square</p><p><em>n </em>= Number of observations in the sample<br> <em>k </em>= Number of explanatory variables<br> <em>m </em>= 1 if model has an intercept term; otherwise <em>m </em>= 0</p><p>(<strong>Higher R2 and Adjusted R2 values indicate better model performance)</strong></p><p><strong>Root Mean Squared Error (RMSE)</strong></p><p>§ Estimate of standard deviation of the error term.</p><p>§ Calculated as square root of Mean Squared Error (MSE).</p><p>§ Scale dependent metric which does not have standalone meaning.</p><p>§ Used for comparison across models for model selection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/234/1*IGQkzKb066BvpijnYCa52g.png" /></figure><p>where<em>,</em></p><p><em>Y </em>=Observed value</p><p><em>Y </em>` =Predicted value</p><p><em>n </em>= Number of observations</p><p><strong>Coefficient of Variation (COV)</strong></p><p>§ COV is calculated as ratio of RMSE to Dependent Variable Mean, multiplied by 100.</p><p>§ Unlike RMSE, it is a unit-less expression of variation in data</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/244/1*WjLOrhEwGIQBx0QxhtWmyA.png" /></figure><p><em>where</em></p><p><em>RMSE </em>= Root Mean Squared Error</p><p><em>Y ` </em>=Average Value of Dependent Variable</p><h3>Interpreting residual plots to improve your Regression</h3><p><strong>Need for Residual Analysis</strong></p><p>Objective 1: To check whether the residuals are ‘pattern less’ (randomly scattered) centered around zero Method of Analysis: Residual Plot</p><p>Objective 2: To check whether the residuals follow a normal distribution Method of Analysis: Normal Q-Q Plot</p><p><strong><em>Residual Plot</em></strong></p><p>§ A graph that shows the residuals on the vertical axis and the fitted values on the horizontal axis.</p><p>§ If the points in a residual plot are randomly dispersed around zero (horizontal axis), a linear regression model is appropriate for the data, otherwise a non-linear model is more appropriate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/794/1*N0cljVbP5Q-TZq2Xvv6f5w.png" /></figure><p><strong><em>Normal Q-Q Plot</em></strong></p><p>§ Quantile-Quantile (Q-Q) plot is a graphical method for comparing two probability distributions by plotting their quantiles against each other.</p><p>§ Normal Q-Q plot shows the observed quantiles of residuals on the vertical axis and the theoretical quantiles of standard normal distribution on the horizontal axis.</p><p>§ If residuals follow normal distribution, the normal Q-Q plot should be a straight line</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/386/1*EKSLMpjK35XOE-nghBh95A.png" /></figure><p>These are some of the concepts for multiple regression I’ve mentioned. The second part of this article I will walk you through on How we can Execute the Linear regression Model.</p><h3><strong>Linear Regression model as an API Service</strong></h3><p>As a data scientist, Our primary task is to make an impact with our machine learning models. When we start a new project, our primary task is to understand the business requirement, explore the data sources and the key decisions on the technology stacks.</p><p>You start exploring the data and build hypotheses around it. Once you’ve got a full understanding of what data you’re dealing with and have aligned with the client on what steps to be taken, one of the outcomes can be to create a predictive model.</p><p>The process of building a predictive model is iterative until the results are presented and everyone is happy. In order to create an impact this model needs to be executed within client infrastructure. Also, when the model is used by other people you can get the necessary input to improve the model and each step. But how quickly we can do this also depends on complication of infrastructure and how familiar we are with it!</p><p>So in-order to tackle this situation, you need a tool that can fit in the complicated infrastructure, preferably in a language that you’re familiar with. This is where you can use one of the tool known as Flask. Flask is fun and easy to setup. This micro framework for Python offers a powerful way of annotating Python function with REST endpoint. I’m using Flask to publish ML model API that allows you to send data, and receive a prediction as a response.</p><p>For better code maintenance, I would recommend using a separate Jupyter notebook where we will build a simple ML model. The objective of this article is to Execute your model as an API service. So we will not much emphasis on Data cleaning, Feature engineering, Hyper parameter tuning or other core modeling activities.</p><p>For the purpose of demonstration, I will train a simple Linear Regression model to predict the Salary of NBA Player using a Kaggle dataset.</p><h3><strong>To access code and dataset please clone the Git Repo. The csv file has all the required data for this Analysis.</strong></h3><p><a href="https://github.com/himswamy/ml-flask-api.git">himswamy/ml-flask-api</a></p><p><strong>Import all the necessary libraries</strong></p><p>If the libraries are not available on your system please use :</p><p>pip install &lt;Library name&gt;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J5cMDG98vlCjX2PlHMe2yA.png" /></figure><p><strong>Import the Dataset and have look at couple of rows</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tAzQqCgsb2b-K_Sk6M2NLw.png" /></figure><p>Our data is clean and we are assuming that all the data preparation activities are already taken care off , So we directly jump into Model building and Execution task.</p><p><strong>Split the dataset into Train and test</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vLrNCDmw-hDNOR1odewf2Q.png" /></figure><p><strong>Separate the Target and Predictor features</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r351TgA2k6uuFLxEvakW5g.png" /></figure><p><strong>Let’s Build a simple Linear Regression model and generate the pickle for it.</strong></p><p>It is always a good practice to do a test run and check if the model performs well. Construct data frame with an array of column names and an array of data (using new data, the one which is not present in train or test datasets).</p><p>Compare the train and test dataset score, they seems to be close enough.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KDp7jiWXZ63qFffd85X8UQ.png" /><figcaption>Regression Model</figcaption></figure><p>Once the model you have created seems to perform well, you can save it as file. You can then open this pickle file later and call the function Predict to get a prediction for new input data. This is exactly what we will do in Flask.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kKFFDSLiej-3qyNL_1w1mg.png" /><figcaption>Pickle file</figcaption></figure><p><strong>Develop a Flask Api service and Execute it</strong></p><p>Flask runs on a server. This can be in the environment of the client or a different server depending on the client’s requirements. When we are running python flask_app.py , it will first loads the created pickle file. Once this is loaded you can start making predictions.</p><p>While it works to start Flask interface directly in Jupyter notebook, I would recommend to convert it to Python script and run from command line as a service.</p><p>Python script with Flask endpoint can be started as the background process. This allows to run endpoint as a service and start other processes on different ports.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*a7RrTrmag922-lw58n0CAw.png" /><figcaption>Flask_app.py</figcaption></figure><p><strong>Request predictions</strong></p><p>Predictions are made by passing a POST JSON request to the created Flask web server which is on port 5000 by default. In flask_app.py this request is received and a prediction is based on the already loaded prediction function of our model. It returns the prediction in JSON format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*J8lJ_0gq_OEyc2j9PgLWsQ.png" /><figcaption>request.py</figcaption></figure><p>Now, all you need to do is call the web server with the correct syntax of data points. The input dataset should have the same data format and shape on which the pickle file was prepared, to get this JSON response of your predictions.</p><p>For example, the below output:</p><p><strong>python request.py -&gt; &lt;Response [200]&gt; &quot;[2566.74726972]&quot;</strong></p><p>For the data we sent we got a prediction of Salary “<strong>2566.74726972</strong>” as output of our model. Basically, in the above steps we are just sending an array of data to an endpoint. This will transform the data to a Json format. The endpoint reads the JSON post and transforms it back to the original array.</p><p>This simple approach can easily let other people use your machine learning model and quickly make an impact.</p><p>Feel free to improvise on this approach. Suggestion are always welcome.</p><p>Full code:</p><p><a href="https://github.com/himswamy/ml-flask-api.git">https://github.com/himswamy/ml-flask-api.git</a></p><h3><strong>Happy Learning!</strong></h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad43b366eb78" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/a-glance-at-linear-regression-and-execute-the-linear-regression-model-as-an-api-service-ad43b366eb78">A Glance at Linear Regression and Execute the Linear Regression model as an API Service</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>