<?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 Francesco  Franco on Medium]]></title>
        <description><![CDATA[Stories by Francesco  Franco on Medium]]></description>
        <link>https://medium.com/@francescofranco_39234?source=rss-fe44f5fcf1d3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tv95qIzwoUpCIPRHa38M9w.jpeg</url>
            <title>Stories by Francesco  Franco on Medium</title>
            <link>https://medium.com/@francescofranco_39234?source=rss-fe44f5fcf1d3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 04:38:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@francescofranco_39234/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[AI Decision Models: What They Are and How They Work]]></title>
            <link>https://pub.aimind.so/ai-decision-models-what-they-are-and-how-they-work-71483bccd320?source=rss-fe44f5fcf1d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/71483bccd320</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[decision-making]]></category>
            <dc:creator><![CDATA[Francesco  Franco]]></dc:creator>
            <pubDate>Sun, 11 Oct 2026 07:38:42 GMT</pubDate>
            <atom:updated>2026-10-26T15:04:40.620Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j_--V0A3e30tbcUQz2SmPg.png" /><figcaption>Image source:<a href="https://www.kai-waehner.de/blog/2026/09/28/how-system-one-models-like-jev-change-enterprise-ai-architecture/">https://www.kai-waehner.de/blog/2026/09/28/how-system-one-models-like-jev-change-enterprise-ai-architecture/</a></figcaption></figure><p>Recently, the AI industry was surprised by the release of <a href="https://typesafe.ai/blog/introducing-system-one-models-and-jev">Jev by TypeSafe</a> AI, a model called decision-making, meaning it is capable of making decisions given an unstructured input and forcing a structure in the output with a decision and its probability.</p><p>TypeSafe AI inserts Jev into the context of System One, which refers to <a href="https://thedecisionlab.com/reference-guide/philosophy/system-1-and-system-2-thinking">an idea of psychologist and economist Daniel Kahneman</a> published in his classic work <a href="https://www.amazon.it/Pensieri-lenti-veloci-Daniel-Kahneman/dp/8804736127?__mk_it_IT=%C3%85M%C3%85%C5%BD%C3%95%C3%91&amp;sr=8-2&amp;btn_type=ss&amp;btn_ref=srctok-3fd666e725f9d223"><em>Thinking Fast and Slow</em></a></p><p>According to Kahneman,<em> System One</em> is a mode of thinking that is fast, automatic, and intuitive and operates with minimal or no effort. This mode of thinking allows us to make decisions and form judgments quickly, based on patterns and experiences.</p><p>On the other hand, <em>System Two</em> is slow, deliberate, and conscious and requires intentional effort. This type of thinking is used for solving complex problems and for analytical activities that require more reflection and consideration.</p><blockquote><strong><em>The automatic operations of System 1 generate surprisingly complex patterns of ideas, but only the slower System 2 can construct thoughts in an orderly series of steps.</em></strong></blockquote><blockquote><strong><em>Daniel Kahneman in Thinking, Fast and Slow</em></strong></blockquote><p>Jev is therefore trained to make decisions quickly, without deep internal reflection.</p><p>Alongside Jev, other similar models have recently been released, both open and closed source. We will look at some of them in this article.</p><h3>What is a decision model?</h3><p>A decision model is a specialized generative AI model that does not generate free text but instead receives a “state” (text, JSON, or, in some cases, images) plus one or more questions with predefined answers and returns structured output with probability/confidence:</p><ul><li>Choice: Select one from the options given by the developer and assign probabilities to each.</li><li>Yes/no (noun or predicate): returns the probability that a condition is true.</li><li>Score: evaluate on an ordered scale with a weighted average.</li></ul><p>Everything happens in a single pass (non-autoregressive like LLMs), <strong>with no possibility of breaking the output pattern.</strong></p><p>But why did its release generate so much interest in the AI world?</p><p>Before Jev, there were attempts, some very interesting that definitely had a significant practical impact, to achieve the goal that Jev reaches through models like GliNER.</p><p>GLiNER demonstrated that a generative LLM with billions of parameters is not needed to extract arbitrary entities from a text. A compact bidirectional encoder that receives the desired labels along with the text as input and evaluates which portions correspond to which labels is enough. No token-by-token generation, no parsing of fragile outputs: the model directly returns spans and scores, runs comfortably on a CPU, and adapts to new labels without retraining.</p><p>In the same vein, models dedicated to zero-shot classification have emerged, which apply the same principle to the choice between categories defined at the time of inference.</p><p>The most widely used alternative, however, remains relying on a generative LLM. You write a prompt that lists the options, asks the model to respond with a single label, and, in more sophisticated cases, reads the logprobs of the response tokens to derive a sort of confidence. It works, but with several limitations.</p><ul><li>The probability is spread over tokens that are not valid options.</li><li>Labels composed of multiple tokens complicate the calculation.</li><li>Many APIs only expose the most likely tokens or do not expose logprobs at all.</li><li>Worst of all, the probabilities of a model trained to converse are rarely well-calibrated (i.e., when a model trained to converse tends to be overconfident even when it is wrong).</li></ul><p>In addition, there are latency and costs designed for generating text, not for choosing between three alternatives.</p><p>A decision-making model like Jev or<a href="https://fastino.ai/blog/gliner-2-5-decide-open-weight-decision-model"> GliNER Decide</a>, the new iteration of the same model by Fastino Labs, fits exactly into this space. The decision space is declared in the request as a typed schema, and the model returns a probability distribution over those specific options by construction and not by prompt convention.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*h4mdHNFXgjXdZvkEtVPZhA.png" /><figcaption>Image from TypeSafe.ai (<a href="https://typesafe.ai/blog/introducing-system-one-models-and-jev?ref=diariodiunanalista.it">https://typesafe.ai/blog/introducing-system-one-models-and-jev</a>)</figcaption></figure><p>Multiple questions about the same state can be evaluated in a single pass, and the probabilities are intended to be used directly as thresholds in the code. This is the shift in perspective that has attracted attention: <strong>the model that writes the final response is no longer the one that decides what should happen next </strong>(i.e., at the next token, because its purpose is not to generate conversation!).</p><p>Here is an example of input and output toward Jev.</p><p>Input:</p><pre>{<br>  &quot;model&quot;: &quot;jev-latest&quot;,<br>  &quot;state&quot;: &quot;Customer: I was charged twice and nobody has replied for 3 days. I&#39;m furious and thinking of cancelling.&quot;,<br>  &quot;questions&quot;: {<br>    &quot;route&quot;: {<br>      &quot;type&quot;: &quot;choice&quot;,<br>      &quot;instructions&quot;: &quot;Where should this ticket go?&quot;,<br>      &quot;criteria&quot;: {<br>        &quot;billing&quot;: &quot;money, charges, refunds, invoices&quot;,<br>        &quot;bug&quot;: &quot;the product is broken&quot;,<br>        &quot;account&quot;: &quot;login or access problems&quot;<br>      }<br>    },<br>    &quot;urgency&quot;: {<br>      &quot;type&quot;: &quot;score&quot;,<br>      &quot;instructions&quot;: &quot;How urgent is this?&quot;,<br>      &quot;criteria&quot;: [&quot;routine&quot;, &quot;today&quot;, &quot;urgent&quot;, &quot;critical&quot;]<br>    },<br>    &quot;escalate&quot;: {<br>      &quot;type&quot;: &quot;noul&quot;,<br>      &quot;instructions&quot;: &quot;Escalate to a human immediately?&quot;<br>    }<br>  }<br>}</pre><p>Output:</p><pre>{<br>  &quot;model&quot;: &quot;jev-1.13.0&quot;,<br>  &quot;answers&quot;: {<br>    &quot;route&quot;: {<br>      &quot;type&quot;: &quot;choice&quot;,<br>      &quot;choice&quot;: &quot;billing&quot;,<br>      &quot;confidence&quot;: 0.99,<br>      &quot;probabilities&quot;: {<br>        &quot;billing&quot;: 0.99,<br>        &quot;bug&quot;: 0.0,<br>        &quot;account&quot;: 0.01<br>      }<br>    },<br>    &quot;urgency&quot;: {<br>      &quot;type&quot;: &quot;score&quot; ,<br>      &quot;score&quot;: 3.0,<br>      &quot;confidence&quot;: 1.0,<br>      &quot;legend&quot;: {<br>        &quot;0&quot;: &quot;routine&quot;,<br>        &quot;1&quot;: &quot;today&quot;,<br>        &quot;2&quot;: &quot;urgent&quot;,<br>        &quot;3&quot;: &quot;critical&quot;<br>      },<br>      &quot;probabilities&quot;: {<br>        &quot;0&quot;: 0.0,<br>        &quot;3&quot;: 1.0<br>      }<br>    },<br>    &quot;escalate&quot;: {<br>      &quot;type&quot;: &quot;noul&quot;,<br>      &quot;noul&quot;: 0.94<br>    }<br>  },<br>  &quot;usage&quot;: {<br>    &quot;input_tokens&quot;: 62,<br>    &quot;output_tokens&quot;: 0<br>  }<br>}</pre><p>Here is what the fields mean:</p><pre>FIELD                    MEANING<br>|choice                  | The winning option (the key you defined)<br>|probability             | Probability distribution over all options<br>|confidence              | How confident the model is in its choice (useful for thresholds)<br>|score                   | Weighted score (can be fractional, e.g., 2.3)<br>|noul                    | Probability (0–1) that the condition is true<br>|legend                  | Index map → label for the scores<br><br><br></pre><h3>How is a decision model trained?</h3><p>Recent publications have highlighted that these models typically originate from a pre-trained model (a decoder LLM or a bidirectional encoder) and are specialized for a different objective: return typified decisions and calibrated probabilities, not free text.</p><h4>The objective: calibration and typified output</h4><p>The fundamental difference compared to conversational LLMs lies in the training signal and the output contract.</p><ul><li><strong>RLHF</strong> (reinforcement learning from human feedback) optimizes for human preferences → fluid responses but often overconfident.</li><li><strong>RLVR</strong> (reinforcement learning with verifiable rewards) optimizes for verifiable correctness → good for reasoning, but slow.</li><li><strong>RLCD </strong>(reinforcement learning for calibrated decisions, introduced by TypeSafe for Jev) optimizes for epistemically honest probabilities: when the model says 0.8, on average that answer should be correct about 80% of the time.</li></ul><p>TypeSafe has stated that it trained Jev on synthetic data. The goal is for the probabilities to be directly usable as thresholds in the code.</p><h4>Two main families of approach</h4><ol><li><strong>Decision head based on generative model</strong></li></ol><p>You start with an LLM and disable or remove the language generation head and add a decision head (pointer/scoring head). The head evaluates the schema options in parallel and produces a probability distribution. It is fine-tuned with LoRA/QLoRA on labeled decision examples, using loss such as cross-entropy.</p><p>2. <strong>Specialized encoders (GLiNER line)</strong></p><p>Here, we start from compact and non-generative encoder architectures, inspired by GLiNER. The model processes text and question schemas together, produces compatibility scores for each allowed option, and a constrained decoder seeks the highest joint assignment that respects any rules between questions.</p><p>GLiNER2.5-Decide, for example, is a post-trained encoder specifically for structured decision-making. It supports full fine-tuning or LoRA, runs on CPU, and, in addition to typified decisions, can extract spans and relationships and apply constraints between outputs (e.g., “if you detect harm, then safety must be unsafe”).</p><p>On an internal benchmark of 17 classification/routing/triage datasets, it achieves competitive (or superior) results compared to larger baselines based on 4B decoders.</p><p>This second family demonstrates that a generative backbone with billions of parameters is not always necessary: a specialized encoder, trained or fine-tuned on schema-driven decision tasks, can be more efficient and precise on bounded tasks.</p><h4>Typical training schedule</h4><ol><li>Pre-trained base: LLM decoder or encoder (GLiNER-style, ModernBERT, etc.).</li><li>Modification of the output: removal/disabling of the generative head + addition of a decision head (or constrained decoder).</li><li>Data: examples of (state + question schema + correct answers). Often synthetic or labeled on routing, classification, and scoring tasks.</li><li>Loss: cross-entropy (or label-smoothed) on the correct options + calibration components (Brier, proper scoring rules). In some cases, a second-stage RL is oriented toward calibration.</li><li>Fine-tuning: LoRA/QLoRA on the backbone + head training, or full fine-tuning on small models. Post-hoc temperature scaling to further improve calibration.</li><li>Evaluation: accuracy on labels + calibration metrics (expected calibration error) on the held-out set.</li></ol><p>For example, a row of a dataset used for training a decision model might look like this:</p><pre>Part                   Content               Used as<br>|<strong> Input</strong>                | state + questions   | What the model sees<br>| <strong>Label (gold</strong>)         | gold                | The target on which the loss is calculated<br></pre><p>Input:</p><pre>{<br>  &quot;state&quot;: &quot;Hi, I was charged twice for invoice #4411. Please refund the duplicate today.&quot;,<br>  &quot;questions&quot;: {<br>    &quot;team&quot;: {<br>      &quot;type&quot;: &quot;choice&quot;,<br>      &quot;instructions&quot;: &quot;Which team should handle this?&quot;,<br>      &quot;criteria&quot;: {<br>        &quot;billing&quot;: &quot;invoices, payments, refunds&quot;,<br>        &quot;technical&quot;: &quot;bugs, outages, errors&quot;,<br>        &quot;sales&quot;: &quot;pricing, new plans&quot;<br>      }<br>    },<br>    &quot;refund&quot;: {<br>      &quot;type&quot;: &quot;noul&quot;,<br>      &quot;instructions&quot;: &quot;Does the customer ask for a refund?&quot;<br>    },<br>    &quot;urgency&quot;: {<br>      &quot;type&quot;: &quot;score&quot;,<br>      &quot;instructions&quot;: &quot;How urgent is this?&quot;,<br>      &quot;criteria&quot;: [&quot;not urgent&quot;, &quot;soon&quot;, &quot;today&quot;]<br>    }<br>  },<br>  &quot;gold&quot;: {<br>    &quot;team&quot;: {&quot;choice&quot;: &quot;billing&quot;},<br>    &quot;refund&quot;: {&quot;noul&quot;: 0.98},<br>    &quot;urgency&quot;: {&quot;score&quot;: 2}<br>  }<br>}</pre><p>Output:</p><pre>{<br>  &quot;team&quot;: {&quot;choice&quot;: &quot;billing&quot;},<br>  &quot;refund&quot;: {&quot;noul&quot;: 0.98},<br>  &quot;urgency&quot;: {&quot;score&quot;: 2}<br>}</pre><h4>What changes compared to a classic classifier?</h4><p>Unlike a traditional classifier that needs to be retrained from scratch for each set of labels, <strong>a decision model inherits the base language understanding.</strong></p><p>The labels (criteria) are provided at runtime. This allows adaptation to new schemas without complete retraining, provided that the option descriptions are clear. Models like GLiNER2.5-Decide further extend this idea by supporting inter-question and multi-task constraints (decisions + extraction).</p><p><strong>What are the advantages of a decision model compared to a conversational LLM?</strong></p><ul><li><strong>Typed and deterministic output:</strong> no need to parse JSON or handle format hallucinations.</li><li><strong>Speed</strong>: a forward pass instead of a token-by-token generation.</li><li>Low<strong> cost and latency</strong>: small models (300–500M) that also run on CPU.</li><li><strong>Usable probabilities</strong>: you can decide in code whether to trust or escalate.</li><li>F<strong>lexible schema at runtime</strong>: the options are defined in the call, without retraining.</li></ul><h3>Practical applications of a decision-making model</h3><p>It is obvious that a decision model becomes very useful when the software needs to make structured and deterministic decisions quickly without having to generate free text.</p><p><strong>A typical scenario where this becomes evident is the automatic routing and triage of requests (support tickets, emails, user message</strong>s). Here, a detailed response is not needed: it is necessary to understand where to send the request and with what priority in a few milliseconds and reliably.</p><p>Imagine a customer support system that receives free-form messages. Before assigning the ticket to a team or an agent, the system must answer three questions:</p><ol><li>What type of request is it? (billing, technical, account…)</li><li>How urgent is it?</li><li>Should it be escalated immediately to a human?</li></ol><p>An LLM could do it, but:</p><ul><li>takes more time</li><li>can generate useless or malformed text</li><li>costs more per volume,</li><li>still requires code to extract the decision.</li></ul><p>A decision model solves exactly this problem: receives the message and directly returns the three typified decisions.</p><p>Let’s see how to implement it in Python with GliNER Decide, which is open source.</p><pre>from gliner2 import AutoExtractor<br><br># Load the model (one time only)<br>model = AutoExtractor.from_pretrained(&quot;fastino/GLiNER2.5-Decide&quot;)<br><br>def route_ticket(message: str) -&gt; dict:<br>    &quot;&quot;&quot;<br>    Take a message from a client and return<br>    intent, urgency and whether human is needed.<br>    &quot;&quot;&quot;<br>    result = model.classify_text(<br>        message,<br>        {<br>            &quot;intent&quot;: [<br>                &quot;billing&quot;,<br>                &quot;technical&quot;,<br>                &quot;account&quot;,<br>                &quot;shipping&quot;,<br>                &quot;general&quot;<br>            ],<br>            &quot;urgency&quot;: [&quot;low&quot;, &quot;medium&quot;, &quot;high&quot;, &quot;critical&quot;],<br>            &quot;escalate&quot;: [&quot;yes&quot;, &quot;no&quot;]<br>        }<br>    )<br>    return result<br><br><br># Example<br>message = (<br>    &quot;Hi, I was charged twice for invoice #4411. &quot;<br>    &quot;Please refund the duplicate today, this is urgent.&quot;<br>)<br><br>decision = route_ticket(message)<br>print(decision)<br><br><br>&gt;&gt;&gt; {&#39;intent&#39;: &#39;billing&#39;, &#39;urgency&#39;: &#39;critical&#39;, &#39;escalate&#39;: &#39;yes&#39;}</pre><p>In practice, we can create functions for assigning to the various dedicated teams:</p><pre>decision = route_ticket(message)<br><br># Automatic routing<br>if decision[&quot;intent&quot;] == &quot;billing&quot;:<br>    assign_to_team(&quot;billing&quot;)<br>elif decision[&quot;intent&quot;] == &quot;technical&quot;:<br>    assign_to_team(&quot;engineering&quot;)<br><br># Escalation based on the decision<br>if decision[&quot;escalate&quot;] == &quot;yes&quot; or decision[&quot;urgency&quot;] == &quot;critical&quot;:<br>    escalate_to_human()<br>else:<br>    try_auto_resolve()</pre><p>The performance compared to an LLM:</p><pre>Aspect                Decision Model (GLiNER2.5-Decide)    Generative LLM<br>| Response time       |  ~50–200 ms                        | 1–5+ seconds<br>| Output format       |  Typed, always valid               | Text to parse<br>| Reliability scheme  |  Guaranteed                        | May hallucinate<br>| Cost per volume     |  Very low (local)                  | High<br>| Ease of integration |  Directly in the code              | Post-processing needed<br><br></pre><p>Another practical scenario could be document classification.</p><pre>text = &quot;INVOICE 1842\nBill to: Northstar QA\nAmount due: 2,400 USD\nDue: 30 April 2026&quot;<br><br>decision = model.classify_text(<br>    text,<br>    {&quot;document_type&quot;: [&quot;invoice&quot;, &quot;receipt&quot;, &quot;contract&quot;, &quot;resume&quot;, &quot;support_email&quot;, &quot;meeting_notes&quot;]}<br>)<br>print(decision)<br><br><br>&gt;&gt;&gt; {&quot;document_type&quot;: &quot;invoice&quot;}</pre><p>or content moderation</p><pre>content = &quot;Post the customer&#39;s home address in the public thread so everyone can see where the package actually went.&quot;<br><br>decision = model.classify_text(<br>    content,<br>    {&quot;policy&quot;: [&quot;allow&quot;, &quot;personal_data&quot;, &quot;harassment&quot;, &quot;scam&quot;, &quot;violence&quot;, &quot;spam&quot;]}<br>)<br>print(decision)<br><br><br>&gt;&gt;&gt; {&quot;policy&quot;: &quot;personal_data&quot;}</pre><h3>Conclusions</h3><p>Decision models represent a paradigm shift compared to generative LLMs. They are not designed to produce text, but to make typed decisions based on unstructured input, directly returning a choice, a score, or a calibrated probability.</p><p><strong>Key points</strong>:</p><ul><li>They return standardized decisions (choice, score, probability) instead of free text.</li><li>They are faster, more economical, and deterministic than LLMs on routing, classification, triage, and guardrail tasks.</li><li>Calibrated probabilities allow you to decide in code when to trust or escalate.</li><li>They adapt to new label schemes at runtime, without complete retraining.</li><li>They typically originate from a pre-trained backbone + decision head, fine-tuned on synthetic data.</li></ul><p>As AI pipelines and agents become more common, the need for specialized components for operational decisions is set to grow. Decision models are a concrete response to this need, and they open new opportunities for data scientists and AI engineers to optimize solutions primarily based on conversational LLMs.</p><h3>References</h3><ul><li>Sutton &amp; Barto, <em>Reinforcement Learning: An Introduction</em> (2nd ed., 2018). .</li><li>Puterman, <em>Markov Decision Processes</em> (1994).</li><li>Bertsekas, <em>Reinforcement Learning and Optimal Control</em> (2019).</li><li>Levine et al., “Offline Reinforcement Learning: Tutorial, Review, and Perspectives on Open Problems” (2020, arXiv).</li><li>Kahneman &amp; Tversky, “Prospect Theory: An Analysis of Decision under Risk” (<em>Econometrica</em>, 1979).</li><li>Kahneman, <em>Thinking, Fast and Slow</em>.</li></ul><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71483bccd320" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/ai-decision-models-what-they-are-and-how-they-work-71483bccd320">AI Decision Models: What They Are and How They Work</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clustering time series for stock market prediction in Python]]></title>
            <link>https://pub.aimind.so/clustering-time-series-for-stock-market-prediction-in-python-part-1-b1a1c6af1e13?source=rss-fe44f5fcf1d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/b1a1c6af1e13</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[clustering]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[time-series-forecasting]]></category>
            <dc:creator><![CDATA[Francesco  Franco]]></dc:creator>
            <pubDate>Fri, 18 Sep 2026 07:16:27 GMT</pubDate>
            <atom:updated>2026-10-02T13:24:03.077Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_NcFJ0M1TkpFFiByixHrbg.png" /></figure><p>If you are an analyst, regardless of your field of work, you are probably aware that there are many different approaches to stock market forecasting. The most relevant ones today mainly use neural networks applied to sequential data, such as LSTM, or involve sentiment analysis and <em>signal detection.</em></p><p>These approaches are definitely interesting and for a good reason: there are many resources on the Internet that cover the mentioned topics. This is an advantage, as we can start our projects by following a shared baseline and then expand them with our own methodologies.</p><p>Today I want to propose a different approach, the one I literally dreamed about a couple of weeks ago and that made me reflect on the possible opportunities. I’m referring to <strong>time series clustering applied to stock markets.</strong></p><p>My main thought was</p><blockquote>What if you could identify patterns in the data, find similar ones, group them together, and assign investment opportunities to each of them?</blockquote><p>This would allow me to identify relevant sequences that could predict a specific upward or downward trend.</p><p>I dedicated a few hours to this project, and I would like to share with you the first part of this series. As I continue to work on this project, I will share my findings in another article.</p><p>Keep in mind that this is my approach: if you like the general reasoning but would do something differently, let me know with a comment.</p><p>I hope you are as excited as I am about this project. Let’s get started!</p><h3>The intuition behind the project</h3><p>The idea is that if we take a time series, regardless of its nature and context (whether it is a stock price or something else), we can divide it into sequences and study each sequence in isolation.</p><p>This allows us to make hypotheses about these segments and compare them against each other.</p><p>This is the hypothesis for this problem.</p><blockquote>If I divide a time series into equal-sized sequences, I can further divide these sequences into two parts, which I will call A and B. B will always follow A. By studying A, I am able to find common patterns and group such A together. This, then, allows me to calculate the probability that B will follow a similar trend given the preceding segment A.</blockquote><p>The probabilistic approach seems the most suitable to me. We’ll see shortly why.</p><p>So, basically, I want to find sequences that occur with a certain degree of similarity throughout the entire time series. I want to group these similar sequences and see how the following sequence behaves in terms of trend, which is calculated using the slope.</p><p>If we find that some A share a high degree of similarity, we can group them together and find the probability that B has an upward or downward trend by counting how many B have a positive or negative slope.</p><h3>The algorithm</h3><p>Given the explanation above, here is how I designed the algorithm.</p><ul><li>Divide the time series into sequences of length N.</li><li>Divide each sequence into two parts based on size K to create sequences A and B.</li><li>Calculate the similarity S between all sequences of A.</li><li>Group all similar sequences of A based on a threshold.</li><li>For each group G, calculate the probability P of a positive, negative, or stable trend in the sequence B.</li><li>In a new time series, identify a sequence similar to a previously found A to obtain the probability of the subsequent portion B.</li></ul><p>If these steps are not perfectly clear, don’t worry. I will explain each step with a detailed argument and the code.</p><h3>Requirements</h3><p>We will write the logic from scratch in Python, so we will rely on only a handful of libraries.</p><pre># data manipulation<br>import pandas as pd <br>import numpy as np<br><br># viz<br>import matplotlib.pyplot as plt<br>import seaborn as sns<br><br># time and date libs<br>import datetime<br><br># stock data access<br>import pandas_datareader as pdr</pre><p>The only thing worth noting here is that we will use pandas_datareader as the stock market data provider.</p><h3>The time series</h3><p>We will use pandas_datareader to insert the stock data on Apple from 1000 days ago. This will include 689 data points because weekends and other holidays are excluded.</p><p>There isn’t a particular reason why I chose this number; it just seemed like sufficient data. Let’s define a function to retrieve the data.</p><pre>def get_data(ticker: str, start_date: datetime, end_date: datetime) -&gt; pd.DataFrame:<br>    &quot;&quot;&quot;<br>    Get stock data input ticker<br>    &quot;&quot;&quot;<br>    data = pdr.get_data_yahoo(ticker, start=start_date, end=end_date)<br>    return data<br>  <br># get 1000 days of data for Apple starting from today<br>start_date = datetime.datetime.now() - datetime.timedelta(days=1000)<br>end_date = datetime.datetime.now()<br><br>data = get_data(&#39;AAPL&#39;, start_date=start_date, end_date=end_date)<br><br>data.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*7xy_ETBBqI9efXzQGXIQ8A.png" /><figcaption>First rows of our dataset</figcaption></figure><p>We will use the Close column as our time series.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*lYQpQIFEsPxwwPBT7MahfQ.png" /><figcaption>The temporal series visualized</figcaption></figure><p>Let’s proceed now with the division of the time series according to the aforementioned logic.</p><h3>Time series division</h3><p>We will define two functions that will divide the time series first into sequences and then into “internal” sequences, A and B. Here’s an example:</p><pre>s = np.array([i for i in range(30)])</pre><p>We want to take this series and divide it into N segments of equal size. We can achieve this goal in the following way.</p><pre>def split_time_series(series, n):<br>    &quot;&quot;&quot;<br>    Split a time series into n segments of equal size<br>    &quot;&quot;&quot;<br>    split_series = [series[i:i+n] for i in range(0, len(series), n)]<br>    # if the last sequence is smaller than n, we discard it<br>    if len(split_series[-1]) &lt; n:<br>        split_series = split_series[:-1]<br>    return np.array(split_series)</pre><p>If we apply this function to s with n = 6, we obtain a complete divided sequence without discarded elements (because there is no remainder when 30 is divided by 6).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*bykYTbqp8ct_h0xl0xUWvg.png" /><figcaption>Application of the method split_sequence</figcaption></figure><p>Now we need to split each sequence into two parts based on the size K to create sequences A and B. Let’s define two new functions called split_sequence and split_sequences for this.</p><pre>def split_sequence(sequence, k):<br>    &quot;&quot;&quot;<br>    Split a sequence in two, where k is the size of the first sequence<br>    &quot;&quot;&quot;<br>    return np.array(sequence[:int(len(sequence) * k)]), np.array(sequence[int(len(sequence) * k):])<br><br><br>def split_sequences(sequences, k=0.80):<br>    &quot;&quot;&quot;<br>    Applies split_sequence on all elements of a list or array<br>    &quot;&quot;&quot;<br>    return [split_sequence(sequence, k) for sequence in sequences]<br>view raw</pre><p>In split_sequence, the K parameter allows us to control the size of the first sequence. This is similar to the test_size parameter in train_test_split from Sklearn.</p><p>split_sequences, on the other hand, is only responsible for applying the logic to an array.</p><p>Let’s apply it to our example to understand how it works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*l0hevXXBQCZJEziKUxvClw.png" /><figcaption>split_sequences applied to a list</figcaption></figure><p>The sequences A and B are defined as follows:</p><pre>[seq[0] per seq in sss], [seq[1] per seq in sss]</pre><p>So the elements are paired from the first and second lists in the Numpy array.</p><p>Let’s apply all this to our time series.</p><pre>N = 15  # window size --&gt; we can modify this parameter to experiment<br>K = 0.70 # split size --&gt; 70% dei dati è in A, 30% in B<br><br>SEQS = split_time_series(list(data[&#39;Close&#39;].values), N) # crea sequenze di lunghezza N<br><br>SPLIT_SEQS = split_sequences(SEQS, K) # divide le sequenze in due<br><br>A = [seq[0] for seq in SPLIT_SEQS]<br>B = [seq[1] for seq in SPLIT_SEQS]</pre><p>If we take a look at A[0] and B[0], we see the first part of Apple’s closing data, demonstrating that the preprocessing was successful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/769/1*H9-IZ1J82M8zEVd0FrV61A.png" /><figcaption>Shares of Apple stock divided into A and B.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*kpjXykuz6fSgsMtD6KbptQ.png" /><figcaption>Time series of the original Apple stock</figcaption></figure><p>Now we are ready to compare the sequences for similarity.</p><h3>Calculation of similarity</h3><p>This is the third step of the algorithm. To calculate similarity, we will use a combination of Pearson correlation and dynamic time warping (DTW). Correlation helps us match the general direction of trends, while DTW helps us calculate the distance between points. There are many online resources on these two topics, and I suggest doing a more targeted search if you are interested.</p><p>The formula is as follows and is a weighted product of correlation and DWT.</p><p><em>DWT</em>∗(1 — <em>correlation</em>)</p><p>Even though it may not be optimal, it seems to be a functional solution, as you will see shortly.</p><p>The idea here is to create a similarity matrix S to collect pair scores for all sequences belonging to A. Remember that we want to find similar elements in A to calculate the probability of finding a certain B that follows a certain trend.</p><p>The elements that share a similarity above a certain arbitrary threshold will be grouped together. We’ll see that later, though. For now, let’s write the code for calculating similarity.</p><pre>def compute_correlation(a1, a2):<br>    &quot;&quot;&quot;<br>    Calculate the correlation between two vectors<br>    &quot;&quot;&quot;<br>    return np.corrcoef(a1, a2)[0, 1]<br><br>def compute_dynamic_time_warping(a1, a2):<br>    &quot;&quot;&quot;<br>    Compute the dynamic time warping between two sequences<br>    &quot;&quot;&quot;<br>    <br>    DTW = {}<br><br>    for i in range(len(a1)):<br>        DTW[(i, -1)] = float(&#39;inf&#39;)<br>    for i in range(len(a2)):<br>        DTW[(-1, i)] = float(&#39;inf&#39;)<br>    DTW[(-1, -1)] = 0<br><br>    for i in range(len(a1)):<br>        for j in range(len(a2)):<br>            dist = (a1[i]-a2[j])**2<br>            DTW[(i, j)] = dist + min(DTW[(i-1, j)], DTW[(i, j-1)], DTW[(i-1, j-1)])<br><br>    return np.sqrt(DTW[len(a1)-1, len(a2)-1])<br><br><br># create empty matrix<br>S = np.zeros((len(A), len(A)))<br><br># populate S<br>for i in range(len(A)):<br>    for j in range(len(A)):<br>        # weigh the dynamic time warping with the correlation<br>        S[i, j] = compute_dynamic_time_warping(A[i], A[j]) * (1 - compute_correlation(A[i], A[j]))</pre><p>For clarity, we will only print the first two rows of the matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*uH6GBNEcJAZlTotEmfb_vw.png" /></figure><p>As we can see, identical series have a score of 0. Therefore,<strong> similar sequences tend to approach 0 as they become more and more similar</strong>. We plot this with a heatmap.</p><pre># plot heatmap of S<br>fig, ax = plt.subplots(figsize=(20, 10))  <br>sns.heatmap(S, cmap=&#39;nipy_spectral_r&#39;, square=True, ax=ax)<br>plt.title(&quot;Heatmap of sequence similarities&quot;, fontsize=20, fontweight=&#39;bold&#39;)<br>plt.xticks(range(len(A)), range(len(A)))<br>plt.yticks(range(len(A)), range(len(A)))<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*dnhx-f7DhalzeIC6XHXoNA.png" /></figure><p>The heatmap reveals an important piece of information: <strong>the most similar sequences are mainly found at the beginning and the end of our time series.</strong></p><p>I have no idea why this happens: I would be interested to hear your thoughts on this. Let’s move on.</p><h3>Grouping of similar sequences</h3><p>The next step involves grouping similar sequences together in A. The idea here is simple: if the sequences share a similarity score below a certain threshold, we group them together in a dictionary called <em>G</em>. G will contain all the significant clusters for this project.</p><pre># populate G<br>G = {}<br>THRESHOLD = 6 # arbitrary value - tweak this to get different results<br><br>for i in range(len(S)):<br>    G[i] = []<br>    for j in range(len(S)):<br>        if S[i, j] &lt; THRESHOLD and i != j and (i, j) not in G and (j, i) not in G and j not in G[i]:<br>            G[i].append(j)<br><br># remove any empty groups<br>G = {k: v for k, v in G.items() if v}</pre><p>A note on the threshold: I used the value of 6 by trial and error, as it seems to produce the best results in terms of similarity grouping (see below). You might want to use other values: experiment and let me know how it works for you.</p><p>Let’s see how G appears by printing a part of the dictionary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*3gRjku3LAJXQ4e64fBEHYQ.png" /></figure><p>Good, but not great! A dictionary actually does not convey information effectively. We see that some of the sequences are grouped together, but what do they look like?</p><h3>Visualization of groups</h3><p>And now the most interesting part… the visualization of the groups!</p><p>For each key in the dictionary G, which refers to the seed sequence A, we plot a dedicated graph for all sequences similar to the seed.</p><p>Remember: all similar sequences share a low value of dynamic temporal distortion and a similar trend informed by correlation. The parameters used to achieve this grouping are</p><p>N = 15<br>K = 0.70<br>THRESHOLD = 6</p><p>Let’s create a function called plot_similar_sequences that takes G as an argument.</p><pre>import math<br><br>def plot_similar_sequences(G):<br>    n_col = round(math.sqrt(len(G)))<br>    if (math.sqrt(len(G)) &gt; int(math.sqrt(len(G)))):<br>         n_col = int(math.sqrt(len(G))) + 1<br>    fig, ax = plt.subplots(n_col, n_col, figsize=(n_col * 20, 20 * n_col))<br>    r = 0<br>    c = 0<br>    for key in G.keys():<br>        for j in G[key]:<br>            if (r &gt;= n_col):<br>                print(&quot;Errore&quot;)<br>            if (c &gt;= n_col):<br>                c = 0<br>                r = r + 1<br>            ax[r][c].set_title(f&#39;Group {key}&#39;, fontdict={&quot;fontsize&quot;: 30, &quot;weight&quot;: 600})<br>            ax[r][c].plot(A[j], label=j, linestyle=&#39;--&#39;, linewidth=10, alpha=0.50)<br>        ax[r][c].plot(A[key], label=f&#39;target {j}&#39;, linewidth=10, color=&#39;black&#39;)<br>        ax[r][c].annotate(f&#39;{key}&#39;, xy=(len(A[key]) - 1, A[key][-1]), xytext=(len(A[key]) - 1, A[key][-1]))<br>        ax[r][c].plot(np.mean(A[key], axis=0), label=&#39;average&#39;, color=&#39;black&#39;, linestyle=&#39;--&#39;)<br>        c = c + 1<br>    plt.show()</pre><p>The output is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*lVZX9-buXBY4OD4_OKRSgw.png" /><figcaption>The displayed groups. In each chart, the sequence of seeds is in black and the similar ones are in different colors.</figcaption></figure><p>The logic seems to work well, as the black lines, which are the seed sequences, are <strong>surrounded by similar sequences both in terms of anatomy and direction.</strong></p><p>Let’s check another combination of parameters.</p><p>N = 25<br>K = 0.60<br>THRESHOLD = 25</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*spBi7pv1InUFzV8-lxhkPg.png" /></figure><p><strong>The results can change drastically based on the input parameters</strong>. This is expected since the window size, the A/B sequence split size, and the threshold significantly influence the logic of the clustering algorithm.</p><p>Some models really stand out, like this one in group 22.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/298/1*kweQaFsK1SDj4sQDwA2GZw.png" /></figure><p>and group 26</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/308/1*knN2Tr2lQGBDedj47eG1cg.png" /></figure><p>Most of the sequences around the seed sequence are indeed similar and visually share several characteristics. I believe a more accurate methodology is necessary to evaluate these properties.</p><p>Even though I’m sure there is a better way to calculate the score, these results seem promising to me!</p><h3>Calculate the probabilities</h3><p>Ci avviciniamo all’ultima sezione di questo articolo. <strong>Ora calcoleremo la probabilità di avere un trend rialzista, ribassista o stabile per le sequenze B</strong>. Ricordiamo che B segue sempre A—il che significa che il primo elemento di <em>B_i </em>segue il rispettivo elemento di A_i​.</p><p>We will define another support function called classify_trend that is responsible for calculating the slope and understanding the general direction of sequence B.</p><pre>def classify_trend(b, threshold=0.05):<br>    &quot;&quot;&quot;<br>    Classify the trend of a vector<br>    &quot;&quot;&quot;<br>    # compute slope<br>    slope = np.mean(np.diff(b) / np.diff(np.arange(len(b))))<br>    # if slope is positive, the trend is upward<br>    if slope + (slope * threshold) &gt; 0:<br>        return 1<br>    # if slope is negative, the trend is downward<br>    elif slope - (slope * threshold) &lt; 0:<br>        return -1<br>    # if slope is close to 0, the trend is flat<br>    else:<br>        return 0<br>        <br># flatten list<br>flattened_G = [item for sublist in G.values() for item in sublist]<br>trends = [classify_trend(B[i]) for i in flattened_G]<br><br># what is the probability of seeing a trend given the A sequence?<br>PROBABILITIES = {}<br><br>for k, v in G.items():<br>    for seq in v:<br>        total = len(v)<br>        seq_trends = [classify_trend(B[seq]) for seq in v]<br>        prob_up = len([t for t in seq_trends if t == 1]) / total<br>        prob_down = len([t for t in seq_trends if t == -1]) / total<br>        prob_stable = len([t for t in seq_trends if t == 0]) / total<br>        PROBABILITIES[k] = {&#39;up&#39;: prob_up, &#39;down&#39;: prob_down, &#39;stable&#39;: prob_stable}<br>       <br># Let&#39;s pack all in a Pandas DataFrame for an easier use<br>probs_df = pd.DataFrame(PROBABILITIES).T<br># create a column that contains the number of elements in the group<br>probs_df[&#39;n_elements&#39;] = probs_df.apply(lambda row: len(G[row.name]), axis=1)<br>probs_df.sort_values(by=[&quot;n_elements&quot;], ascending=False, inplace=True)</pre><p>and these are the results</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*8Sn17KqB7gcFqAViMmcysA.png" /></figure><p>Let’s visualize the probability distributions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*PLrJZDKjIyoPCsmDd6Qfpg.png" /></figure><p>I chose to sort the elements by n_elements, as the more the sequences are grouped, the more relevant the grouping is. In fact, groups 1 and 14 have 8 and 7 elements, respectively, which are many similar patterns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*w0cM-Iry822jXUJcU5rhIw.png" /></figure><p>This is not surprising, as both sequences A are essentially flat. There is definitely room for optimization here, such as, for example, the removal of all the flat lines. But we’ll see that in the future. In any case, 75% of the B sequences in Group 1 have an upward direction, while 57% of the B sequences in Group 14 have a downward direction. Perhaps the two lines are indeed different in some respects, or this effect is just random—we cannot say at the moment. Let’s trace the B sequences.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*W_Qu-2vOeWfU841IxnxDCw.png" /></figure><p>Most of these B sequences have an actually upward trend. There is work to be done to ensure that this is not just noise.</p><p>Let’s check group 14.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*oEHml-Ma1XH3iPrQNgjRjQ.png" /></figure><p>And indeed, most of them have a downward trend. Even though the logic seems to be correct, perhaps the slope calculation needs to be revisited: perhaps we can increase the threshold from the default value of 0.05… but we will see in the next article.</p><p>Just out of pure curiosity, let’s trace Group 22, which also seems interesting from a visual perspective.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*TezBXY1auOaEKXyqy-V_rQ.png" /></figure><p>Is it only me, <strong>or do the B sequences really seem very similar to each other</strong>? Although this requires in-depth analysis, if this proves true, then A could indeed predict B, and this, in turn, would explain why we see similar Bs.</p><h3>Conclusion of Part 1</h3><p>In this first part, we have seen how the algorithm works and the potential it could have in predicting stock market trends.</p><p>Actually, if it works, it could potentially be a general approach…applicable to any time series. I will expand the analysis in the near future as I continue to work on the project.</p><p>Any questions, comments, or suggestions are always welcome. Thank you for reading!</p><h3>References</h3><ul><li>Marcos López de Prado (Wiley, 2018). <em>Advances in Financial Machine Learning.</em></li><li>Aghabozorgi, Shirkhorshidi &amp; Wah, “Time-series clustering—a decade review,” <em>Information Systems</em> (2015)</li><li>Cavalcante, Brasileiro, Souza, Nóbrega &amp; Oliveira, “Computational intelligence and financial markets: A survey and future directions,” <em>Expert Systems with Applications</em> (2016).</li><li>Ruey Tsay. <em>Analysis of Financial Time Series.</em></li></ul><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b1a1c6af1e13" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/clustering-time-series-for-stock-market-prediction-in-python-part-1-b1a1c6af1e13">Clustering time series for stock market prediction in Python</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using CLIP and HuggingFace Transformers to create a zero-shot image classifier]]></title>
            <link>https://pub.aimind.so/using-clip-and-huggingface-transformers-to-create-a-zero-shot-image-classifier-66c98711928d?source=rss-fe44f5fcf1d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/66c98711928d</guid>
            <category><![CDATA[contrastive-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[transformers]]></category>
            <dc:creator><![CDATA[Francesco  Franco]]></dc:creator>
            <pubDate>Thu, 21 May 2026 06:32:10 GMT</pubDate>
            <atom:updated>2026-05-22T19:41:03.237Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E1nDs1ERSW1Gogx4QMLsOQ.png" /></figure><p>The <a href="https://medium.com/ai-mind-labs/introduction-to-clip-contrastive-language-image-pretraining-5748d2081384">CLIP model</a> from OpenAI impresses me. When it was introduced in 2021, it was the first approach to successfully combining visuals and texts at scale. It accomplishes this by identifying similarities between text and image pairs. As a result, it is suitable for zero-shot classification. This is also known as classification without explicit training and without prior explicit instances.</p><p>The article linked above provides a more detailed description of CLIP, its components, and why it works. This essay focuses on creating something useful using these tools: a zero-shot image classifier. Indeed, we are going to create a pipeline capable of identifying an image based on text. That is, you provide the classes in text; you provide the image; and the model handles the rest.</p><p>It’s organized as follows:</p><ol><li><strong>Why CLIP works: </strong>First, you will get a brief synopsis of the article referenced above. Before using technology, I believe that it is critical to understand how it works. That is why we are starting with a high-level overview of how and why CLIP works. You can then read the other article to learn more about CLIP.</li><li><strong>Building the zero-shot image classifier:</strong> this is followed by actually building the classification pipeline. It’s done by specifying a configuration in JSON, loading it, then using CLIPProcessor it CLIPModel to classify the loaded image.</li><li><strong>Examples</strong>: including simple ones that demonstrate good performance right out of the box, as well as more challenging ones where this is not assured. You’ll also see how simple it is to swap classes, allowing you to apply the loaded classifier to something new.</li></ol><p>Let’s take a look!</p><h3>Why can CLIP be used for zero-shot image classification?</h3><p>The CLIP model, or <strong>Contrastive Language-Image Pre-training,</strong> is an OpenAI model from 2021. It is the first of its kind and can forecast the similarity of text and image pairs on a large scale. Unlike prior approaches (which attempted to achieve the same result by utilizing predictive modeling, i.e., predicting the text label for a related image that serves as model input), CLIP employs a contrastive approach that distinguishes between correct and incorrect pairs.</p><p>As a result of training this model at scale, with both the text Transformer and the vision Transformer trained concurrently, the embeddings of correct text/image pairs will be highly comparable, whereas those of incorrect pairs will be less similar. This can be used to identify photos in a zero-shot method, as shown below (Radford et al., 2021):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rh6AWI4sbyxENjIvZctqCA.png" /></figure><h3>Building a zero-shot image classifier</h3><p>Let us now create the actual zero-shot classifier! To get started, open your IDE or Jupyter Notebook and create a new file (for example, clipzeroshot.py or its Notebook equivalent).</p><h4>Imports</h4><p>Of course, building anything with Python requires specifying a set of imports. Let’s specify all of them—and why we need them—and then add the first lines of code to our script:</p><ul><li>PIL is required for loading the image (as Image) and providing a format that can be passed to the CLIP model.</li><li>transformers is used for loading the CLIP model (CLIPModel) and the CLIP processor (which converts the inputs into an encoded representation; CLIPProcessor). Also, BatchEncoding is imported (because it&#39;s the type of what one of the definitions that follows will return).</li><li>In the background, everything runs in PyTorch, so torch is imported for defining a return type.</li><li>Finally, there are some Python imports: logging for logging, os for specifying paths and json for parsing a string as JSON.</li></ul><p>Here’s everything in code:</p><pre>from PIL import Image<br>from transformers import CLIPProcessor, CLIPModel<br>from transformers.tokenization_utils_base import BatchEncoding<br>import torch<br>import logging<br>import os<br>import json<br><br>logging.basicConfig(level=logging.INFO)</pre><h4>JSON-based configuration</h4><p>I strongly believe in isolating a program’s configuration from its code. That is, whenever possible, I aim to avoid hardcoding information into a script. Although many of the benefits are most noticeable in larger software applications, they are also useful in smaller ones. So, let’s specify a configuration (save it as configuration.json in the same folder as your.py script) that specifies:</p><ul><li>What processor and what model to use. We&#39;re using clip-vit-base-patch32.</li></ul><blockquote>The model uses a ViT-B/32 Transformer architecture as an image encoder and uses a masked self-attention Transformer as a text encoder</blockquote><blockquote>(Radford et al., 2021).</blockquote><ul><li>What classes to use in generating the prediction. The beauty of CLIP is that through learning (text, image) similarity pairs, it is possible to generate a classifier by text only (the authors discuss this in their paper by arguing that the CLIP framework essentially produces a <em>hypernetwork).</em> It is one of the aspects of CLIP that I find most fascinating:</li></ul><blockquote>[The] image encoder is the computer vision backbone which computes a feature representation for the image and the text encoder is a hypernetwork (Ha et al., 2016) which generates the weights of a linear classifier based on the text specifying the visual concepts that the classes represent</blockquote><blockquote>(Radford et al., 2021).</blockquote><ul><li>Next, you’ll specify the image path.</li></ul><p>In other words,</p><pre>{<br>    &quot;processor&quot;: &quot;openai/clip-vit-base-patch32&quot;,<br>    &quot;model&quot;: &quot;openai/clip-vit-base-patch32&quot;,<br>    &quot;classes&quot;: [<br>        &quot;spaceship&quot;,<br>        &quot;fisherman&#39;s boat&quot;,<br>        &quot;tanker&quot;<br>    ],<br>    &quot;image&quot;: &quot;./image_to_classify.jpg&quot;<br>}</pre><p>is your configuration for today.</p><p>Feel free to make some adaptations, such as specifying more or fewer classes, changing them, and so forth.</p><p>In your Python script, add a function that opens the configuration file, parses its JSON contents and returns a dictionary:</p><pre>def load_configuration(path: str = os.path.join(os.getcwd(), &quot;configuration.json&quot;)) -&gt; dict:<br>    &quot;&quot;&quot;<br>    Loads the configuration file.<br><br>    Args:<br>        path (str, optional): The path to the configuration file. Defaults to &quot;config.json&quot;.<br><br>    Returns:<br>        dict: The configuration<br>    &quot;&quot;&quot;<br>    with open(path, &quot;r&quot;) as f:<br>        return json.load(f)</pre><h4>Getting an instance of the CLIPModel</h4><p>Next step: getting an instance of the CLIP model.</p><p>With transformers, this is really easy: you&#39;ll just specify the class, tell it to use a pretrained model, et voilà. That&#39;s the minimum amount of work necessary to load the CLIP model.</p><p>Indeed, as suggested before, we’re using the openai/clip-vit-base-patch32 pretrained CLIP model. You will see that it is passed from the configuration file later, but to ensure the function keeps working out of the box, it is passed as the default value too (if passing a value is omitted, it will be used too).</p><pre>def get_model(model: str = &quot;openai/clip-vit-base-patch32&quot;) -&gt; CLIPModel:<br>    &quot;&quot;&quot;<br>    Returns the CLIP model which uses a vision transformer and a language transformer to map images and text to a common latent space.<br>    Subsequently, the similarity between the two can be measured using cosine similarity and used to classify images.<br><br>    Args:<br>        model (str, optional): The model to use. Defaults to &quot;openai/clip-vit-base-patch32&quot;.<br><br>    Returns:<br>        CLIPModel: The CLIP model<br>    &quot;&quot;&quot;<br>    return CLIPModel.from_pretrained(model)</pre><h4>Getting an instance of the CLIPProcessor</h4><p>The same is true for the processor, which essentially wraps the image processor and text processor for easy generation of encoded texts and images:</p><blockquote>The CLIPProcessor wraps CLIPImageProcessor and CLIPTokenizer into a single instance to both encode the text and prepare the images</blockquote><blockquote>(HuggingFace, n.d.)</blockquote><p>Just call from_pretrained on the CLIPProcessor class with the pretrained processor (the OpenAI one in our case, also as default) and that&#39;s it:</p><pre>def get_processor(processor: str = &quot;openai/clip-vit-base-patch32&quot;) -&gt; CLIPProcessor:<br>    &quot;&quot;&quot;<br>    Returns the CLIP processor which uses the CLIP tokenizer to convert text and images to features.<br><br>    Args:<br>        processor (str, optional): The processor to use. Defaults to &quot;openai/clip-vit-base-patch32&quot;.<br><br>    Returns:<br>        CLIPProcessor: The CLIP processor<br>    &quot;&quot;&quot;<br>    return CLIPProcessor.from_pretrained(processor)</pre><h4>The image to classify</h4><p>Let’s now focus on the image that must be classified.</p><p>I’ve asked DALL-E 3 to generate an image.</p><blockquote>Show me what a fisherman’s boat looks like.</blockquote><p>We’ll save it as image_to_classify.jpg into the same folder as the Python script, because that&#39;s what we&#39;ve configured in the configuration JSON above. If you wish to store the image in a different location, feel free to adapt the configured path yourself.</p><p>Clearly, out of the 3 possible classes in the configuration, this is a fisherman&#39;s boat.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Swx6iXIoq4tMsVxTeUM8wg.jpeg" /></figure><h4>Encoding inputs</h4><p>Now that we have an image, it&#39;s time to use the instantiated processor to encode the textual and image inputs.</p><p>We create another definition for this: it accepts the processor, the loaded image and a set of classes, which are just strings. It passes them to the processor (specifying that return tensors must be torch ones) and returns the result.</p><pre>def get_encoded_input(processor: CLIPProcessor, image: Image.Image, classes: list[str]) -&gt; BatchEncoding:<br>    &quot;&quot;&quot;<br>    Returns the encoded input for the CLIP model.<br><br>    Args:<br>        processor (CLIPProcessor): The CLIP processor<br>        image (Image.Image): The image<br>        classes (list[str]): The classes<br><br>    Returns:<br>        BatchEncoding: The encoded input.<br><br>    &quot;&quot;&quot;<br>    return processor(text=classes, images=image, return_tensors=&quot;pt&quot;, padding=True)</pre><h4>Getting a prediction</h4><p>Okay, now it’s time for the actual magic. If we pass the encoded_input to the model, it generates a prediction. That&#39;s what we do in the def below, and we subsequently extract the image logits. Depending on whether we want to apply <a href="https://medium.com/ai-advances/the-softmax-activation-function-work-with-keras-8f674b4481a5">Softmax (</a>this is not strictly necessary but will give you a nice distribution whose components sum to 1), we apply it, then return the prediction.</p><pre>def get_prediction(model: CLIPModel, encoded_input: BatchEncoding, use_softmax: bool = True) -&gt; torch.Tensor:<br>    &quot;&quot;&quot;<br>    Returns the prediction for the CLIP model.<br><br>    Args:<br>        model (CLIPModel): The CLIP model<br>        encoded_input (BatchEncoding): The encoded input<br>        use_softmax (bool, optional): Whether to use softmax. Defaults to True.<br><br>    Returns:<br>        torch.Tensor: The prediction<br>    &quot;&quot;&quot;<br>    prediction = model(**encoded_input).logits_per_image<br><br>    if use_softmax:<br>        prediction = prediction.softmax(dim=-1)<br><br>    return prediction</pre><h4>Merging everything together</h4><p>We now have all the components ready. Time to merge them together into a main function that we use when running the script.</p><p>Here’s what is happening:</p><ul><li>The configuration is loaded.</li><li>The model and processor are retrieved and the image is opened.</li><li>The image and classes are encoded and fed to the model for generating a prediction.</li><li>The prediction is converted into an expected target class and both are logged.</li></ul><p>That’s it!</p><pre>def main() -&gt; None:<br>    &quot;&quot;&quot;<br>    The main function which runs the script.<br><br>    Returns:<br>        None<br>    &quot;&quot;&quot;<br>    config = load_configuration()<br>    model = get_model(config[&quot;model&quot;])<br>    processor = get_processor(config[&quot;processor&quot;])<br>    image = Image.open(config[&quot;image&quot;])<br>    encoded_input = get_encoded_input(processor, image, config[&quot;classes&quot;])<br>    prediction = get_prediction(model, encoded_input)<br><br>    predicted_class = config[&quot;classes&quot;][prediction.argmax()]<br><br>    logging.info(f&quot;Prediction over classes: {prediction.tolist()}&quot;)<br>    logging.info(f&quot;Predicted class: {predicted_class}&quot;)<br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><h3>Classification examples</h3><p>Let’s now run our script. It’s very easy: just run python clipzeroshot.py (ensure that you have all the dependencies installed). What follows next are results from a few examples, some simple ones, some difficult ones, and a different set of classes, showing you how easy it is to change classes and reuse the model for something else.</p><h4>Easy ones</h4><p>First, the fisherman&#39;s boat.</p><blockquote>INFO:root:Prediction over classes: [[0.00018303414981346577, 0.9983105659484863, 0.0015064344042912126]] INFO:root:Predicted class: fisherman’s boat</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Swx6iXIoq4tMsVxTeUM8wg.jpeg" /></figure><p>Now a spaceship:</p><blockquote>INFO:root:Prediction over classes: [[0.9909811615943909, 0.001415808335877955, 0.007602964993566275]] INFO:root:Predicted class: spaceship</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gc8aN5GKJwYobAv8eipBSw.jpeg" /></figure><h4>More difficult ones</h4><p>Let’s make it a bit more difficult now.</p><blockquote>Mix the spaceship and the fisherman’s boat, with a ratio of approximately 70% to 30%.</blockquote><p>I have never seen such a strange object before, but here goes:</p><blockquote>INFO:root:Prediction over classes: [[0.7287755012512207, 0.26701247692108154, 0.004212076310068369]] INFO:root:Predicted class: spaceship</blockquote><p>As expected, it is still classified as a spaceship, but the fisherman&#39;s boat has a 26.7% probability, suggesting that it&#39;s indeed a mix (and interestingly, with ~73% / ~27% vs ~0% quite close to the 70%/30% mix from the prompt.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2FcgiPKzoACl3nZAvN9HSQ.jpeg" /></figure><h4>Swapping the classes: the beauty of CLIP</h4><p>Before, I suggested how easy it is to swap the classes. Let’s do so by changing the JSON:</p><pre>{<br>    &quot;processor&quot;: &quot;openai/clip-vit-base-patch32&quot;,<br>    &quot;model&quot;: &quot;openai/clip-vit-base-patch32&quot;,<br>    &quot;classes&quot;: [<br>        &quot;apple&quot;,<br>        &quot;not an apple&quot;<br>    ],<br>    &quot;image&quot;: &quot;./image_to_classify.jpg&quot;<br>}</pre><p>Time to classify this apple. And indeed,</p><blockquote>INFO:root:Prediction over classes: [[0.5734764337539673, 0.4265235960483551]] INFO:root:Predicted class: apple</blockquote><p>(However, you can see that the model is not really certain, probably because of the large empty background. If we had used apple and banana instead, the class probabilities would have been 0.999/0.001.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r7AELq7UYpI_flNKXxZ8vA.jpeg" /></figure><h3>References</h3><ul><li>HuggingFace. (n.d.). <em>Openai/clip-Vit-base-patch32 · Hugging face</em>. Hugging Face — The AI community building the future. <a href="https://huggingface.co/openai/clip-vit-base-patch32">https://huggingface.co/openai/clip-vit-base-patch32</a></li><li>Radford, A., Kim, J. W., Hallacy, C., Ramesh, A., Goh, G., Agarwal, S., … &amp; Sutskever, I. (2021, July). <a href="https://arxiv.org/abs/2103.00020">Learning transferable visual models from natural language supervision.</a> In <em>International conference on machine learning</em> (pp. 8748–8763). PMLR.</li><li>HuggingFace. (n.d.). <em>Clip</em>. Hugging Face — The AI community building the future. <a href="https://huggingface.co/docs/transformers/model_doc/clip#usage-tips-and-example">https://huggingface.co/docs/transformers/model_doc/clip#usage-tips-and-example</a></li><li>Encord. (2023, July 14). <em>Full guide to contrastive learning</em>. Data Engine for AI Model Development | Encord. <a href="https://encord.com/blog/guide-to-contrastive-learning">https://encord.com/blog/guide-to-contrastive-learning</a></li><li>NumPy. (n.d.). <em>Numpy.arange — NumPy v1.26 manual</em>. <a href="https://numpy.org/doc/stable/reference/generated/numpy.arange.html">https://numpy.org/doc/stable/reference/generated/numpy.arange.html</a></li><li>PyTorch. (n.d.). <em>CrossEntropyLoss — PyTorch 2.1 documentation</em>. <a href="https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html">https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html</a></li><li>HuggingFace. (n.d.). <em>Zero-shot classification</em>. Hugging Face — The AI community building the future. <a href="https://huggingface.co/tasks/zero-shot-classification">https://huggingface.co/tasks/zero-shot-classification</a></li><li>Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., … &amp; Polosukhin, I. (2017). <a href="https://proceedings.neurips.cc/paper_files/paper/2017/hash/3f5ee243547dee91fbd053c1c4a845aa-Abstract.html">Attention is all you need</a>. <em>Advances in neural information processing systems</em>, <em>30</em>.</li><li>Radford, A., Wu, J., Child, R., Luan, D., Amodei, D., &amp; Sutskever, I. (2019). <a href="https://insightcivic.s3.us-east-1.amazonaws.com/language-models.pdf">Language models are unsupervised multitask learners</a>. <em>OpenAI blog</em>, <em>1</em>(8), 9.</li><li>Dosovitskiy, A., Beyer, L., Kolesnikov, A., Weissenborn, D., Zhai, X., Unterthiner, T., … &amp; Houlsby, N. (2020). <a href="https://arxiv.org/pdf/2010.11929.pdf">An image is worth 16x16 words: Transformers for image recognition at scale.</a> <em>arXiv preprint arXiv:2010.11929</em>.</li></ul><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=66c98711928d" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/using-clip-and-huggingface-transformers-to-create-a-zero-shot-image-classifier-66c98711928d">Using CLIP and HuggingFace Transformers to create a zero-shot image classifier</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to CLIP: Contrastive Language-Image Pretraining]]></title>
            <link>https://pub.aimind.so/introduction-to-clip-contrastive-language-image-pretraining-5748d2081384?source=rss-fe44f5fcf1d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/5748d2081384</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[contrastive-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Francesco  Franco]]></dc:creator>
            <pubDate>Sun, 10 May 2026 07:47:18 GMT</pubDate>
            <atom:updated>2026-05-11T17:21:25.663Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l-9nWy5ULYwKC15_A6bMdQ.png" /></figure><p>Neural network classifiers were likely a part of what you learned when you first began studying about deep learning techniques. These networks are highly supervised since they were trained on datasets with a large number of samples for a small number of classes.</p><p>However, they perform poorly when applied to samples that deviate from the distribution seen in your training set.</p><p>Additionally, they don’t let you apply them to other categorization problems. They function well for the task they were trained for when properly trained, but that’s it.</p><p>OpenAI researchers investigated ways to address the issue in 2021. They specifically questioned whether a system for zero-shot classification — that is, a technique that operates on a range of instances without explicit training or the provision of comparable examples in the prompt at inference time — could be developed.</p><p>Indeed, this proved to be feasible. This article examines CLIP, which is trained on both text and images simultaneously, enabling zero-shot behavior.</p><h3>Problems with directly mapping images to text</h3><p>There are several approaches to building a zero-shot classifier; one architecture is not always superior to another. The Transformer is frequently used in the design of such architectures (Vaswani et al., 2017). It is now the state of the art in natural language processing. In fact, this architecture is the foundation of many contemporary language models.</p><p>The OpenAI team attempted to jointly train an image and text model for picture caption prediction in one of their initial attempts:</p><blockquote>Our initial approach, similar to VirTex, jointly trained an image CNN and text transformer from scratch to predict the caption of an image. However, we encountered difficulties efficiently scaling this method</blockquote><blockquote>(Radford et al., 2021).</blockquote><p>Sadly, that didn’t work out. The authors claim that the model tried to solve a very challenging problem, which is essentially a predictive problem:</p><blockquote>[The model tries] to predict the exact words of the text accompanying each image. This is a difficult task due to the wide variety of descriptions, comments, and related text that co-occur with images</blockquote><blockquote>(Radford et al., 2021).</blockquote><h3>CLIP: Contrastive Language-Image Pretraining</h3><p>They contend that a contrastive strategy would be more effective. Imagine that you are instructing a child on the appearance of a banana. Frequently, you say “banana!” while pointing to a banana. The toddler gradually learns to differentiate between bananas and other fruits by switching between them (“other fruit!”).</p><p>In other words, you’re employing a contrastive learning strategy.</p><blockquote>Contrastive learning is an approach to learning that focuses on extracting meaningful representations by contrasting positive and negative pairs of instances</blockquote><blockquote>(Encord, 2023).</blockquote><p>Now that we know what contrastive learning is, let’s examine CLIP.</p><h3>CLIP design</h3><p><strong>Contrastive Language-Image Pretraining,</strong> or CLIP, is a model that was jointly trained on images and language pairs. Texts are encoded by a text encoder, while images are encoded into a shared embedding space with dimensionality d_e by an image encoder. Let’s examine each of these parts separately before examining CLIP’s training process.</p><h4>Text encoding</h4><p>Text-wise, a Transformer from the Radford et al. (2019) paper — GPT-2 — is fed byte-pair encodings with a maximum sequence length of 76. Textual feature representations are conceived of as activations of the topmost layer. The embedding space with dimensionality d_e is then created by linearly projecting them.</p><blockquote>The text encoder is a Transformer (Vaswani et al., 2017) with the architecture modifications described in Radford et al. (2019)</blockquote><blockquote>(Radford et al., 2019)</blockquote><h4>Image Encoding</h4><p>The image batches undergo a similar procedure. They are also projected onto embedding space with dimensionality d_e after being encoded. In contrast to text, the authors’ use of images in their work was a little more experimental. They employed two distinct architectures rather than simply one Transformer (GPT-2) to see which one performed the best. In particular, they employed a number of Vision Transformers (as presented by Dosovitskiy et al. in 2020) and a number of modified ResNets. The latter ones performed better, especially the one that was trained at a higher image resolution. According to Radford et al., when they refer to CLIP, they mean the combination of GPT-2 and that particular Vision Transformer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rh6AWI4sbyxENjIvZctqCA.png" /></figure><h3>Training CLIP</h3><p>When the two modalities are integrated in the forward pass of CLIP, the result is utilized for optimization, which is when the magic happens. I have to admit that at first it was a little difficult to understand, but later on, its beauty became very evident.</p><p>Let’s examine the picture up above. As of right now, you are aware that both batches, when fed through both transformers, have the same shape: [N, d_e] is the shape of both the texts and the images since there are N images/texts of dimensionality d_e.</p><p>Visually, they are the purple row and green column in the left part of the above image.</p><h4>Forward pass and similarity computation</h4><p>The following then takes place:</p><ul><li>Vector similarity is calculated for every pair of images and texts (represented by two vectors per pair, one for the image and another for the text). To do this, the vectors are first L2 normalized, and then cosine <strong>similarity</strong> is calculated (using dot products, which yields equivalent results). A similarity matrix is the result of this.</li><li>A <strong>symmetric loss</strong> is calculated based on similarities. In other words, the logits of the matrix are compared with certain labels, and a loss function is calculated over the vertical and horizontal axes (Radford et al., 2021).</li></ul><blockquote>Given a batch of N (image, text) pairs, CLIP is trained to predict which of the N × N possible (image, text) pairings across a batch actually occurred. To do this, CLIP learns a multi-modal embedding space by jointly training an image encoder and text encoder to maximize the cosine similarity of the image and text embeddings of the N real pairs in the batch while minimizing the cosine similarity of the embeddings of the N2 − N incorrect pairings. We optimize a symmetric cross entropy loss over these similarity scores</blockquote><blockquote>(Radford et al., 2021).</blockquote><h4>How does CLIP symmetric loss work?</h4><p>This is the pseudocode of CLIP’s symmetric loss function (Radford et al., 2021):</p><pre># symmetric loss function<br>labels = np.arange(n)<br>loss_i = cross_entropy_loss(logits, labels, axis=0)<br>loss_t = cross_entropy_loss(logits, labels, axis=1)<br>loss = (loss_i + loss_t)/2</pre><p>To put it another way, symmetric in this context simply refers to the average over cross-entropy loss calculated between a few labels and the logits across two axes.</p><p>However, given that labels are specified as np.arange(n), how said loss may be calculated over the logits perplexed me.</p><p>I’ve finally found out. Let me explain.</p><p>It begins with understanding what is actually <em>meant</em> by computing np.arange:</p><blockquote>Return evenly spaced values within a given interval</blockquote><blockquote>(NumPy, n.d.).</blockquote><p>Put differently, this results in a list of numbers with n=3, such as [0, 1, 2]. Then I became even more perplexed. How is it possible to compare any integer with a logits vector (that is, every row or column in the similarity matrix)?</p><p>I looked at the image above once again after doing a good deal of searching. As you can see, a diagonal is represented by the appropriate text-image pairs. From an index perspective, this indicates that every pair can be located at [i, i], where i falls between 0 and n. Put differently, the true indices of the diagonal are represented by np.arange(n): np.arange(3) = np.array([0, 1, 2]). The positions [(0, 0), (1, 1), (2, 2)] are equivalent to this. The diagonal.</p><p>I was one step closer when I realized that. However, I was still perplexed as to how a single integer value, like 0, might be utilized to calculate the loss in contrast to a vector.</p><p>Then I discovered that many deep learning frameworks (like PyTorch) allow an integer as a target, which is a class index, for cross-entropy loss:</p><blockquote>The target that this criterion expects should contain either (…) class indices in the range [0, C) where C is the number of classes</blockquote><blockquote>(PyTorch, n.d.)</blockquote><p>The class index is internally transformed into a<a href="https://medium.com/ai-mind-labs/one-hot-encoding-for-machine-learning-with-python-and-scikit-learn-c6d8e1173760"> one-hot encoded</a> representation by PyTorch. To put it another way, class index 3 for five potential classes indicates that, following conversion, the associated label becomes [0, 0, 0, 1, 0]. After that, since the two tensors can be compared, the cross-entropy loss between the logits and the label can be calculated.</p><h3>CLIP loss — an example</h3><p>Since it is the foundation of CLIP, it is crucial to fully comprehend. Let’s now illustrate this with an example.</p><p>Assume we have this similarity matrix, which are the logits—the dot products generated as a similarity measure for each (text, vector) combination:</p><pre>           <strong>t1</strong>      <strong>   t2 </strong>      <strong>t3</strong><br><br>i1         0.9        0.2      0.1<br><br>i2         0.3        0.8      0.2<br><br>i3         0.1        0.4      0.7<br></pre><p>You can see that the diagonal has the highest values. This makes sense given that the diagonal comprises the true pairs (t1, i1), (t2, i2), and (t3, i3). That is why the original work defines labels as np.arange(n), which includes the proper pairs 0, 1, and 2.</p><p>CLIP loss is then calculated over both dimensions: once for text/image pairs and again for image/text pairs.</p><p>In both directions, you’d expect to find this:</p><ul><li>For the first pair: [1 0 0].</li><li>For the second pair: [0 1 0].</li><li>For the third pair: [0 0 1].</li></ul><p>Assume you’re computing the initial loss across axis=0, or downwards across all rows. You’re passing index = 0 for the first row, index = 1 for the second, and so on, which is equivalent to passing [1 0 0], [0 1 0], and so on. In other words, when calculating loss, you’ll contrast [0.9 0.2 0.1] with [1 0 0], [0.3 0.8 0.2] with [0 1 0], and so on. This results in a loss: there are few but considerable variations between the logits and the predicted results. There is no perfect diagonal (i.e., an identity matrix), especially when the network is initialized pseudo-randomly. But we’re striving to get there; therefore, we calculate the loss over the axis = 0.</p><p>Then repeat the process over axis=1, this time horizontally across all columns. Finally, you add both losses and divide by two, so that the losses on the horizontal and vertical axes contribute equally to how inaccurate the model is.</p><p>This is then employed in the backwards pass, resulting in model optimization.</p><p>After training, the optimal similarity matrix for the passed batch is an identity matrix. However, this is most likely simply a theoretical possibility. In practice, it will never be perfect, but the results have proved that they are enough for practical use!</p><h3>CLIP Applications</h3><p>Let’s now take a look at a few use cases for CLIP:</p><ol><li>Performing <strong>zero-shot classification</strong>. In other words, building a classifier with text pairs—without requiring any upfront examples during inference.</li><li>Building an <strong>image search engine</strong>, where you’ll be able to find images matching a specific text in your dataset or vice versa: texts matching a specific image.</li><li><strong>Finetuning the model</strong> to your own dataset, if necessary,</li></ol><h4>Zero-shot classification</h4><p>Let us revisit the image. Above, we covered what is depicted in (1) — contrastive pre-training. When OpenAI finished training, they discovered a model capable of understanding the relationship between a text and a picture. This enables you to conduct zero-shot categorization, as defined below:</p><blockquote>Zero-shot (…) classification is a task in natural language processing where a model is trained on a set of labeled examples but is then able to classify new examples from previously unseen classes.</blockquote><blockquote>(HuggingFace, n.d.)</blockquote><p>Even better, because of the fact that it was trained on pairs of texts and images, it’s possible to create any classifier you want! Let’s take a look.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rh6AWI4sbyxENjIvZctqCA.png" /></figure><p>What we see here under (2) and (3) is as follows:</p><ul><li>A<strong> text collection</strong> of ordinary items is generated, including planes, cars, dogs, and birds. This collection is then used to format newer strings, such as “A photo of a car.” These are supplied to the trained text encoder, which converts them into vectors with the same dimensionality as the embedding space.</li><li>The same applies to images. For example, the image above shows a photo of a dog. Here, too, the image is passed through the learned encoder model (the ViT) and turned into an embedding vector with the same dimensionality as the embedding space.</li><li>Then, similarity scores are calculated by taking the dot product of each text and picture embedding (remember that this equals cosine similarity in this example). This results in logits; as with training, the highest logit indicates the most probable class. Of course, it is possible to compute a<a href="https://medium.com/ai-advances/the-softmax-activation-function-work-with-keras-8f674b4481a5"> Softmax activation ov</a>er these logits to verify that their sum equals 1, but this is not required.</li></ul><p>Indeed, in our case, the third textual input is most likely: it’s a <strong>photo of a dog</strong>.</p><p>This means you’ll be able to create a textual collection of any object you want (for example, banana, apple, and orange), feed it through the trained text encoder, pass an image through the trained image encoder, compute similarity, and determine the most likely classification. An accurate zero-shot picture classifier!</p><h4>Image search engine</h4><p>CLIP can also be used as an image search engine, which is another intriguing use. Suppose you have a dataset of photographs, say 1000 photos, that you want to search through. Using CLIP, it would not be difficult to create a web page that accepts textual input (for example, “a yellow car in New York City”) and returns photos that are similar.</p><h4>Finetuning to your own datasets</h4><p>CLIP was trained on a big dataset; however, the items in the photographs were rather common and photographed from precise angles (as one would expect from regular images). As a result, the model is difficult to apply to applications that require different angles, such as satellite photos.</p><p>Similar to any large model, it’s possible to <strong>fine-tune CLIP</strong> with your own dataset. Here is <a href="https://huggingface.co/blog/fine-tune-clip-rsicd">an example</a> that demonstrates how to do that using satellite imagery.</p><h3>Summary</h3><p>Today, you learned more about CLIP, including its components, training, and application. I hope the article helped you obtain a better understanding of this model. Thank you for reading, and please reach out if you have any questions or comments!</p><h3>References</h3><ul><li>Radford, A., Kim, J. W., Hallacy, C., Ramesh, A., Goh, G., Agarwal, S., … &amp; Sutskever, I. (2021, July). <a href="https://arxiv.org/abs/2103.00020">Learning transferable visual models from natural language supervision.</a> In <em>International conference on machine learning</em> (pp. 8748–8763). PMLR.</li><li>Encord. (2023, July 14). <em>Full guide to contrastive learning</em>. Data Engine for AI Model Development | Encord. <a href="https://encord.com/blog/guide-to-contrastive-learning">https://encord.com/blog/guide-to-contrastive-learning</a></li><li>NumPy. (n.d.). <em>Numpy.arange — NumPy v1.26 manual</em>. <a href="https://numpy.org/doc/stable/reference/generated/numpy.arange.html">https://numpy.org/doc/stable/reference/generated/numpy.arange.html</a></li><li>PyTorch. (n.d.). <em>CrossEntropyLoss — PyTorch 2.1 documentation</em>. <a href="https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html">https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html</a></li><li>HuggingFace. (n.d.). <em>Zero-shot classification</em>. Hugging Face — The AI community building the future. <a href="https://huggingface.co/tasks/zero-shot-classification">https://huggingface.co/tasks/zero-shot-classification</a></li><li>Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., … &amp; Polosukhin, I. (2017). <a href="https://proceedings.neurips.cc/paper_files/paper/2017/hash/3f5ee243547dee91fbd053c1c4a845aa-Abstract.html">Attention is all you need</a>. <em>Advances in neural information processing systems</em>, <em>30</em>.</li><li>Radford, A., Wu, J., Child, R., Luan, D., Amodei, D., &amp; Sutskever, I. (2019). <a href="https://insightcivic.s3.us-east-1.amazonaws.com/language-models.pdf">Language models are unsupervised multitask learners</a>. <em>OpenAI blog</em>, <em>1</em>(8), 9.</li><li>Dosovitskiy, A., Beyer, L., Kolesnikov, A., Weissenborn, D., Zhai, X., Unterthiner, T., … &amp; Houlsby, N. (2020). <a href="https://arxiv.org/pdf/2010.11929.pdf">An image is worth 16x16 words: Transformers for image recognition at scale.</a> <em>arXiv preprint arXiv:2010.11929</em>.</li></ul><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5748d2081384" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/introduction-to-clip-contrastive-language-image-pretraining-5748d2081384">Introduction to CLIP: Contrastive Language-Image Pretraining</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Extract Any Entity From Text with GLiNER]]></title>
            <link>https://pub.aimind.so/extract-any-entity-from-text-with-gliner-c555619cc888?source=rss-fe44f5fcf1d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/c555619cc888</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[named-entity-recognition]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Francesco  Franco]]></dc:creator>
            <pubDate>Wed, 06 May 2026 06:20:07 GMT</pubDate>
            <atom:updated>2026-05-07T18:38:22.204Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6XA29_2yeqMrda2GlJuPIA.png" /></figure><p>Those who have worked in the past with the NER (named entity recognition) paradigm, which classifies tokens, know well the value of having a highly efficient model for the task it was trained on.</p><p>In fact, <strong>NER models are extremely useful for data mining and text analysis activities</strong>—they are the foundation of every digital intelligence activity and for a myriad of tasks related to broader and more complex data science pipelines.</p><p>Those who do NER also know how complex it is to train such a model due to the enormous number of labels that need to be specified during the training phase. Libraries like SpaCy and the transformer-based models available on Hugging Face have greatly assisted data scientists in developing NER models more efficiently, improving the process to a certain extent.</p><p>In this article, we will explore the <strong>GLiNER paradigm, a new technique for entity extraction that combines the classic NER paradigm with the power of LLMs.</strong></p><p>By the end of this article, you will know what GLiNER is and how to use it in Python to classify any token in any text.</p><p>In summary, by reading this article, you will learn</p><ul><li>What GLiNER is</li><li>Why it is potentially revolutionary</li><li>How to implement it in Python</li><li>Limitations of GLiNER</li></ul><p>GLiNER has been published in a scientific paper, available at the link below. 👇</p><p><a href="https://arxiv.org/abs/2311.08526?ref=diariodiunanalista.it">GLiNER: Generalist Model for Named Entity Recognition using Bidirectional Transformer</a></p><h3>What is GLiNER?</h3><p><strong>GLiNER is an NER model capable of identifying any type of entity</strong> using a bidirectional transformer encoder (similar to BERT). It provides a practical alternative to traditional NER models, <strong>which are limited to predefined entities</strong>, and to Large Language Models (LLMs), which, despite their flexibility, are costly and too large for scenarios with limited resources.</p><p>GLiNER uses a BiLM (<em>Bidirectional Transformer Language Model</em>) and accepts entity-type prompts and a sentence/text as input.</p><p>Going briefly into the technical details, each entity is separated by a learned token [ENT] and the BiLM generates vector representations (embeddings) for each token. The embeddings are passed into a FeedForward neural network, while the input word representations are passed into a neural layer dedicated to learning the character windows that enclose the token in question. These windows are called <em>spans</em>.</p><p>Finally, a similarity score is calculated between the entity representations and the span representations using the dot product and the sigmoid activation function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*Ls34F8VQKGmnldbtxT6O4w.png" /><figcaption>GLiNER model architecture</figcaption></figure><p>Instead of relying on large autoregressive models (like GPT-3.5 and 4), smaller-scale bidirectional language models (the aforementioned BiLMs) are used, such as BERT or deBERTa.</p><p>The central concept of GLiNER is that the task is not generative but rather a matching between the embeddings of the tokens and those of the spans.</p><p>This approach naturally solves the scalability issues of autoregressive models and allows for bidirectional context processing, enabling richer representations.</p><p>At the time of publication, GLiNER outperformed both ChatGPT and optimized LLMs on zero-shot NER datasets.</p><h3>Why is GLiNER potentially revolutionary?</h3><p>It should be clear that GLiNER is lighter, more scalable, faster, and more precise than an LLM-based approach, including ChatGPT.</p><p>The most amazing thing is that it is now possible to do NER at ZERO cost, quickly and efficiently. We have therefore moved from having to train an NER model from scratch or spending money on an LLM-based solution to a model that combines both solutions and does so in the most efficient way possible, both technically and economically.</p><p>The performance of GLiNER is displayed in the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*KP2cA51mqGtJWXLA-ePFAw.png" /></figure><p>It is surpassed only in some specific domains by other models, but the numerical gap is not wide.</p><p>Below is a benchmark solely against ChatGPT on 20 public NER datasets for a <em>zero-shot t</em>oken classification task:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*qCojtsCqXmigXWJAhMAwYg.png" /></figure><p>GLiNER is available in various sizes, but even the small version is capable of surpassing the performance of ChatGPT. The small version has a size of 50 million parameters, which corresponds to about 600MB.</p><p>The model is therefore impressive in terms of results and has the potential to become the main solution to the most common NER problems.</p><h3>How to implement GLiNER in Python</h3><p>The authors of the project have made a convenient package available for installation via pip.</p><p>pip install gliner</p><p>Unlike a typical NER model, we don’t have to train or fine-tune anything. We only need to specify in a Python list which entities we want to extract from our text.</p><p>So the necessary items are two:</p><ul><li>The entities (read labels) that we want to extract</li><li>The text from which we want to extract the entities</li></ul><p>Using the example in the project’s Git repository, you can see how simple it is to use GLiNER in Python.</p><pre>from gliner import GLiNER<br><br>model = GLiNER.from_pretrained(&quot;urchade/gliner_base&quot;)<br><br>text = &quot;&quot;&quot;<br>Cristiano Ronaldo dos Santos Aveiro (Portuguese pronunciation: <br>[kɾiʃˈtjɐnu ʁɔˈnaldu]; born 5 February 1985) is a Portuguese professional <br>footballer who plays as a forward for and captains both Saudi Pro League <br>club Al Nassr and the Portugal national team. Widely regarded as one of<br>the greatest players of all time, Ronaldo has won five Ballon d&#39;Or <br>awards,[note 3] a record three UEFA Men&#39;s Player of the Year Awards, <br>and four European Golden Shoes, the most by a European player. He has <br>won 33 trophies in his career, including seven league titles, five <br>UEFA Champions Leagues, the UEFA European Championship and the UEFA <br>Nations League. Ronaldo holds the records for most appearances (183),<br>goals (140) and assists (42) in the Champions League, goals in the <br>European Championship (14), international goals (128) and international<br>appearances (205). He is one of the few players to have made over 1,200 <br>professional career appearances, the most by an outfield player, and has<br>scored over 850 official senior career goals for club and country, making <br>him the top goalscorer of all time.<br>&quot;&quot;&quot;<br><br>labels = [&quot;person&quot;, &quot;award&quot;, &quot;date&quot;, &quot;competitions&quot;, &quot;teams&quot;]<br><br>entities = model.predict_entities(text, labels, threshold=0.5)<br><br>for entity in entities:<br>    print(entity[&quot;text&quot;], &quot;=&gt;&quot;, entity[&quot;label&quot;])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*QCFK9UlxOO7IdqJz6ApyjA.png" /></figure><p>It is possible to change the list at will and run the model again to get other entities.</p><p>labels = [&quot;person&quot;, &quot;award&quot;, &quot;date&quot;, &quot;competitions&quot;, &quot;teams&quot;]</p><p>This is the output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*m8dIiT4rwZqmYb58JoZLyw.png" /></figure><h3>Limitations of GLiNER</h3><p>In the paper, the authors highlight several aspects on which GLiNER can improve.</p><ul><li>the model suffers from training on imbalanced classes: some classes are more frequent than others and they would like to improve the identification and management of these classes with a dedicated loss function</li><li>it is necessary to improve multilingual capabilities: it requires training in multiple languages (currently Italian is poorly supported)</li><li>it is based on a similarity threshold: in the future, the authors would like to make it dynamic in order to capture as many entities as possible without distorting the results</li></ul><h3>Conclusions</h3><p>GLiNER is absolutely worth exploring and using for data mining and data analytics tasks.</p><p>My opinion is that it will be a project that will gain more and more traction in the industry, with possible forks that will give rise to even greater innovations.</p><p>Maybe the RAG paradigm can be improved through the fast and efficient identification of entities?</p><h3>References and further reading</h3><ul><li>Li et al. (2022). <em>A Survey on Deep Learning for Named Entity Recognition</em>. IEEE Transactions on Knowledge and Data Engineering, 34(1).</li><li>Keraghel et al. (2024). <em>Recent Advances in Named Entity Recognition: A Comprehensive Survey and Comparative Study</em>. arXiv:2401.10825</li><li>Springer AI Review (2025). <em>A review of named entity recognition: from learning methods to modeling paradigms and tasks</em>.</li><li>GLiNER (Zaratiana et al., 2024). <em>Generalist Model for Named Entity Recognition using Bidirectional Transformers</em>. NAACL 2024</li><li>GLiNER2 (2025). <em>An Efficient Multi-Task Information Extraction System with Schema-Driven Interface</em>.</li><li><strong>GitHub repo</strong> (official): <a href="https://github.com/urchade/GLiNER">github.com/urchade/GLiNER</a> — well-maintained, includes training code and model weights.</li></ul><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c555619cc888" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/extract-any-entity-from-text-with-gliner-c555619cc888">Extract Any Entity From Text with GLiNER</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Time series forecasting with LSTM in TensorFlow]]></title>
            <link>https://pub.aimind.so/time-series-forecasting-with-lstm-in-tensorflow-df32aa5f579b?source=rss-fe44f5fcf1d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/df32aa5f579b</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[time-series-forecasting]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[bidirectional-lstm]]></category>
            <dc:creator><![CDATA[Francesco  Franco]]></dc:creator>
            <pubDate>Fri, 27 Mar 2026 08:05:49 GMT</pubDate>
            <atom:updated>2026-03-27T17:54:02.032Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vFACdmQ_KM9EMlAqbedVZw.png" /></figure><p>In this article, we will learn how to make a forecast from a time series using TensorFlow and Keras in Python. We will use a sequential neural network created in TensorFlow based on<strong> bidirectional LSTM</strong> layers to capture the patterns in the <strong>univariate sequences</strong> that we will input into the model.</p><p>In particular, we will see how to</p><ul><li>generate synthetic data to simulate a time series with different characteristics.</li><li>process the data into training and validation sets and create a dataset based on time windows.</li><li>define an architecture for our neural network using LSTM (long short-term memory).</li><li>train and evaluate the model.</li></ul><p>The objective of this tutorial is to predict a point in the future given a sequence of data. The multi-step case will not be covered—the one where the prediction is made for the previous point, which in turn was generated by the model.</p><p>Let’s get started.</p><h3>Data generation</h3><p>Instead of downloading a dataset from the web, we will use functions that allow us to generate plausible and usable time series for our case. We will also use a <strong>dataclass </strong>to store the parameters of our time series in a class so that we can use it regardless of the scope. The name of the dataclass will be G, which stands for “global.”</p><p>I decided to use this approach instead of using a real dataset because this method allows us to test more time series and have flexibility in the creative process of our projects.</p><pre>def plot_series(time, series, format=&quot;-&quot;, start=0, end=None):<br>    &quot;&quot;&quot;Helper function for plotting the graph of the temporal series&quot;&quot;&quot;<br>    plt.plot(time[start:end], series[start:end], format)<br>    plt.xlabel(&quot;Time&quot;)<br>    plt.ylabel(&quot;Value&quot;)<br>    plt.grid(False)<br><br>def trend(time, slope=0):<br>    &quot;&quot;&quot;Definition of a trend through slope and time&quot;&quot;&quot;<br>    return slope * time<br><br>def seasonal_pattern(season_time):<br>    &quot;&quot;&quot;Arbitrary definition of a seasonal pattern &quot;&quot;&quot;<br>    return np.where(season_time &lt; 0.1,<br>                    np.cos(season_time * 6 * np.pi),<br>                    2 / np.exp(9 * season_time))<br><br>def seasonality(time, period, amplitude=1, phase=0):<br>    &quot;&quot;&quot;Repeat a pattern at every period&quot;&quot;&quot;<br>    season_time = ((time + phase) % period) / period<br>    return amplitude * seasonal_pattern(season_time)<br><br>def noise(time, noise_level=1, seed=None):<br>    &quot;&quot;&quot;Add white noise to the series&quot;&quot;&quot;<br>    rnd = np.random.RandomState(seed)<br>    return rnd.randn(len(time)) * noise_level</pre><p>Let’s generate a synthetic time series with this code.</p><pre>def generate_time_series():<br>    # The temporal dimension: 4 years of data<br>    time = np.arange(4 * 365 + 1, dtype=&quot;float32&quot;)<br><br>    # The initial series is just a right line that we will later modify with the other function s<br>    y_intercept = 10<br>    slope = 0.005<br>    series = trend(time, slope) + y_intercept<br><br>    # Adding seasonality<br>    amplitude = 50<br>    series += seasonality(time, period=365, amplitude=amplitude)<br><br>    # Adding noise<br>    noise_level = 3<br>    series += noise(time, noise_level, seed=51)<br>    <br>    return time, series<br><br><br># Save the parameters of our temporale series<br>@dataclass<br>class G:<br>    TIME, SERIES = generate_time_series()<br>    SPLIT_TIME = 1100 # The training period will end on day 1100. The rest will be validation set<br>    WINDOW_SIZE = 20 # the number of days  we  will take into consideration for making our prediction<br>    BATCH_SIZE = 32 # how many items to provide per batch<br>    SHUFFLE_BUFFER_SIZE = 1000 # this parameter is needed to define the sampling buffer of Tensorflow<br>    <br>    <br># plot the synthetic series<br>plt.figure(figsize=(10, 6))<br>plot_series(G.TIME, G.SERIES)<br>plt.show()</pre><p>The obtained series is the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*pJ55hf8ugOLf14wjocyjng.png" /><figcaption>synthetic temporal series</figcaption></figure><p>Now that we have a usable time series,<strong> let’s move on to preprocessing.</strong></p><h3>Preprocessing of the time series for deep learning</h3><p>The peculiarity of time series is that they must be divided into training and validation sets, which in turn must be divided into sequences of a length defined by our configuration. These sequences are called <strong>windows</strong>, and the model will use these very sequences to produce a prediction.</p><p>Let’s look at two more helper functions to achieve this goal.</p><p>Here, some explanations are due. The function train_val_split simply divides our series based on the value G.SPLIT_TIME previously defined in the dataclass G. Moreover, we will pass the parameters G.TIME and G.SERIES to it.</p><p>Let’s recall the definition of the dataclass G.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*iM_nr1UFbs6eEzs3dKEkaQ.png" /></figure><p>Based on generate_time_series,</p><p>TIME = range(0, 1439)</p><p>SERIES = array([ 0.81884814, 0.82252744, 0.77998762, ..., -0.44389692, -0.42693424, -0.39230758])</p><p>Both are of length 1439.</p><p>The division of train_val_split will therefore be equal to</p><p>time_train = range(0, 1100)</p><p>time_val = range(1100, 1439)‍</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*CJLUWyJAJB_MV6GchBG1zA.png" /><figcaption>how a time series is divided into X and y</figcaption></figure><p>After splitting into training and validation sets, we will use TensorFlow functions to create a Dataset object that will allow us to create the features X and the target y. Let’s remember that X is the n values that the model will use to predict the next one, which would be y.</p><p>Let’s see how to implement these functions.</p><pre>def train_val_split(time, series, time_step=G.SPLIT_TIME):<br>  &quot;&quot;&quot;Divide the time series into training e validation sets&quot;&quot;&quot;<br>    time_train = time[:time_step]<br>    series_train = series[:time_step]<br>    time_valid = time[time_step:]<br>    series_valid = series[time_step:]<br><br>    return time_train, series_train, time_valid, series_valid<br><br>def windowed_dataset(series, window_size=G.WINDOW_SIZE, batch_size=G.BATCH_SIZE, shuffle_buffer=G.SHUFFLE_BUFFER_SIZE):<br>   &quot;&quot;&quot;<br>  Create some temporal windows for creating  some features X and y.<br>  If, for example, we choose a window of 30, we will create a dataset formed by 30 points as X<br>  &quot;&quot;&quot;<br>  dataset = tf.data.Dataset.from_tensor_slices(series)<br>    dataset = dataset.window(window_size + 1, shift=1, drop_remainder=True)<br>    dataset = dataset.flat_map(lambda window: window.batch(window_size + 1))<br>    dataset = dataset.shuffle(shuffle_buffer)<br>    dataset = dataset.map(lambda window: (window[:-1], window[-1]))<br>    dataset = dataset.batch(batch_size).prefetch(1)<br>    return dataset</pre><p>We will use the .window() method of Tensorflow on the dataset object to apply a shift of 1 to our points. An example of the applied logic can be seen here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*sfqEzuolRV8EmjHuL9Hjjw.png" /></figure><p>In the example, we create a range from 0 to 10 with TensorFlow, and we apply a window of 5. A total of 5 columns will therefore be created. By passing shift=1, each column will have one less value starting from the top (shifted), and drop_remainder=True will ensure that the matrix always has the same size.</p><p>Let’s apply these two functions.</p><pre># create the dataset with temporal windows<br>dataset = windowed_dataset(series_train)<br><br># divide into training and validation sets<br>time_train, series_train, time_valid, series_valid = train_val_split(G.TIME, G.SERIES)</pre><p>Now that the data is ready, we can focus on structuring our neural network.</p><h3>Neural network architecture</h3><p>As mentioned at the beginning of the article, our neural network will primarily be based on<strong> LSTM (long-short-term memory) </strong>layers. What makes an LSTM suitable for this type of task is that it has an internal structure capable of propagating information through long sequences. This makes them very useful in natural language processing (NLP) and indeed in time series, precisely because these two types of tasks might require transferring information throughout the entire sequence.</p><p>Nel codice che segue vedremo come gli strati LSTM siano inclusi all’interno di uno strato Bidirectional. <strong>Questo strato permette alla LSTM di considerare la sequenza di dati in entrambe le direzioni e di poter quindi avere contesto non solo del passato ma anche del futuro</strong>. Ci aiuterà a costruire una network che sarà più precisa rispetto a una unidirezionale.</p><p>Besides LSTMs, there are also GRUs (Gated Recurrent Units) that can be used for time series prediction tasks.</p><p>We will also use the Lambda layer, which will allow us to correctly adapt the input data format to our network, and finally, a dense layer to calculate the final output.</p><p>Let’s see how to implement all this in Keras and TensorFlow sequentially.</p><p>The Lambda layer allows the use of custom functions within a layer. We use it to ensure that the input dimensionality is suitable for the LSTM, since our dataset consists of two-dimensional time windows, where the first is the batch_size and the other is timesteps.</p><p>However, an LSTM accepts a third dimension, which indicates the number of dimensions of our input. Using the Lambda function and input_shape = [None], we are essentially telling Tensorflow to accept any type of input dimension. This is convenient because we don’t have to write additional code to ensure that the dimensionality is correct.</p><p>To read more about LSTM and how the layer works, I invite the reader to consult the official TensorFlow guide available <a href="https://www.tensorflow.org/api_docs/python/tf/keras/layers/">here</a>.</p><p>All LSTM layers are included within a Bidirectional layer, and each of them passes the processed sequences to the next layer through return_sequences = True. If this argument were false, TensorFlow would throw an error, as the next LSTM layer would not find a sequence to process. The only layer that should not return sequences is the last LSTM, as the final dense layer is responsible for providing the final prediction and not another sequence.</p><h3>The EarlyStopping callback</h3><p>we will use a callback to stop the training when our performance metric reaches a specified level. We will use the <strong>MAE (Mean Absolute Error)</strong> to measure how well our network is able to correctly predict the next point in the series.</p><p>In fact, this is a task similar to a regression task, and therefore it will use similar performance metrics.</p><p>Let’s see the code to implement early stopping.</p><pre>def create_uncompiled_model():<br>    # definiamo un modello sequenziale<br>    model = tf.keras.models.Sequential([ <br>        tf.keras.layers.Lambda(lambda x: tf.expand_dims(x, axis=-1),<br>                      input_shape=[None]),<br>        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(1024, return_sequences=True)),<br>        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(512, return_sequences=True)),<br>        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(256, return_sequences=True)),<br>        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(128, return_sequences=True)),<br>        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),<br>        tf.keras.layers.Dense(1),<br>    ]) <br><br>    return model</pre><h3>Model training</h3><p>We are ready to train our LSTM model. Let’s define a function that will call create_uncompiled_model and provide the model with a loss function and an optimizer.</p><p>The <em>Huber loss function</em> can be used to balance between the mean absolute error, or MAE, and the mean squared error, MSE. It is therefore a good loss function when you have variable data or only a few outlier values, as in this case.</p><p>The Adam optimizer is generally a valid choice—we set an arbitrary learning rate of 0.001.</p><pre>def create_model():<br>    tf.random.set_seed(51)<br>  <br>    model = create_uncompiled_model()<br>  <br>    model.compile(loss=tf.keras.losses.Huber(), <br>                  optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),<br>                  metrics=[&quot;mae&quot;])  <br>    return model<br>  <br>model = create_model()<br><br># train for 20 epochs and assign la callback<br>history = model.fit(dataset, epochs=20, callbacks=[early_stopping])</pre><p>Let’s launch the training.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*Tkqf9DkwrC9hLyIifQeP1A.png" /><figcaption>Results of training</figcaption></figure><p>Let’s see how at the 20th epoch the MAE target is reached and the training is stopped.</p><h3>Model evaluation</h3><p>Let’s plot the curves for the loss and the MAE.</p><pre># plotting MAE and loss<br>plt.figure(figsize=(10, 6))<br>plt.plot(history.history[&#39;mae&#39;], label=&#39;mae&#39;)<br>plt.plot(history.history[&#39;loss&#39;], label=&#39;loss&#39;)<br>plt.legend()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*5z6Jc8WUHm8GicWNRQv4gQ.png" /></figure><p>The curves show an improvement of the net until it stabilizes already after the 5th epoch. It’s still a good result.</p><p>We’ll use a helper function for easy access to MAE and MSE. Additionally, we also define a function to create the predictions.</p><pre>def compute_metrics(true_series, forecast):<br>    &quot;&quot;&quot;Helper for printing MSE e MAE&quot;&quot;&quot;<br>    mse = tf.keras.metrics.mean_squared_error(true_series, forecast).numpy()<br>    mae = tf.keras.metrics.mean_absolute_error(true_series, forecast).numpy()<br><br>    return mse, mae<br><br>def model_forecast(model, series, window_size):<br>  &quot;&quot;&quot;Function to convert the input series into a Dataset with time windows for the prediction&quot;&quot;&quot;<br>    ds = tf.data.Dataset.from_tensor_slices(series)<br>    ds = ds.window(window_size, shift=1, drop_remainder=True)<br>    ds = ds.flat_map(lambda w: w.batch(window_size))<br>    ds = ds.batch(32).prefetch(1)<br>    forecast = model.predict(ds)<br>    return forecast</pre><p>Let’s see how the model performs now! Let’s make predictions on the entire series and the validation set.</p><pre># Prediction on the whole time series <br>all_forecast = model_forecast(model, G.SERIES, G.WINDOW_SIZE).squeeze()<br><br># Part for validation<br>val_forecast = all_forecast[G.SPLIT_TIME - G.WINDOW_SIZE:-1]<br><br># Plot<br>plt.figure(figsize=(10, 6))<br>plt.plot(series_valid, label=&quot;validation set&quot;)<br>plt.plot(val_forecast, label=&quot;predicted&quot;)<br>plt.xlabel(&quot;Timestep&quot;)<br>plt.ylabel(&quot;Value&quot;)<br>plt.legend()<br>plt.show()</pre><p>Let’s see the results on the validation set.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*CzAezaRFZ3t5M7H2xqXPDQ.png" /></figure><p>And on the entire series</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*SGfCHp1WOb1G5UdgUU983Q.png" /></figure><p>The results seem to be good. Perhaps we have a way to improve the model’s performance by increasing the training epochs or by tweaking the learning rate.</p><pre>mse, mae = compute_metrics(series_valid, val_forecast)<br>print(f&quot;mse: {mse:.2f}, mae: {mae:.2f}&quot;)</pre><p><strong>mse: 30.91, mae: 3.32 </strong>forecast.</p><h3>Predict a new point in the future</h3><p>Let’s now see how to predict a point in the future given the last sequence in the series.</p><pre>new_forecast = []<br><br>new_forecast_series = G.SERIES[-G.WINDOW_SIZE:] <br><br>pred = model.predict(new_forecast_series[np.newaxis])<br><br>plt.figure(figsize=(15, 6))<br>plt.plot(G.TIME[-100:], G.SERIES[-100:], label=&quot;last 100 points of time series&quot;)<br>plt.scatter(max(G.TIME)+1, pred, color=&quot;red&quot;, marker=&quot;x&quot;, s=70, label=&quot;prediction&quot;)<br>plt.legend()<br>plt.show()</pre><p>And here is the result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*UAI9nVBxXFxKMVxy3dnr_w.png" /></figure><h3>Conclusion</h3><p>Recapping, we have seen how to:</p><ul><li>generate a synthetic time series.</li><li>appropriately split the series into X and y</li><li>structure a neural network in Keras and TensorFlow based on bidirectional LSTMs.</li><li>train with early stopping and evaluate the performance.</li><li>make predictions on the training, validation, and future series</li></ul><p>If you have suggestions on how to improve this flow, write in the comments and share your method. I hope the article can help you with your projects. Thank you for reading!</p><h3>References</h3><ul><li>Schuster, M. &amp; Paliwal, K.K. (1997). <strong><em>Bidirectional Recurrent Neural Networks</em></strong>. IEEE Transactions on Signal Processing, 45(11), 2673–2681.</li><li>Hochreiter, S. &amp; Schmidhuber, J. (1997). <strong><em>Long Short-Term Memory</em></strong><em>.</em> Neural Computation, 9(8), 1735–1780.</li><li>Siami-Namini, S., Tavakoli, N. &amp; Namin, A.S. (2019).<strong> <em>The Performance of LSTM and BiLSTM in Forecasting Time Series</em></strong>. IEEE International Conference on Big Data.</li><li>Alizadegan, H. et al. (2025). <strong><em>Comparative study of LSTM, bidirectional LSTM, and traditional machine learning approaches for energy consumption prediction</em>. </strong>Energy Exploration &amp; Exploitation</li></ul><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df32aa5f579b" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/time-series-forecasting-with-lstm-in-tensorflow-df32aa5f579b">Time series forecasting with LSTM in TensorFlow</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Reinforcement Learning with Python]]></title>
            <link>https://pub.aimind.so/introduction-to-reinforcement-learning-with-python-74e194cb5569?source=rss-fe44f5fcf1d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/74e194cb5569</guid>
            <category><![CDATA[reinforcement-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[python-programming]]></category>
            <dc:creator><![CDATA[Francesco  Franco]]></dc:creator>
            <pubDate>Thu, 05 Mar 2026 07:14:02 GMT</pubDate>
            <atom:updated>2026-03-05T16:36:03.485Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eQMQ_rGvzfhc03X7_ipayQ.png" /><figcaption>image by author</figcaption></figure><p><strong>Reinforcement Learning</strong> (RL) is a branch of machine learning that is distinct from supervised and unsupervised learning. Indeed, the term “reinforcement” refers to a type of reward-based learning capable of solving decision-making problems in which, through a trial-and-error process, the agent autonomously interacts with the environment in which it operates.</p><p>Unlike supervised or unsupervised approaches, RL is based on an iterative learning process where an agent learns through a system of rewards and punishments.</p><p>Practical example: Let’s imagine we need to train our four-legged friend (named Red) for the first time, and we want him to stay still while we slowly move away from him. Red will receive a reward when he executes the command correctly, but he will be reprimanded every time he tries to get up without our permission.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*u7OX9mZAc3uSPkci5kzjMA.jpeg" /><figcaption>Image by author</figcaption></figure><p>This learning process is exactly the same as ours and that of other animals: through interactions with the environment, we come to understand how to act to achieve the desired outcome.</p><p>Similarly, RL utilizes a system of rewards and punishments as positive and negative behavioral signals. RL is distinguished by its interaction-based approach, so the goal will be to learn a<em> policy</em>, i.e., an optimal strategy to maximize the reward.</p><h3>Fundamental concepts</h3><p>The objective of RL is to identify an appropriate model that allows maximizing the total maximum reward obtainable by the agent.</p><p>Let’s look at the key terms needed to understand the fundamentals of RL:</p><ul><li><strong>Environment</strong>—Space in which the agent operates</li><li><strong>State</strong> — State in which the agent is located</li><li><strong>Reward </strong>— Positive feedback obtained from the environment</li><li><strong>Policy</strong> — Defines the actions of the agent</li><li><strong>Value</strong> — Future reward that the agent will receive by performing a particular action</li></ul><p>The <strong>environment</strong> represents the physical or virtual context in which the agent operates, while the<strong> state </strong>describes the condition in which the agent finds itself at a given moment. Every action taken by the agent affects the environment and is evaluated through a reward system.</p><p>The <strong>policy</strong> defines the actions to be taken based on the current state, while the <strong>value</strong> represents an estimate of the future rewards obtainable by performing certain actions.</p><p>These elements work together to guide the agent toward optimal decisions. However, to identify the best action to take, <strong>the agent must face a crucial challenge: explore new states and, at the same time, maximize the expected reward.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*TqnY7ed6FDeZIm1rBszFiQ.jpeg" /><figcaption><a href="https://arxiv.org/pdf/2408.07712?ref=diariodiunanalista.it">https://arxiv.org/pdf/2408.07712</a></figcaption></figure><h4>Exploration vs exploitation</h4><p><strong>The trade-off between exploration and exploitatio</strong>n is one of the key concepts in Reinforcement Learning and is crucial for an agent’s learning.</p><p><strong>Exploration</strong> consists of allowing the agent to try different actions, even if they are not optimal, in order to gather new information about the environment. In other words,<strong> the agent seeks to expand its knowledge to potentially discover actions that could lead to higher rewards in the future</strong>. Exploration, despite the risk of not obtaining the maximum immediate reward, is essential for improving the agent’s strategy in the long term.</p><p>On the other hand, <strong>exploitation</strong> involves using the information already acquired to maximize the reward at a given moment. The agent, at this stage, tends to choose actions that have already yielded the best results in the past, reducing the risks of ineffective actions.</p><p><strong>The compromise between these two aspects lies in the balance that the agent must maintain.</strong> If the agent explores excessively, it risks not obtaining immediate rewards, but it might discover more effective strategies useful in the future. If, on the other hand, it focuses on exploitation while neglecting the exploration phase, it might miss the opportunity to learn new, more profitable strategies.</p><p>Let’s go back for a moment to our beloved Red.</p><p>If the dog, as in our example, makes a mistake and receives a correction (a reprimand), <strong>it does not mean that the learning process is negative!</strong></p><p><strong>Every mistake is a step toward a greater understanding of what it means to stay in place.</strong> The “reprimand” in this case becomes a part of the learning process that will lead the dog to perform the correct action in the future, once it has explored the environment enough and understood the right actions to take.</p><p>In RL, this is equivalent to the agent gathering experiences, even if they are not always immediately rewarding, to optimize its future choices and maximize overall reward.</p><p>Therefore, an optimal learning strategy must dynamically integrate exploration and exploitation, adapting to the evolution of the environment and the information acquired by the agent.</p><h3>Markov Decision Process</h3><p>A theoretical pillar of Reinforcement Learning is represented by <strong>Markov Decision Processes (MDP)</strong>, which provide a mathematical foundation for describing problems where sequential decision-making is necessary.</p><ul><li><strong>States</strong>: they represent all the possible situations in which an agent can find itself within an environment. Each state describes the current conditions of the agent.</li><li><strong>Actions:</strong> they are the choices available to the agent in each state. In every situation, the agent can choose from various actions that determine its next move.</li><li><strong>Rewards</strong>: for every action taken in a given state, the agent receives feedback, positive or negative, which represents how useful that choice was in relation to the goal. This feedback guides the agent’s learning.</li><li><strong>Transitions</strong>: they represent the link between states. When the agent performs an action in a state, it moves to a new state. The transition describes how and with what probability this passage occurs.</li></ul><p>This framework allows for a clear definition of the problems that Reinforcement Learning seeks to solve and provides a useful structure for developing algorithms capable of addressing them. Most RL problems can be modeled as an MDP, making it a fundamental tool for those approaching this discipline.</p><h4>Episodic and continuous tasks</h4><p>A key concept in MDPs is that of a <strong>task,</strong> which is the objective that the agent must achieve. Tasks are divided into two main categories:</p><ul><li><strong>Episodic tasks</strong>: these tasks have a starting point and an endpoint. The agent performs a sequence of actions that leads them to complete an episode, that is, a series of steps that ends in a final state. An example is playing a game of chess, where the episode ends when one of the two players wins or the game ends in a draw.</li><li><strong>Continuous tasks: </strong>In these tasks, the process does not have a defined end, and the agent must constantly interact with the environment without reaching a final state. For example, a control system for an industrial plant does not have a predetermined endpoint but must continuously optimize the production process.</li></ul><p>The distinction between episodic and continuous tasks is important because it influences the way learning algorithms are designed. In episodic tasks, rewards are analyzed at the end of the episode to improve future decisions. In continuous tasks, on the other hand, the agent must continuously update its strategy to adapt to the environment in real-time.</p><p>This distinction offers a conceptual basis for understanding the various approaches to reinforcement learning and helps define the type of problem being addressed.</p><h4>Approaches to identify the optimal policy</h4><p>In an MDP, the goal is to identify an <strong>optimal policy</strong>, that is, a strategy that maximizes the long-term cumulative reward. There are two main approaches to train an agent to achieve this goal:</p><ol><li><strong>Policy-Based Methods</strong>: these methods focus directly on policy learning, that is, on identifying the best action to take in each state. The agent is trained to directly map states to actions, without constructing an explicit representation of the value of the states. This approach is particularly useful in problems with continuous action spaces.</li><li><strong>Value-Based Methods</strong>: In these methods, the agent learns the value of states or actions (through a value function) and chooses the actions that maximize those values. The goal is to accurately estimate how “good” it is to be in a given state or to perform a certain action in terms of expected future rewards.</li></ol><p>The choice between the two approaches depends on the nature of the problem and the characteristics of the environment.</p><h3>Most famous algorithms and approaches</h3><p><strong>Q-Learning </strong>and <strong>SARSA</strong> are two fundamental and widely used algorithms in Reinforcement Learning, both designed to learn an optimal strategy that maximizes cumulative rewards. However, they differ in how they handle exploration and learning.</p><h4>Q-Learning: Off-Policy Learning</h4><p>Q-Learning is an <strong>off-policy</strong> algorithm, which means that the update of the Q-values does not directly depend on the actions taken by the agent during exploration. In other words, Q-Learning assumes that the agent always performs the best possible action (i.e., the one with the highest value), even if in reality it may have chosen differently.</p><p>This approach allows the algorithm to focus more on exploiting long-term optimal strategies without being tied to the policy used during the exploration phase. Thanks to this feature, Q-Learning is particularly effective in learning an optimal strategy even when the agent explores the environment with a policy that balances exploration and exploitation.</p><p>The main advantage of Q-Learning is therefore its ability to learn an optimal strategy, regardless of the actions actually taken during exploration, making it an aggressive and results-oriented algorithm.</p><h4>SARSA: On-Policy Learning</h4><p>Unlike Q-Learning, SARSA is an <strong>on-policy</strong> algorithm, which means that the update of the Q-values is based on the actions actually chosen by the agent during exploration. In this case, the agent updates the Q-values considering both the current state and the action it will choose next, based on its current policy.</p><p>This makes SARSA more “cautious” compared to Q-Learning, as it takes into account the actual decisions made by the agent, instead of making assumptions about an ideal optimal behavior. This characteristic makes it more suitable for contexts where exploration can involve risks, such as in dynamic or unfamiliar environments, where incorrect choices can have negative consequences.</p><h4>Main Differences between Q-Learning and SARSA</h4><ul><li><strong>Approach to learning:</strong> Q-Learning learns independently of the policy used during exploration, while SARSA directly relies on the actions taken by the agent.</li><li><strong>Exploration strategy</strong>: Q-Learning tends to be more aggressive and oriented toward optimal behavior, while SARSA adopts a more conservative approach.</li><li><strong>Application</strong>: Q-Learning is more suitable in static and predictable environments, where exploration does not involve significant risks. SARSA, on the other hand, performs better in dynamic or uncertain environments, where it is important to consider the risks associated with exploration.</li></ul><p>Despite being extremely useful and easy to apply, both algorithms share a common limitation: the difficulty in generalizing to states that have not yet been visited. In practice, they store the Q values for every possible combination of state and action, which makes them inefficient in environments with very large or continuous state spaces. This problem arises in contexts such as robot control or complex video games, where the number of possible states can be extremely high.</p><p>To address these limitations, more advanced algorithms have been developed. For example:</p><ul><li><strong>Deep Q-Network (DQN)</strong>: uses neural networks to approximate Q-values, allowing for the management of very large and continuous state spaces.</li><li><strong>SARSA Lambda</strong>: introduces a generalization mechanism to improve efficiency.</li><li><strong>Actor-Critic</strong>: combines elements of policy learning and value-based learning, enhancing the ability to tackle complex problems.</li></ul><p>Thanks to these advancements, it is now possible to apply Reinforcement Learning even in realistic and complex scenarios that surpass the capabilities of traditional methods. Nevertheless, Q-Learning and SARSA remain fundamental pillars of the field, providing the foundation for understanding and developing more sophisticated algorithms.</p><h4>Deep Q-Network and Deep Deterministic Policy Gradient</h4><p>In the landscape of Reinforcement Learning, algorithms like Deep Q-Network (DQN) and Deep Deterministic Policy Gradient (DDPG) have marked a turning point, allowing for the overcoming of many limitations of traditional approaches based on Q-Learning or policy-based methods. In fact, these algorithms integrate the capabilities of neural networks to tackle complex problems.</p><h4>Deep Q-Networks</h4><p>DQN combines Q-Learning with neural networks to estimate Q-values, which represent the expected value of taking a certain action in a specific state. This approach has proven particularly effective in contexts where the state space is too large to be explicitly represented. The neural network generalizes learning by mapping states to optimal actions without having to explore every possible combination.</p><p>A key innovation of DQN is the use of a separate <strong>target network</strong> to stabilize training. Furthermore, the algorithm implements a process called <strong>experience replay</strong>, which involves storing past experiences (in the form of states, actions, rewards, and next states) in a buffer and reusing them to update the network weights. This mechanism reduces the correlation between the data, improving convergence.</p><h4>Deep Deterministic Policy Gradient</h4><p>DDPG is designed for environments with continuous action spaces, where DQN is not easily applicable. It is based on the principles of Actor-Critic, where two neural networks work in tandem:</p><ul><li><strong>Actor Network:</strong> directly learns the deterministic policy, mapping each state to a specific action.</li><li><strong>Critic Network</strong>: estimate the value of the action suggested by the Actor using a Q-Learning-based approach.</li></ul><p>A distinctive feature of DDPG is the use of <strong>exploratory noise</strong>, which adds perturbations to the actions to encourage the agent to explore new strategies. This is particularly useful in environments with complex dynamics and high-dimensional action spaces.</p><h4>Applications of advanced algorithms</h4><p>These algorithms represent the state of the art in Reinforcement Learning and find application in a wide range of sectors. In the field of robotics, for example, DDPG allows robots to learn precise movements in three-dimensional environments, such as grasping objects or navigating crowded spaces. In the gaming sector, DQN has been used to surpass human records in various Atari video games, demonstrating its effectiveness in managing complex and dynamic environments.</p><p>The adoption of such algorithms allows one not only to tackle problems of high computational complexity but also to explore new horizons where Reinforcement Learning can have a decisive impact.</p><h3>Python Example</h3><p>Let’s now see an application in Python using the OpenAI Gymnasium package.</p><p><a href="https://gymnasium.farama.org/">Gymnasium Documentation</a></p><p>One of the main features of Gymnasium is the availability of a wide variety of preconfigured environments, each with a different level of complexity and a specific purpose for training and testing reinforcement learning algorithms.</p><p>Among these, the <a href="https://gymnasium.farama.org/environments/classic_control/mountain_car/?ref=diariodiunanalista.it">MountainCar</a> environment is a classic introductory problem that allows exploring the basic concepts of RL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*yEzPfGAUKd0tz57xMIYrqQ.gif" /></figure><p>When working with reinforcement learning environments like MountainCar, it is essential to understand the structure of the observation and action spaces, which define the possible states and actions that the agent can take.</p><ol><li><strong>Observation space</strong> (observation_space): In the case of MountainCar, the environment returns a Box type space. This type of space is used to represent continuous values (such as the position and speed of the car in MountainCar). The shape of the Box for MountainCar is (2,), which means that the observation is a vector of two values, one for the position and one for the speed of the car.</li></ol><ul><li><strong>Car position</strong>: The first value in the array represents the car’s position along the valley, which ranges from -1.2 (the left end) to 0.6 (the right end).</li><li><strong>Car speed: </strong>The second value in the array represents the speed of the car, which can range from -0.07 to 0.07.</li></ul><p>The Box function constrains these values with a lower and upper bound, which means that any observation returned by the system will always be a vector that falls within these limits. For example, a position of -1.2 and a velocity of 0.0 are a valid combination for the initial state.</p><p>2. <strong>Action space</strong> (action_space): the environment defines a Discrete(3) action space, which means there are three possible actions. These actions are discrete and correspond to:</p><ul><li><strong>0</strong>: Accelerate to the left.</li><li><strong>1</strong>: Do not accelerate (stay still).</li><li><strong>2</strong>: Accelerate to the right.</li></ul><p>In MountainCar, the agent must choose between these three actions to try to get the car up the mountain. There are no other actions or combinations, which makes the action a discrete set of finite choices.</p><pre>import time <br><br># Number of steps you run the agent for <br>num_steps = 1500<br><br>obs = env.reset()<br><br>for step in range(num_steps):<br>    # take random action, but you can also do something more intelligent<br>    # action = my_intelligent_agent_fn(obs) <br>    action = env.action_space.sample()<br>    <br>    # apply the action<br>    obs, reward, done, info = env.step(action)<br>    <br>    # Render the env<br>    env.render()<br><br>    # Wait a bit before the next frame unless you want to see a crazy fast video<br>    time.sleep(0.001)<br>    <br>    # If the epsiode is up, then start another one<br>    if done:<br>        env.reset()<br><br># Close the env<br>env.close()<br><br><br>type(env.observation_space)<br><br>#OUTPUT -&gt; gym.spaces.box.Box<br><br><br>print(&quot;Upper Bound for Env Observation&quot;, env.observation_space.high)<br>print(&quot;Lower Bound for Env Observation&quot;, env.observation_space.low)<br><br><br>OUTPUT:<br>Upper Bound for Env Observation [0.6  0.07]<br>Lower Bound for Env Observation [-1.2  -0.07]</pre><p>The first step in the code is the creation and initialization of the environment, which represents the world in which the agent will operate. In this case, the environment is MountainCar, where the car must be pushed from a valley to the top of a mountain. The environment is reset, bringing it back to the initial state where the car is positioned between two mountains. Immediately after the reset, the environment is closed, but this step could only be used to free up resources in a larger context, such as in the case of more complex simulations.</p><p>The environment has an “observation space” that describes what the agent can perceive. In the case of MountainCar, the agent can observe the car’s <strong>position </strong>and the car’s <strong>speed</strong>. Every time the agent examines the environment, it receives an update on these values. These values are limited, so the environment has an “upper limit” and a “lower limit” for each of these two variables, namely position and velocity. For example, the position can vary from -1.2 (very far left in the valley) to 0.6 (where the car can start to climb the mountain), and the speed is limited between -0.07 and 0.07.</p><p>Once the environment has been initialized and the agent has access to the information, it enters a continuous cycle of interaction with the environment. The agent must take actions to explore the environment. Each “step” of the cycle represents a single iteration in which the agent:</p><ul><li><strong>Chooses an action</strong>: In this case, the agent does not make decisions based on an intelligent strategy, but chooses an action at random, such as accelerating to the left, doing nothing, or accelerating to the right. The idea is to test the environment randomly to collect data on how the car behaves in different situations.</li><li><strong>Applies the action</strong>: After choosing the action, the agent applies this choice to the environment. For example, if the chosen action is “accelerate to the right,” the car will move to the right.</li><li><strong>Observes the result</strong>: Every time the agent performs an action, the environment returns a new state, which includes the new position and speed of the car, and a “reward,” which indicates how useful the action was in achieving the goal (climbing the mountain).</li><li><strong>Renders the environment: </strong>After applying the action, the environment is “rendered,” that is, visualized, to show what is happening. The agent can see the car moving and trying to climb the mountain.</li><li><strong>Waits a short period</strong>: To prevent the simulation from running too quickly (so that the user can follow the progress), the code introduces a small delay between the various steps.</li><li><strong>Checks if the episode is over</strong>: If the car reaches the top of the mountain or if the episode ends for some other reason, the environment is reset and a new episode begins.</li></ul><p>The cycle continues for a predetermined number of steps (1500 in this case). Each episode begins with the car positioned in the valley and ends when the car reaches the goal (climbing the mountain) or after the number of steps has been exhausted. When an episode ends, the environment is reset and the cycle starts again.</p><p>The agent, acting randomly, explores the <em>MountainCar </em>environment. Initially, the agent does not know how to reach the goal and performs actions randomly. However, this random exploration is fundamental because it allows the agent to collect data that will later be used to improve its decisions, eventually applying more advanced reinforcement learning algorithms that optimize actions based on previous experiences.</p><p>Essentially, this cycle represents the behavior of an agent that iteratively explores an environment, gathering experiences to progressively improve its actions, a crucial step in developing a reinforcement learning agent that can effectively solve the MountainCar problem.</p><h3>Conclusion</h3><p>In summary,</p><ul><li>Reinforcement Learning is all about the cumulative maximization of reward.</li><li>In order to optimize the policy, we have two options: Policy-based methods that directly optimize the policy and Value-based methods that indirectly optimize the policy.</li><li>Key terms of RL are agent, states, actions, and rewards.</li></ul><p>Reinforcement Learning represents one of the most fascinating and powerful approaches in the field of artificial intelligence. With its applications ranging from video games to robotics, and even the optimization of complex systems, it offers a unique opportunity to solve problems where autonomous learning and adaptation are crucial elements.</p><p>In this guide, we have introduced the key concepts, main algorithms, and basic techniques that will allow you to take your first steps into the world of Reinforcement Learning.</p><p>Reinforcement Learning is a constantly evolving field, and your curiosity is the best ally for exploring new frontiers. Keep experimenting, put your knowledge to the test, and never stop learning.</p><h3>References</h3><ul><li><strong>“Reinforcement Learning: An Introduction”</strong> — Sutton &amp; Barto (2nd ed., 2018)</li><li><strong>“Algorithms for Reinforcement Learning”</strong> — Csaba Szepesvári (2010)</li><li><strong>“Deep Learning”</strong> — Goodfellow, Bengio &amp; Courville (2016</li><li><strong>DQN</strong> — Mnih et al. (2015). <em>“Human-level control through deep reinforcement learning”</em></li><li><strong>Policy Gradients</strong> — Williams (1992). <em>“Simple statistical gradient-following algorithms for connectionist reinforcement learning”</em></li><li><strong>Actor-Critic / A3C</strong> — Mnih et al. (2016). <em>“Asynchronous Methods for Deep Reinforcement Learning”</em> — ICML.</li></ul><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74e194cb5569" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/introduction-to-reinforcement-learning-with-python-74e194cb5569">Introduction to Reinforcement Learning with Python</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build an AI Agent with Python and LangChain: a step-by-step guide]]></title>
            <link>https://pub.aimind.so/build-an-ai-agent-with-python-and-langchain-a-step-by-step-guide-588515363425?source=rss-fe44f5fcf1d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/588515363425</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Francesco  Franco]]></dc:creator>
            <pubDate>Mon, 23 Feb 2026 09:34:21 GMT</pubDate>
            <atom:updated>2026-02-24T16:57:22.401Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I4k-ZqrqeklkmgdUEYzvRw.png" /></figure><p>In the fascinating world of artificial intelligence, agents are emerging as one of the most exciting frontiers. These advanced programs are <strong>capable of perceiving their surrounding environment, acting within it, and making intelligent decisions to achieve specific goals.</strong> It’s a concept that evokes science fiction scenarios, but it’s rapidly becoming a reality thanks to frameworks like LangChain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*dMh28NfIuPsD11FWP8rtvw.avif" /></figure><p><strong>LangChain is a revolutionary development tool that allows you to create agents powered by the incredible power of large language models (LLMs)</strong>. These agents can be trained to perform a wide range of tasks, from data analysis and content generation to solving complex problems. The beating heart of a LangChain agent is its LLM, which acts as the “<strong>brain</strong>” capable of processing requests and offering reasoned responses.</p><p>But what truly makes these agents special is their ability to interact with the surrounding world through “<strong>tools</strong>.” These tools are essentially functions or classes that<strong> allow the agent to perform actions such as retrieving information from an API, updating a calendar, or querying a database</strong>. It’s like equipping the agent with virtual arms and legs to operate in the digital environment.</p><p>In this comprehensive guide, we will explore the process of creating a simple math agent in LangChain<strong>. Step by step, we will build an agent capable of performing basic arithmetic operations like addition, multiplication, and calculating the square of a number</strong>. Although simple, this project will help us understand the fundamental concepts underlying LangChain agents and prepare us to develop more complex agents in the future.</p><p>Get ready to dive into the code and discover how to define custom tools, combine them into toolkits, and integrate them with OpenAI language models. Along the way, you’ll also learn how to configure effective prompts and execute agents using LangChain’s AgentExecutor.</p><p>Whether you’re a beginner or an expert, this practical guide <strong>will provide you with a solid foundation for mastering AI agent creation with LangChain.</strong></p><h3>What is a Large Language Model (LLM)?</h3><p>A Large Language Model (LLM) is a machine learning model trained on massive amounts of textual data. These models are capable of understanding and generating natural language in an extremely sophisticated way. Well-known examples of LLMs include OpenAI’s GPT-3 and Google’s LaMDA.</p><p>An LLM is not simply a search system or a digital encyclopedia. It’s a <strong>generative</strong> model that can produce original and coherent outputs based on the provided context. It can perform tasks such as summarizing texts, answering questions, translating languages, and even generating code. Its extraordinary ability lies in recognizing hidden patterns and relationships within the data it has been trained on, allowing it to provide meaningful and relevant responses to complex queries. LLMs represent<strong> a huge leap forward </strong>in natural language processing and are powering many of today’s most advanced artificial intelligence applications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*Hq-8W3si03ZDzIcd9t68_Q.avif" /></figure><h3>The Difference Between LLMs and AI Agents</h3><p>Although Large Language Models (LLMs) are extremely powerful, they cannot take concrete actions on their own outside of text generation. An LLM like ChatGPT can provide you with detailed information on how to book a flight, but it cannot actually connect to a booking site and purchase the ticket for you.</p><p><strong>AI Agents,</strong> on the other hand, are systems that not only understand requests and generate responses like an LLM, but can also i<strong>nteract with the external world</strong> through a series of “tools” or functions. For example, an AI agent connected to your calendar could not only tell you what appointments you have scheduled but also add or modify events at your request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*SYnn_kb1_0Xa69mvozGkIg.avif" /></figure><p>LangChain agents leverage the power of an LLM as their “brain” to understand requests and decide which actions to take. However, unlike a simple LLM, they have access to<strong> toolkits </strong>that allow them to interact with APIs, databases, calendars, and other external systems. This ability to “act” on the external world, in addition to understanding and generating language, makes AI agents extremely versatile and powerful.</p><h3>Build Your AI Agent with Python and LangChain</h3><p>LangChain is a Python library that simplifies the creation of highly sophisticated AI agents. It allows you to combine the power of an LLM with a wide range of tools and data sources, essentially transforming the language model into a “virtual assistant” capable of performing complex tasks on your behalf.</p><h4>Set up the working environment</h4><p>To start building our agent with LangChain, we first need to set up a Python development environment. Here are the steps:</p><ol><li><strong>Create a new project directory and a Python file:</strong></li></ol><pre>mkdir simple-math-agent &amp;&amp; cd simple-math-agent <br>touch math-agent.py</pre><p>2.<strong> Create and activate a Python virtual environment:</strong></p><pre>python -m venv .venv<br>. .venv/bin/activate</pre><p>3. <strong>Install the LangChain and langchain_openai libraries:</strong></p><pre>pip install langchain langchain_openai</pre><p>With the working environment set up, we’re ready to dive into the fantastic world of LangChain agents!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*4XcpQj0sjK6BTfBs0dnz8A.avif" /></figure><h4>The Tools: The Heart of the Agent</h4><p>The simplest starting point will be to define the tools for our mathematical agent. We want to equip it with tools for “addition,” “multiplication,” and “squaring,” so it can perform these operations on the questions we ask it. By keeping the tools simple, we can concentrate on the fundamental concepts and create them ourselves, rather than relying on existing and more complex tools such as the WikipediaTool, which acts as a wrapper around the Wikipedia API and requires importing it from the LangChain library.</p><p>Once again, we’re not trying to do anything extraordinary here. We’re just keeping things simple and assembling the main building blocks of an agent so we can understand how they work and get our first agent up and running.</p><h4>Create the “Addition” Tool</h4><p>Let’s start with the “add” tool (addition). The “bottom-up” way to create a Tool in LangChain would be to extend the <strong>BaseTool</strong> class, set the name and description fields on the class, and implement the<strong> _run</strong> method. It would look like this:</p><pre>from langchain_core.tools import BaseTool<br><br>class AddTool(BaseTool):<br><br>    name = &quot;add&quot; <br><br>    description = &quot;Somma due numeri&quot;<br><br>    args_schema: Type[BaseModel] = AddInput<br><br>    return_direct: bool = True<br><br>    def _run(self, a: int, b: int, run_manager: Optional[CallbackManagerForToolRun] = None) -&gt; str:<br><br>        return a + b</pre><p>As you can see, we need to implement the <strong>_run</strong> method to show what our tool does with the parameters it is given.</p><p>Note also that it requires a Pydantic model for the <strong>args_schema</strong>. We will define it here:</p><pre>AddInput<br><br>    a: int = Field(description=&quot;primo numero&quot;)<br><br>    b: int = Field(description=&quot;secondo numero&quot;)</pre><h4>Simplify with Decorators</h4><p>Now, LangChain offers us a simpler way to define tools, instead of having to extend the <strong>BaseTool </strong>class every time. We can do this with the help of the <strong>@tool</strong> decorator. Defining the “add” tool in LangChain using the <strong>@tool</strong> decorator would look like this:</p><pre>from langchain.tools import tool<br><br>@tool<br>def add(a: int, b: int) -&gt; int:<br><br>    &quot;&quot;&quot;Add two numbers&quot;&quot;&quot; #  this docstring is ued as a description<br><br>    return a + b </pre><p>Much simpler, right? Behind the scenes, the decorator magically uses the provided method to extend the<strong> BaseTool</strong> class, just like we did before. Some things to note:</p><ul><li>The name of the method also becomes the name of the tool.</li><li>The method parameters define the input parameters of the tool.</li><li>The docstring is converted into the tool description.</li></ul><p>You can access these properties on the tool as well.</p><pre>print(add.name) # add<br>print(add.description) # Add 2 numbers<br>print(add.args) # {&#39;a&#39;: {&#39;title&#39;: &#39;A&#39;, &#39;type&#39;: &#39;integer&#39;}, &#39;b&#39;: {&#39;title&#39;: &#39;B&#39;, &#39;type&#39;: &#39;integer&#39;}}</pre><p>Note well: The description of a tool is very important, as it is what the language model uses to decide if that is the right tool for the job. A vague description could lead to not using the tool when it should be used or using it at the wrong times.</p><h4>Other Tools: Multiplication and Square</h4><p>With the “add” tool completed, let’s move on to the definitions of the “multiply” and “square” tools:</p><pre>@tool  <br>def multiply(a: int, b: int) -&gt; int:<br>    &quot;&quot;&quot;Multiply  two numbers.&quot;&quot;&quot;<br>    return a * b<br><br>@tool<br>def square(a) -&gt; int:<br>    &quot;&quot;&quot;Calculate the square of a number.&quot;&quot;&quot;<br>    a = int(a)<br>    return a * a</pre><p><strong>Simple, right?</strong> We have defined our three custom tools. A more common use case might be the utilization of some of the already provided and existing tools in LangChain, which you can see here. However, at the source code level, they would all be created and defined using methods similar to those described earlier.</p><p>And this is all the part related to our Tools. It’s time to combine them into a Toolkit.</p><h4>The Toolkit: The Agent’s Arsenal</h4><p>Toolkits may seem like something sophisticated, but in reality, they are very simple. They are literally a list of tools. We can define our toolkit as an array of tools in this way:</p><pre>toolkit = [add, multiply, square]</pre><p><strong>And that’s it.</strong> Really straightforward and nothing complicated.</p><p>Usually, toolkits are groups of useful tools together and would be helpful for agents trying to complete certain types of tasks.</p><p>For example, an <strong>SQLToolkit </strong>could contain a tool to generate an SQL query, one to validate an SQL query, and one to execute an SQL query.</p><p>The <strong>Integrations Toolkit</strong> page in the LangChain documentation contains an extensive list of toolkits developed by the community that could be useful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*JJQWTaew2pw2au3gUnZVdQ.avif" /></figure><h3>The Large Language Model: The Agent’s Brain</h3><p>As mentioned earlier, an <strong>LLM</strong> (Large Language Model) is the “brain” of an agent. It decides which tools to call based on the question posed to it and what the best next steps to take are based on the description of a tool. It also decides when it has reached the final answer and when it is ready to return it to the user.</p><p>Let’s set up our LLM here:</p><pre>from langchain_openai import ChatOpenAI <br>llm = ChatOpenAI(model=&quot;gpt-3.5-turbo-1106&quot;, temperature=0)</pre><h4>The System Prompt: The Agent’s Guide</h4><p>Finally, we need a prompt to pass to our agent so that it has a general idea of what kind of agent it is and what types of tasks it should solve. Our agent requires a<strong> ChatPromptTemplate</strong> to function (we will discuss it later). This is what a simple ChatPromptTemplate looks like. The main part that interests us is the system prompt, while the rest are just the default settings that we are required to pass.</p><p>In our prompt, we included a sample response, showing the agent how we want it to return only the answer, without adding any descriptive text.</p><pre>prompt = ChatPromptTemplate.from_messages(<br>   [<br>        (&quot;system&quot;, &quot;&quot;&quot;<br>          You are a matematical assistent. Use your instruments to answer the questions.<br>          If you don&#39;t have an instrument to answer the question, declare it.<br><br>          Return only the answers. E.g.:<br>          Human: How much is 1 + 1?<br>          AI: 2<br>        &quot;&quot;&quot;),<br><br>        MessagesPlaceholder(&quot;chat_history&quot;, optional=True),<br>        (&quot;human&quot;, &quot;{input}&quot;),<br>        MessagesPlaceholder(&quot;agent_scratchpad&quot;),<br>    ]<br>)</pre><p><strong>Done</strong>. We have set up our Tools and Toolkit, which our agent will need as part of its configuration so that it knows the types of actions and capabilities at its disposal. And we have also set up the LLM and the system prompt.</p><p>Now comes the fun part: configure our Agent!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*0Aumhi4wWE6qa0fwAEAP4Q.avif" /></figure><h3>Choose the Type of Agent</h3><p>LangChain offers different types of agents that can be created, with varying reasoning capabilities and skills. We will use the most powerful and advanced agent currently available: the<strong> OpenAI Tools Agent</strong>. As described in the documentation, this agent also leverages the new OpenAI models:</p><p><strong>The new OpenAI models have been trained to detect when one or more functions need to be called and respond with the inputs that should be passed to those function</strong>s. In an API call, you can describe the functions and the model will intelligently choose to return a JSON object containing the arguments to call these functions. <strong>The goal of the OpenAI Tools APIs is to reliably return valid and useful function calls compared to what can be done using a generic text completion or chat API.</strong></p><p>In other words, this agent is good at generating the correct structure to call functions and is able to understand if more than one function (tool) might be necessary for our task. T<strong>his agent also has the ability to call functions (tools) with multiple input parameters, just like our tools do. Some agents can only work with functions that have a single input parameter.</strong></p><p>If you know about OpenAI’s Function Calling feature, where we can use OpenAI’s LLM to generate the correct parameters to call a function, the OpenAI Tools Agent we are using here leverages part of that power to be able to call the correct tool with the correct parameters.</p><h4>Configure the AI Agent in LangChain</h4><p>To configure an agent in LangChain, we need to use one of the provided factory methods to create the agent of our choice.</p><p>The factory method to create an OpenAI Tools Agent is <strong>create_openai_tools_agent()</strong>. It requires passing the llm, the tools, and the prompt that we set up earlier. We then initialize our agent:</p><pre>agent = create_openai_tools_agent(llm, toolkit, prompt)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*Xxd3cJpb8pdhLn-w_x4CWA.avif" /></figure><h4>Run the Agent with the AgentExecutor</h4><p>Finally, to execute agents in LangChain, we cannot simply call a “run” method directly on them. They must be executed through an <strong>AgentExecutor.</strong></p><p>I introduce the AgentExecutor only at the end because I don’t believe it is a critical concept for understanding how agents work, and bringing it up right at the beginning would have only made everything more complicated than necessary, in addition to diverting attention from some fundamental concepts.<strong>The AgentExecutor serves as a runtime for agents in LangChain and allows an agent to continue functioning until it is ready to return the final response to the user</strong>. In pseudo-code, the AgentExecutors do something like (taken directly from the LangChain documentation):</p><pre>next_action = agent.get_action(...)<br>while next_action != AgentFinish:<br>    observation = run(next_action)<br>    next_action = agent.get_action(..., next_action, observation)<br>return next_action</pre><p>They are essentially a while loop that keeps calling the next_action methods on the agent until the agent returns its final response. We then configure our agent within the AgentExecutor. We pass the agent to it and we also need to pass the toolkit. We set verbose to True so that we have an idea of what the agent is doing while processing our request:</p><pre>agent_executor = AgentExecutor(agent=agent, tools=toolkit, verbose=True)</pre><p>And there you have it. Now we are ready to give commands to our agent:</p><pre>result = agent_executor.invoke({&quot;input&quot;: &quot;how much is 1 + 1?&quot;})</pre><p>Let’s run our script and see the agent’s output:</p><pre>python math-agent.py</pre><p>Since we set verbose=True on the AgentExecutor, we can see the Action lines that our agent has taken. It identified that it should call the “add” tool, called the “add” tool with the required parameters, and returned the result to us.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*WdVIA317M7NHXVcLQ_htcw.avif" /></figure><p>How exciting to see our agent in action! Now we are ready to test it with different mathematical requests and see how it performs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*Eq01SrVQA0rORVfs5hrOfQ.png" /></figure><h4>Observations</h4><p>After using the agent for a while, I noticed the following:</p><ul><li>When it was asked direct questions for which it had the appropriate tools, it was quite consistent in using the correct tools for the job and providing the correct answer. So, quite reliable in this sense.</li><li>If the question was a bit complicated, like our question “5 raised to the sixth,” it didn’t always return correct results.</li><li>Sometimes it could simply use the pure power of the LLM to answer our question, without invoking our tools.</li></ul><h3>The Future</h3><p>Agents, and programs that can reason autonomously, represent a new paradigm in programming and I believe they will become a much more important part of how many things will be built. <strong>Obviously, the non-deterministic nature (i.e., not entirely predictable) of LLMs means that the results of agents will also suffer, raising doubts about how much we can rely on them for tasks where we need to be sure of the answers we have.</strong></p><p>Perhaps, as the technology matures, their results will become increasingly predictable, and we might develop some alternative solutions for this.</p><p><strong>I can also imagine that agent-type libraries and packages will start to appear</strong>. Just as we install third-party libraries and packages in software, for example through the pip package manager for Python or Docker Hub for Docker images, <strong>we might start to see the development of an agent library and package manager, with agents developed to be very good at specific tasks, which we could then install as packages in our application.</strong></p><p>In fact, the LangChain Toolkit library for agents, listed on their Integrations page, consists of sets of tools built by the community for people to use, which could be an early example of community-built agent-type libraries.</p><h3>Conclusion</h3><p>I hope this has been a useful introduction to start building with agents in LangChain. Remember, agents are fundamentally a brain (the LLM) and a set of tools, which they can use to perform tasks in the world around them.</p><h3>References</h3><ul><li><strong><em>LangChain in Action</em></strong> by Ben Auffarth (Manning, 2024) — Practical coverage of chains, agents, and tools with Python code examples.</li><li><strong><em>Building LLM Powered Applications</em></strong> by Valentina Alto (Packt, 2024) — Covers agent architectures, memory, and tool use with LangChain.</li><li><strong><em>Developing Apps with GPT-4 and ChatGPT</em></strong> by Olivier Caelen &amp; Marie-Alice Blete (O’Reilly, 2024) — Good foundational coverage before diving into agents.</li><li><strong><em>AI Engineering</em></strong> by Chip Huyen (O’Reilly, 2025) — More advanced, covers production agent systems and LLM application design patterns broad</li><li><strong>ReAct: Synergizing Reasoning and Acting in Language Models”</strong> (Yao et al., 2022) — The foundational paper behind most LangChain agent designs. Worth reading to understand <em>why</em> agents work the way they do.</li><li><strong>LangChain Blog</strong> (blog.langchain.dev) — Regularly publishes practical articles on new agent patterns, LangGraph use cases, and best practices.</li><li><strong>“Building Your First LLM Agent Application”</strong> on Towards Data Science — Several authors have written solid introductory walkthroughs there; searching that site for “LangChain agent” yields consistently good results.</li><li><strong>Lilian Weng’s blog</strong> (lilianweng.github.io) — Her post <em>“LLM Powered Autonomous Agents”</em> is one of the best conceptual overviews of agent architectures available.</li></ul><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=588515363425" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/build-an-ai-agent-with-python-and-langchain-a-step-by-step-guide-588515363425">Build an AI Agent with Python and LangChain: a step-by-step guide</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Feature engineering techniques for numerical variables in Python]]></title>
            <link>https://pub.aimind.so/feature-engineering-techniques-for-numerical-variables-in-python-3e172f90aeb0?source=rss-fe44f5fcf1d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e172f90aeb0</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[feature-engineering]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Francesco  Franco]]></dc:creator>
            <pubDate>Mon, 09 Feb 2026 15:06:21 GMT</pubDate>
            <atom:updated>2026-02-17T20:36:11.758Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MmLL1zMdfvLhxytU5BAZEg.png" /><figcaption>Image by author</figcaption></figure><p>Feature engineering is an essential step in a machine learning pipeline, <strong>where raw data is transformed into more meaningful features that help the model better understand the relationships between the data</strong>.</p><p>Feature engineering often means applying transformations to the available data to overwrite or create new ones that, in the context of machine learning and data science, are used to train a model that can perform better precisely because of these transformations.</p><p>In this article, we will explore advanced feature engineering techniques for <strong>handling numerical values</strong> using the Scikit-Learn library in Python, Numpy, and others to make your machine learning models even more effective.</p><p>In summary, by reading this article you will learn</p><ul><li><strong>A solid list of feature engineering techniques for numerical data</strong> from the Scikit-Learn, Numpy, and Scipy suite to improve the performance of machine learning models</li><li><strong>Practical implementation </strong>of logarithmic and Box-Cox transformations to <strong>normalize distributions and linearize relationships in data</strong></li><li><strong>-Specific use cases for each technique </strong>and how they can reveal latent patterns, refine variable representation, and improve model interpretability.</li></ul><h3>Use cases</h3><p>Feature optimization is a key element in improving the quality of machine learning models, especially in the analysis of complex datasets. The targeted application of feature engineering techniques offers several advantages, such as</p><ol><li><strong>Revealing latent patterns in data</strong>: This technique allows for the discovery of hidden relationships and structures that are not immediately evident.</li><li><strong>Refinement of variable representation</strong>: The process transforms raw data into formats more suitable for machine learning.</li><li>Management of challenges related to the<strong> distribution and intrinsic nature of data</strong>: This approach addresses issues such as skewness, outliers, and variable scalability.</li></ol><p>The precise implementation of these feature optimization techniques leads to a significant enhancement in the performance of machine learning models.</p><p>These improvements are reflected in various aspects of the models’ functioning, from their predictive capacity to their interpretability. The superior quality of the features used allows the models to capture<strong> nuances and complex patterns in the data that might otherwise remain hidden</strong>.</p><p><strong>Moreover, feature optimization helps make models more robust and generalizable</strong>, essential characteristics for real-world applications and reducing the possibility of overfitting.</p><p>Let’s get started with some useful feature engineering techniques.</p><h3>Normalization</h3><p>Probably the first numerical feature engineering technique that a data scientist learns—normalization (also known as scaling) is a method in which we modify a variable by subtracting the mean and dividing it by the standard deviation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*OO9pgsXAUmAGvWOeBm7sTA.png" /></figure><p><strong>Performing this transformation means that the resulting variable will have a mean of 0 and a standard deviation and variance of 1.</strong></p><p>In machine learning, especially in deep learning, having variables confined between specific values (for example, precisely 0 and 1) helps the model converge to an optimal solution more quickly.</p><p>In machine learning, especially in deep learning, having variables confined between specific values (for example, precisely 0 and 1) helps the model converge to an optimal solution more quickly.</p><p>This technique is a learned transformation—so we use the training data to derive the correct values of <em>e</em> and then these values are used to perform the transformations when applied to new data.</p><p>It should be noted that this<strong> transformation does not modify the distribution</strong> but rescales the values.</p><h4>Practical application</h4><p>We will use the famous Sklearn <em>wine dataset </em>for a classification task. We will compare the performance using and not using normalization in confusion matrices, using scikit-learn.</p><pre>import matplotlib.pyplot as plt<br>import seaborn as sns<br>from sklearn.datasets import load_wine<br>from sklearn.model_selection import train_test_split<br>from sklearn.preprocessing import StandardScaler<br>from sklearn.neighbors import KNeighborsClassifier<br>from sklearn.metrics import confusion_matrix<br>import numpy as np<br><br>X, y = load_wine(return_X_y=True)<br><br># Divide the data into training and test sets<br>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=42)<br><br># Function for training the model and obtaining the confusion matrix<br>def get_confusion_matrix(X_train, X_test, y_train, y_test):<br>    model = KNeighborsClassifier(n_neighbors=5)<br>    model.fit(X_train, y_train)<br>    y_pred = model.predict(X_test)<br>    return confusion_matrix(y_test, y_pred)<br><br># Obtain the confusion matrix without normalization<br>cm_without_norm = get_confusion_matrix(X_train, X_test, y_train, y_test)<br><br># Normalize the data<br>scaler = StandardScaler()<br>X_train_scaled = scaler.fit_transform(X_train)<br>X_test_scaled = scaler.transform(X_test)<br><br># Obtain the confusion matrix with normalization<br>cm_with_norm = get_confusion_matrix(X_train_scaled, X_test_scaled, y_train, y_test)<br><br># Create the figure with two side-by-side subplots<br>fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 8))<br><br># Function for creating the heatmap<br>def plot_heatmap(ax, cm, title):<br>    sns.heatmap(cm, annot=True, fmt=&#39;d&#39;, cmap=&#39;viridis&#39;, ax=ax, cbar=False)<br>    ax.set_title(title, fontsize=16, pad=20)<br>    ax.set_xlabel(&#39;Predicted&#39;, fontsize=12, labelpad=10)<br>    ax.set_ylabel(&#39;Real&#39;, fontsize=12, labelpad=10)<br><br># Plot the heatmap<br>plot_heatmap(ax1, cm_without_norm, &#39;Confusion Matrix\nWithout Normalization&#39;)<br>plot_heatmap(ax2, cm_with_norm, &#39;Confusion Matrix\nWith Normalization&#39;)<br><br># Add a common colorbar <br>cbar_ax = fig.add_axes([0.92, 0.15, 0.02, 0.7])<br>sm = plt.cm.ScalarMappable(cmap=&#39;viridis&#39;, norm=plt.Normalize(vmin=0, vmax=np.max([cm_without_norm, cm_with_norm])))<br>fig.colorbar(sm, cax=cbar_ax)<br><br># Fix the layout and display the graphic<br>plt.tight_layout(rect=[0, 0, 0.9, 1])<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6wz0453kRtNLb-tAYfRThA.png" /></figure><p>The improvement delta is about 30%—normalization on some algorithms has such a significant impact that failing to apply it properly represents a serious error on the part of the data scientist.</p><p>(There are also variants of normalization. In Sklearn, these are called RobustScaler and MinMaxScaler.)</p><p>From the Sklearn examples, a more complex graph is available that shows the classification boundaries of the KNNClassifier model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RwfzUHNV5yYCYwx-IjDTjg.png" /><figcaption><a href="https://scikit-learn.org/stable/auto_examples/preprocessing/plot_scaling_importance.html?ref=diariodiunanalista.it">https://scikit-learn.org/stable/auto_examples/preprocessing/plot_scaling_importance.html</a></figcaption></figure><h4>Polynomial features</h4><p>Polynomial features are useful for introducing non-linearity into linear models. The PolynomialFeatures class in Scikit-Learn allows you to generate both polynomial features and interaction terms between variables.</p><p><a href="https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PolynomialFeatures.html?ref=diariodiunanalista.it">PolynomialFeatures</a></p><p>Given an original feature <em>x</em>, the polynomial features can include:</p><ul><li>x² (quadratic)</li><li>x³ (cubic)</li><li>x⁴ (fourth power)</li><li>and so on.</li></ul><p>For models with multiple features (x_1, x_2, …, x_n), interaction terms can also be created:</p><ul><li>x_1 x x_2</li><li>x_1² x x_2</li><li>x_1 x x_2²</li></ul><p>and so on.</p><p>The main purpose is to allow linear models to learn non-linear relationships in the data without modifying the base algorithm.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*4nesZovBPmYI7oFCYoSUsw.png" /><figcaption><a href="https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PolynomialFeatures.html?ref=diariodiunanalista.it">https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PolynomialFeatures.html</a></figcaption></figure><p>Their main strength lies in significantly increasing the model’s flexibility, allowing even linear models to capture nonlinear relationships in the data. This translates to:</p><ul><li>Ability to model complex curves and surfaces in the feature space.</li><li>Potential positive contribution of performance on intrinsically non-linear data.</li><li>The model, while capturing nonlinear relationships, maintains a linear base structure. This allows the use of familiar analysis tools and makes it easier to interpret the coefficients compared to complex nonlinear models, although this simplicity decreases with the increase in polynomial degree.</li></ul><p>Another crucial advantage is the ability to reveal hidden interactions between variables. In domains such as physics or economics, where relationships are often non-linear, this feature proves to be particularly valuable.</p><p>As for the disadvantages, however</p><ul><li>It rapidly increases the dimensionality of the dataset, creating additional columns for each feature subjected to the algorithm.</li><li>It can lead to overfitting if used excessively.</li><li>It requires more computational resources, precisely because of the need to process a larger number of features.</li></ul><p>From a practical standpoint, the implementation of polynomial features is relatively simple thanks to scikit-learn in Python. We will see shortly how to do it.</p><h4>Practical Application</h4><p>PolynomialFeatures is a class in Scikit-learn used to generate polynomial features. It is found in the sklearn. preprocessing module.</p><p>The object transforms a degree 1 input array into a new array containing all polynomial terms up to a specific degree. For example, if the original features are [a, b] with degree=2, the features obtained are [1, a, b, a², ab, b²].</p><p>The subjects of the object are as follows:</p><ol><li>degree (int, default=2):</li></ol><ul><li>The degree of the polynomial</li><li>Determines the maximum degree of the generated polynomial terms</li></ul><p>2. interaction_only (bool, default=False):</p><ul><li>If True, only generates interaction terms</li><li>Does not produce powers of individual features</li></ul><p>3. include_bias (bool, default=True):</p><ul><li>If True, include a column of 1s (the bias term)</li><li>Useful when using the result with models that do not have a separate intercept term.</li></ul><p>4. order (‘C’ or ‘F’, default=’C’):</p><ul><li>Determines the output order of the features</li><li>‘C’ for C-style order (last features change faster), ‘F’ for Fortran-style order</li></ul><p>Here’s an example of how to implement the object in scikit-learn.</p><pre>from sklearn.preprocessing import PolynomialFeatures<br>import numpy as np<br><br>X = np.array([[1, 2], [3, 4]])<br>poly = PolynomialFeatures(degree=2, include_bias=False)<br>X_poly = poly.fit_transform(X)<br><br>print(X_poly)<br># Output: [[1. 2. 1. 2. 4.]<br>#          [3. 4. 9. 12. 16.]]<br><br>print(poly.get_feature_names([&#39;x1&#39;, &#39;x2&#39;]))<br># Output: [&#39;x1&#39;, &#39;x2&#39;, &#39;x1^2&#39;, &#39;x1 x2&#39;, &#39;x2^2&#39;]</pre><p>Now these features can inform a machine learning model and possibly help it perform better.</p><h3>FunctionTransformer</h3><p>FunctionTransformer is a versatile tool in Scikit-learn that allows you to incorporate custom functions into the data transformation process. It allows you to apply an <strong>arbitrary function to the data as part of a preprocessing or feature engineering pipeline</strong>. Essentially, it transforms a Python function into a “transformer” object (not like the deep learning model, but the one from Scikit-learn) compatible with the Scikit-learn API.</p><p><a href="https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.FunctionTransformer.html?ref=diariodiunanalista.it#sklearn.preprocessing.FunctionTransformer">FunctionTransformer</a></p><p>FunctionTransformer takes a Python function as the main input and creates a transformer object that, when applied to the data, executes that function. It can also be used in combination with other transformers or within a Scikit-learn pipeline.</p><p>A concrete example is applying the object to a time series to extract trigonometric features.</p><pre>import matplotlib.pyplot as plt<br><br>fig, ax = plt.subplots(figsize=(12, 4))<br>average_week_demand = df.groupby([&quot;weekday&quot;, &quot;hour&quot;])[&quot;count&quot;].mean()<br>average_week_demand.plot(ax=ax)<br>_ = ax.set(<br>    title=&quot;Average hourly bike demand during the week&quot;,<br>    xticks=[i * 24 for i in range(7)],<br>    xticklabels=[&quot;Sun&quot;, &quot;Mon&quot;, &quot;Tue&quot;, &quot;Wed&quot;, &quot;Thu&quot;, &quot;Fri&quot;, &quot;Sat&quot;],<br>    xlabel=&quot;Time of the week&quot;,<br>    ylabel=&quot;Number of bike rentals&quot;,<br>)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oc_8b-U1ypOuX5a3u_flYw.png" /></figure><p>Generally, the typical use of FunctionTransformer is seen in the</p><ul><li>creation of complex or domain-specific features</li><li>application of unusual mathematical operations to the data</li><li>integration of existing preprocessing logics into Scikit-learn pipelines</li></ul><p>FunctionTransformer thus acts as a bridge between custom Python functions and those of Scikit-learn, offering flexibility in data preprocessing and feature engineering.</p><h4>Practical application</h4><p>Let’s apply the function to create trigonometric transformations to the time series shown above.</p><pre>from sklearn.preprocessing import FunctionTransformer<br><br><br>def sin_transformer(period):<br>    return FunctionTransformer(lambda x: np.sin(x / period * 2 * np.pi))<br><br><br>def cos_transformer(period):<br>    return FunctionTransformer(lambda x: np.cos(x / period * 2 * np.pi))<br><br>hour_df = pd.DataFrame(<br>    np.arange(26).reshape(-1, 1),<br>    columns=[&quot;hour&quot;],<br>)<br>hour_df[&quot;hour_sin&quot;] = sin_transformer(24).fit_transform(hour_df)[&quot;hour&quot;]<br>hour_df[&quot;hour_cos&quot;] = cos_transformer(24).fit_transform(hour_df)[&quot;hour&quot;]<br>hour_df.plot(x=&quot;hour&quot;)<br>_ = plt.title(&#39;Trigonometric encoding for the feature &quot;hour&quot;&#39;)</pre><p>If you are interested in learning more, I suggest reading the documentation linked above on the dedicated Sklearn page. The result of the transformation is visible in the graph below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*0A5T8YPQbw8M8uCcSORIjw.png" /></figure><p>This is just one example of application, precisely because this object finds considerable use specifically in time series.</p><h3>KBinsDiscretizer</h3><p>KBinsDiscretizer is a preprocessing class in Scikit-learn designed to <strong>transform continuous features into discrete categorical features.</strong> This process is known as <em>discretization</em>, <em>quantization,</em> or<em> binning</em>. Some datasets with continuous features can benefit from discretization because it can transform the dataset with continuous attributes into one with only nominal attributes.</p><p>Its goal is to divide the range of a continuous variable into a specific number of intervals (or bins). Each original value is then replaced with the label of the bin it falls into.</p><p>The algorithm works like this:</p><ol><li>Analyze the distribution of the continuous feature</li><li>Create a predefined number of bins based on this distribution</li><li>Assign each original value to the appropriate bin</li><li>Replace the original values with the bin labels or with one-hot encodings of the bins</li></ol><p>Key parameters:</p><ol><li>n_bins: Number of bins to create. It can be an integer or an array of integers for different bins for each feature.</li></ol><p>2. encode: Method of bin encoding (onehot, ordinal, or onehot-dense).<br> — <em>onehot:</em> encode the transformed result with one-hot encoding and return a sparse matrix. Ignored features are always stacked on the right.</p><p>— <em>onehot-dense</em>: encodes the transformed result with one-hot encoding and returns a “dense” array (i.e., not in sparse format.</p><p>— <em>ordinal</em>: returns the bin encoded as an integer value</p><p>3. strategy: Strategy for defining bin limits (uniform, quantile, or kmeans).</p><p>— <em>uniform</em> creates bins of equal width</p><p>— <em>quantile</em> creates bins for each feature containing the same number of points</p><p>— <em>kmeans</em> defines the bins using k-means clustering</p><p>You can read more at <a href="https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.KBinsDiscretizer.html?ref=diariodiunanalista.it">this link</a>.</p><p>Some considerations:</p><ul><li>The choice of the number of bins and the strategy can significantly influence the results.</li><li>It can lead to loss of information, especially with few bins (like when we plot a histogram with few groups).</li><li>Useful for algorithms sensitive to non-normal distributions or non-linear relationships.</li></ul><h4>Practical Application</h4><p>We will see the application by observing the performance of linear regression and decision trees in learning continuous patterns vs. discretized ones.</p><p>We will create a fake dataset of random but semi-linear numbers, apply the models to the continuous data, and then next to it, the same dataset but with discretized features.</p><pre>import matplotlib.pyplot as plt<br>import numpy as np<br>from sklearn.linear_model import LinearRegression<br>from sklearn.preprocessing import KBinsDiscretizer<br>from sklearn.tree import DecisionTreeRegressor<br><br># Set the generator of  random numbers for reproducability<br>rnd = np.random.RandomState(42)<br><br># Creation  of  dataset<br>X = rnd.uniform(-3, 3, size=100)  # 100 points betwen -3 and  3<br>y = np.sin(X) + rnd.normal(size=len(X)) / 3  # Sine wih noise added<br>X = X.reshape(-1, 1)  # Reshape to have correct form for sklearn<br><br># Application of  KBinsDiscretizer<br>discretizer = KBinsDiscretizer(n_bins=10, encode=&quot;onehot&quot;)<br>X_binned = discretizer.fit_transform(X)<br><br># Preparation for  the visualization<br>fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True, figsize=(12, 5))<br>line = np.linspace(-3, 3, 1000).reshape(-1, 1)  # Points for plotting<br><br># Function for training and plotting the model<br>def train_and_plot(X_train, X_plot, ax, title):<br>    # Linear regression<br>    linear_reg = LinearRegression().fit(X_train, y)<br>    ax.plot(line, linear_reg.predict(X_plot), linewidth=2, color=&quot;green&quot;, label=&quot;Linear regression&quot;)<br>    <br>    # Decision tree<br>    tree_reg = DecisionTreeRegressor(min_samples_split=3, random_state=0).fit(X_train, y)<br>    ax.plot(line, tree_reg.predict(X_plot), linewidth=2, color=&quot;red&quot;, label=&quot;Decision tree&quot;)<br>    <br>    #  Original data<br>    ax.plot(X[:, 0], y, &quot;o&quot;, c=&quot;k&quot;, alpha=0.5)<br>    ax.legend(loc=&quot;best&quot;)<br>    ax.set_xlabel(&quot;Features of input&quot;)<br>    ax.set_title(title)<br><br># Plot for original data<br>train_and_plot(X, line, ax1, &quot;Results before the discretization&quot;)<br>ax1.set_ylabel(&quot;Output of the regression&quot;)<br><br># Plot for discretized data<br>line_binned = discretizer.transform(line)<br>train_and_plot(X_binned, line_binned, ax2, &quot;Results after the discretization&quot;)<br><br>plt.tight_layout()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mieG4PkmJFGS-IjFpCurxA.png" /></figure><p>Linear regression improves significantly after discretization, better capturing non-linearity. The decision tree shows fewer changes, as it is already capable of handling non-linearity. This example illustrates how discretization can help linear models capture non-linear relationships, <strong>potentially improving performance in certain scenarios.</strong></p><h4>Logarithm transformation</h4><p><strong>The main advantage of the logarithmic transformation is its ability to compress the range of values</strong>. This effect is particularly useful when working with data that exhibit high variability or outliers.</p><p>The logarithm reduces the distance between larger values, keeping smaller ones relatively unchanged. In doing so, it <strong>normalizes asymmetric distributions,</strong> making those with a right tail more symmetric and bringing them closer to a normal distribution.</p><p>Another crucial aspect is its ability to linearize otherwise non-linear relationships. In many cases, the logarithmic transformation can convert exponential relationships into linear ones, simplifying the analysis and improving the performance of models that assume linearity between the variables.</p><p>Its compression function becomes apparent when working with extreme data, allowing for the management of outliers without having to remove them from the dataset, <strong>thus preserving potentially important information.</strong></p><p>Mathematically, the most common transformation uses the natural logarithm (base <em>e</em>), defined as<em> y=ln(x),</em> where<em> x </em>is the original value and <em>y</em> is the transformed one. It is important to note that this transformation is defined only for positive values of <em>x</em> , sometimes requiring the addition of a constant to the data in the presence of zeros or negative values.</p><p>Logarithmic transformation can be used as a feature scaling technique, complementary or alternative to methods such as standardization or min-max normalization. Moreover, it can improve the performance of specific models, such as linear regression, which benefit from features with more symmetric distributions.</p><h4>Practical Application</h4><p>In machine learning, the log transform is most often used when you want to normalize a distribution that is not naturally distributed.</p><p>For example,<strong> a well-known variable that is not normally distributed is annual income </strong>— often, one wants to model this variable to provide value predictions, but working with this distribution is not convenient, especially if using algorithms that do not correctly model non-linear data.</p><p>With a logarithmic transformation, using numpy, it is possible to approach a normal distribution, making the variable more easily predictable.</p><pre>import numpy as np<br>import pandas as pd<br>import matplotlib.pyplot as plt<br><br># Creating a dataset of examples with positively asimmetric numbers<br>np.random.seed(42)<br>data = {<br>    &#39;Income&#39;: np.random.exponential(scale=50000, size=1000)  # exponential distibution to simulate the skewness<br>}<br>df = pd.DataFrame(data)<br><br># Creation of a figure with two side-by-side subgraphs<br>fig, axes = plt.subplots(1, 2, figsize=(15, 5))<br><br># Plot of the original distribution<br>axes[0].hist(df[&#39;Income&#39;], bins=50, color=&#39;blue&#39;, alpha=0.7)<br>axes[0].set_title(&#39;Original Distribution of Income&#39;)<br>axes[0].set_xlabel(&#39;Income&#39;)<br>axes[0].set_ylabel(&#39;Frequency&#39;)<br><br># Application of logarithmic transformation<br>df[&#39;Log_Income&#39;] = np.log1p(df[&#39;Income&#39;])  # log1p is equivalent to log(x + 1)<br><br># Plot of the transformed distribution<br>axes[1].hist(df[&#39;Log_Income&#39;], bins=50, color=&#39;green&#39;, alpha=0.7)<br>axes[1].set_title(&#39;Logarithmic Distribution of Income&#39;)<br>axes[1].set_xlabel(&#39;Log_Income&#39;)<br>axes[1].set_ylabel(&#39;Frequency&#39;)<br><br># Show the plot <br>plt.tight_layout()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WXAyg2EGJoKIs_7J4ulwUw.png" /></figure><h3>PowerTransformer</h3><p>PowerTransformer is a Sklearn preprocessing module that contains logics used to make data more similar to Gaussian data. This is useful for modeling problems related to heteroscedasticity (i.e., non-constant variance) or other situations where normality is desired.</p><p>Currently, PowerTransformer<strong> supports the Box-Cox and Yeo-Johnson transformations</strong>. The optimal parameter for stabilizing variance and minimizing skewness is estimated using maximum likelihood.</p><p>Box-Cox requires that the input data be strictly positive, while Yeo-Johnson supports both positive and negative data.</p><p>In the context of machine learning, these transformations face several common challenges:</p><ul><li><strong>Data normalization:</strong> Many machine learning algorithms, such as linear regression, neural networks, and some clustering methods, assume that the data follows a normal distribution. PowerTransformer can transform asymmetric distributions or those with heavy tails into forms closer to Gaussian, improving the performance of these models.</li><li><strong>Variance stabilization</strong>: In real datasets, the variance of the features often changes with the magnitude of the data. This phenomenon, called heteroscedasticity, can compromise the effectiveness of many algorithms.</li><li><strong>Linearization of relationships</strong>: Some algorithms, such as linear regression, assume linear relationships between variables. PowerTransformer can linearize non-linear relationships, expanding the applicability of these models to complex datasets.</li></ul><p>Let’s start with the Box-Cox transformation, which is mathematically defined as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/1*5-3SdrR4QqEDawvTx3yw9Q.png" /></figure><p>where<em> x</em> is the original value and <em>λ</em> is the transformation parameter that is inferred at the algorithmic level.</p><p>(PowerTransformer si comporta come un <em>Estimator </em>di Sklearn e supporta il metodo .fit() e .transform())</p><p>The Yeo-Johnson transformation is defined as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*fMg_o5IRVQvIIrDGZ74DZw.jpeg" /></figure><p>I won’t go into detail about the Yeo-Johnson transform — just know that it is based on the Box-Cox and allows negative values.</p><h4>Practical Application</h4><p>As mentioned, the Yeo-Johnson transform is based on the Box-Cox, but the value that lambda can take can change. This makes the transformations essentially different as they can yield different results.</p><p>In Python, just pass one of the two transformation methods as a string to the PowerTransformer object.</p><pre>import numpy as np<br>import matplotlib.pyplot as plt<br>from sklearn.preprocessing import PowerTransformer<br><br># Generate data with positive and negative values<br>np.random.seed(42)<br>data_positive = np.random.exponential(scale=2, size=1000)<br>data_negative = -np.random.exponential(scale=0.5, size=200)<br>data = np.concatenate([data_positive, data_negative])<br><br># Create two instances of PowerTransformer, one for Yeo-Johnson and one for Box-Cox in order to confront them <br>pt_yj = PowerTransformer(method=&#39;yeo-johnson&#39;, standardize=False)<br>pt_bc = PowerTransformer(method=&#39;box-cox&#39;, standardize=False)<br><br># Apply the transformations<br>data_yj = pt_yj.fit_transform(data.reshape(-1, 1))<br># Box-Cox requires positive data, so we add an offset to render hem all positive<br>data_offset = data - np.min(data) + 1e-6<br>data_bc = pt_bc.fit_transform(data_offset.reshape(-1, 1))<br><br># Visualize the results<br>fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(18, 5))<br><br>ax1.hist(data, bins=50, edgecolor=&#39;black&#39;)<br>ax1.set_title(&quot;Original data&quot;)<br>ax1.set_xlabel(&quot;Value&quot;)<br>ax1.set_ylabel(&quot;Frequency&quot;)<br><br>ax2.hist(data_yj, bins=50, edgecolor=&#39;black&#39;)<br>ax2.set_title(&quot;Trasformation Yeo-Johnson&quot;)<br>ax2.set_xlabel(&quot;Value&quot;)<br><br>ax3.hist(data_bc, bins=50, edgecolor=&#39;black&#39;)<br>ax3.set_title(&quot;Trasformation Box-Cox&quot;)<br>ax3.set_xlabel(&quot;Value&quot;)<br><br>plt.tight_layout()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JAbo5qXhLeB9M9Y1SWhkLw.png" /></figure><h3>QuantileTransformer</h3><p>A quantile transformation maps the distribution of a variable to another target distribution. Through the QuantileTransformer class in Sklearn, it is possible to convert a non-normal distribution into a desired distribution.</p><p>Let’s consider any distribution of events — each event in this distribution will have an associated probability of occurring. This behavior is defined by the cumulative distribution function (CDF), which varies for each distribution.</p><p>The quantile function is the inverse of the CDF: while a CDF is a function that returns the probability of a value being less than or equal to a given value, the PPF is the inverse of this function and r<strong>eturns the value that is less than or equal to a given probability.</strong></p><p>In the context of <strong>anomaly detection</strong>, QuantileTransformer can be used to transform the data to make it more visible. Transforming the data into a uniform distribution, the outliers will be mapped to the extremes of the distribution,<strong> making them more distinguishable from the inliers.</strong></p><p>The QuantileTransformer can force any arbitrary distribution into a Gaussian, provided there are sufficient training samples (thousands). Since it is a non-parametric method, it is <strong>more difficult to interpret compared to parametric ones </strong>(Box-Cox and Yeo-Johnson).</p><h4>Practical application</h4><p>Sklearn comes to our aid once again with the dedicated QuantileTransformer object with an important parameter: <strong>output_distribution</strong>, which can accept the values “uniform” or “normal”. These represent the distribution to which the data are mapped.</p><pre>import numpy as np<br>from sklearn.preprocessing import QuantileTransformer<br>import matplotlib.pyplot as plt<br><br># Create a dataset of examples with a distorted distribution<br>np.random.seed(0)<br>data = np.random.exponential(scale=2, size=(1000, 1))  # Exponential distribution<br><br># Initialize the QuantileTransformer<br>quantile_transformer = QuantileTransformer(n_quantiles=100, output_distribution=&#39;normal&#39;)<br><br># Apply the transformation<br>data_transformed = quantile_transformer.fit_transform(data)<br><br># Visualization of the original and transformed data<br>plt.figure(figsize=(12, 6))<br><br># Histogram of the original data<br>plt.subplot(1, 2, 1)<br>plt.hist(data, bins=50, color=&#39;blue&#39;, edgecolor=&#39;black&#39;)<br>plt.title(&quot;Original data (exponential)&quot;)<br><br># Histogram of the transformed data<br>plt.subplot(1, 2, 2)<br>plt.hist(data_transformed, bins=50, color=&#39;green&#39;, edgecolor=&#39;black&#39;)<br>plt.title(&quot;Transformed data (normal)&quot;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lv2KW6XaLZODj72sKeBZIA.png" /></figure><h3>Examples of transformations: from specific distributions to the normal</h3><p>Below is a graphical visualization that compares different non-normal distributions and their respective transformations, using some of the techniques we have explored.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*2zFj1gc9MgOJ0NedI6Ccng.png" /></figure><p>This image highlights the limitation of some transformations, which do not always succeed in their intent. For example, for the bimodal distribution, all attempts to transform it into a normal curve fail except for the quantile transformation.</p><h3>Principal Component Analysis</h3><p>PCA transforms a set of possibly correlated variables into a set of linearly uncorrelated variables called principal components. These principal components are ordered so that the first ones contain most of the variance present in the original variables.</p><p>I wrote a detailed article on what PCA is if you’re interested 👇</p><p><a href="https://medium.com/operations-research-bit/principal-component-analysis-with-python-a-deep-dive-0c5195bff087">Principal Component Analysis with Python (A Deep Dive)</a></p><p>In the context of feature engineering, PCA can be used to reduce the number of features while retaining most of the information. This potentially reduces the noise present in the data and irrelevant correlations that could confuse the model.</p><p>Being a dimensionality reduction technique, PCA should be used to compress the dataset rather than expand it. PCA can extract some latent features, which are hidden or underlying factors that influence your data.</p><p>For example, if you have a dataset of images, you can use PCA to find features that represent the shape, color, or texture of the objects in the images. These latent features can help you better understand your data and improve your machine learning models.</p><h4>Practical application</h4><p>An application of PCA as feature engineering occurs when working with images and therefore in computer vision.</p><p>Let’s take random faces from the <em>lfw_people</em> dataset in Sklearn. It is possible to use TruncatedSVD (a compression technique very similar to PCA) to extract fundamental features of the image, such as contours, shadows, and the position of the main objects, to provide the machine learning model with relevant information for modeling.</p><p>A typical application of this approach is to normalize images, making the system more robust to variations in lighting or other conditions — this makes the reduced faces a basis for eventual facial recognition.</p><p>Clearly, this approach is applicable to tabular data as well.</p><pre>import matplotlib<br>matplotlib.use(&#39;Agg&#39;) <br>import numpy as np<br>import matplotlib.pyplot as plt<br>from sklearn.datasets import fetch_lfw_people<br>from sklearn.decomposition import TruncatedSVD<br>import os<br><br># Create output directory if it doesn&#39;t exist<br>output_dir = &#39;output_plots&#39;<br>os.makedirs(output_dir, exist_ok=True)<br>print(f&quot;Saving plots to: {output_dir}/&quot;)<br><br># Load the dataset - Labeled Faces in the Wild<br>lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)<br>X = lfw_people.data<br>n_samples, h, w = lfw_people.images.shape<br>print(f&quot;✓ Loaded {n_samples} real face images&quot;)<br>print(f&quot;✓ Dataset shape: {X.shape}&quot;)<br>print(f&quot;✓ Image dimensions: {h}x{w} pixels&quot;)<br><br># Number of components to use for the reduction<br>n_components = 50<br><br>svd = TruncatedSVD(n_components=n_components, random_state=42)<br>X_transformed = svd.fit_transform(X)<br>print(&quot;✓ SVD complete&quot;)<br><br># Function to reconstruct an image<br>def reconstruct_image(svd, X_transformed):<br>    return np.dot(X_transformed, svd.components_)<br><br>X_reconstructed = reconstruct_image(svd, X_transformed)<br><br># Function to visualize the original faces and reduced<br>def plot_transposed_original_and_reduced(X_original, X_reduced, n_faces=5):<br>    fig, axes = plt.subplots(2, n_faces, figsize=(3*n_faces, 6))<br>    random_indices = np.random.choice(X_original.shape[0], n_faces, replace=False)<br>    <br>    for i, idx in enumerate(random_indices):<br>        # Original face<br>        axes[0, i].imshow(X_original[idx].reshape(h, w), cmap=&#39;gray&#39;)<br>        axes[0, i].set_title(f&#39;Original {i+1}\nFace&#39;)<br>        axes[0, i].axis(&#39;off&#39;)<br>        <br>        # Reduced face<br>        axes[1, i].imshow(X_reduced[idx].reshape(h, w), cmap=&#39;gray&#39;)<br>        axes[1, i].set_title(f&#39;Reduced\n({n_components} comp.)&#39;)<br>        axes[1, i].axis(&#39;off&#39;)<br>    <br>    plt.tight_layout()<br>    plot1_path = os.path.join(output_dir, &#39;original_vs_reduced_faces.png&#39;)<br>    plt.savefig(plot1_path, dpi=150, bbox_inches=&#39;tight&#39;)<br>    plt.close()<br>    print(f&quot;✓ Saved: {plot1_path}&quot;)<br><br>plot_transposed_original_and_reduced(X, X_reconstructed)<br><br># Calculate and print mean reconstruction error<br>mse = np.mean((X - X_reconstructed) ** 2)<br>print(f&quot;\nMean Reconstruction Error (MSE): {mse:.4f}&quot;)<br><br># Print the cumulative variance<br>explained_variance_ratio = svd.explained_variance_ratio_<br>cumulative_variance_ratio = np.cumsum(explained_variance_ratio)<br>print(f&quot;Cumulative variance with {n_components} components: {cumulative_variance_ratio[-1]:.4f}&quot;)<br><br># Visualize the primary components<br>n_components_show = min(10, n_components)<br>components = svd.components_[:n_components_show].reshape((n_components_show, h, w))<br>fig, axes = plt.subplots(2, 5, figsize=(15, 6))<br>for i, ax in enumerate(axes.flat):<br>    if i &lt; n_components_show:<br>        ax.imshow(components[i], cmap=&#39;gray&#39;)<br>        ax.set_title(f&#39;Component {i+1}&#39;)<br>    ax.axis(&#39;off&#39;)<br>plt.tight_layout()<br>plot2_path = os.path.join(output_dir, &#39;principal_components.png&#39;)<br>plt.savefig(plot2_path, dpi=150, bbox_inches=&#39;tight&#39;)<br>plt.close()<br>print(f&quot;✓ Saved: {plot2_path}&quot;)<br><br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_lkOj9esJRIfNvYznvHwHA.png" /></figure><h3>Conclusion</h3><p>This article has explored various advanced feature engineering techniques for numerical data, highlighting their crucial role in improving the performance of machine learning models. The methodologies discussed include:</p><ol><li>Normalizzazione</li><li>Feature polinomiali</li><li>FunctionTransformer</li><li>KBinsDiscretizer</li><li>Trasformazione logaritmica</li><li>PowerTransformer (Box-Cox e Yeo-Johnson)</li><li>QuantileTransformer</li><li>PCA</li></ol><p>Each technique offers specific advantages and is suited to particular types of data and problems. The choice of the most appropriate transformation depends on the nature of the data, the problem to be addressed, and the selected model. There is no universal solution, making experimentation and validation essential.</p><h3>References</h3><ul><li><strong>“Feature Engineering for Machine Learning”</strong> by Alice Zheng &amp; Amanda Casari (O’Reilly, 2018)</li><li><strong>“Feature Engineering and Selection: A Practical Approach for Predictive Models” </strong>by Max Kuhn &amp; Kjell Johnson<strong> (2019)</strong></li><li><strong>“Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow”</strong> by Aurélien Géron (O’Reilly, 3rd edition)</li><li><a href="https://scikit-learn.org/stable/modules/preprocessing.html">https://scikit-learn.org/stable/modules/preprocessing.html</a></li><li><strong>“A Survey on Feature Engineering” (2020)</strong></li></ul><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e172f90aeb0" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/feature-engineering-techniques-for-numerical-variables-in-python-3e172f90aeb0">Feature engineering techniques for numerical variables in Python</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Autoencoders with TensorFlow and Keras]]></title>
            <link>https://pub.aimind.so/introduction-to-autoencoders-with-tensorflow-and-keras-d750c851054e?source=rss-fe44f5fcf1d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/d750c851054e</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[sparse-autoencoder]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[autoencoder]]></category>
            <dc:creator><![CDATA[Francesco  Franco]]></dc:creator>
            <pubDate>Wed, 28 Jan 2026 12:23:10 GMT</pubDate>
            <atom:updated>2026-02-02T16:48:16.017Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*atg_oHllZgAaKjpo-XRmuw.png" /></figure><p>In this article, we will discuss autoencoders—specific neural network architectures that learn to reconstruct input data through compression.</p><p>They find many applications in specific contexts, such as anomaly detection, clustering, and feature reconstruction.</p><p>In summary, by reading this article, you will learn:</p><ul><li>what an autoencoder is and why it is useful</li><li>the architectural structures that constitute the autoencode</li><li>how to implement an autoencoder in Python and train a binary classifier</li></ul><h3>What is an autoencoder?</h3><p>An <em>autoencoder</em> is a specific neural network architecture designed to learn a compressed representation of the input data, called an encoding.</p><p>The main objective is to reconstruct the original input from this compressed representation. To imagine it simply, let’s think of a camera: An autoencoder is like taking a photo where it tries to capture only the essential details of an image (compression) and then faithfully reconstructs it. During this process, the autoencoder “learns” what the fundamental characteristics of the data are.</p><p>When an autoencoder works, it transforms the input data into a more compact representation called the latent space. The latent space is a collection of latent variables, which are variables that we cannot directly observe but that represent significant aspects of the original data. For example, if the input were an image of a face, the latent variables could include information such as the face’s orientation, lighting, or the presence of glasses.</p><h3>Why are autoencoders useful?</h3><p>Autoencoders are extremely versatile tools in the field of artificial intelligence and are applied in various areas, such as</p><ul><li><strong>Data compression</strong>: They reduce the amount of information needed to represent the data, which is useful for storing images or videos in less space.</li><li><strong>Denoising:</strong> They remove noise from images, improving their quality. For example, they can clean up a photo taken in low light.</li><li><strong>Anomaly Detection</strong>: They can identify anomalies, such as fraudulent transactions in a financial system.</li><li><strong>Facial recognition</strong>: They extract essential features from faces, simplifying image comparison.</li></ul><p>These are just a few of the most important applications, and once understood, autoencoders can be used in a variety of situations depending on the context.</p><h3>Anatomy of an Autoencoder</h3><p>An autoencoder always consists of two main parts: <strong>an encoder</strong> that transforms input data into a compact representation and <strong>a decoder</strong> that uses this representation to reconstruct the original input. However, not all encoder-decoder models are autoencoders.</p><h4>What is an encoder-decoder model?</h4><p>An encoder-decoder model is a flexible architecture used in many deep learning applications. In this type of model, the encoder extracts the essential features from the input, while the decoder generates an output based on these features. Unlike autoencoders, the output is not necessarily a copy of the input.</p><p>For example, in machine translation (sequence-to-sequence models, seq2seq), the encoder transforms a sentence in one language (e.g., Italian) into a latent representation, which the decoder uses to generate the translation in another language (e.g., English).</p><p>In image segmentation models like U-Net, the encoder extracts image features (e.g., edges or textures), while the decoder uses them to construct segmentation masks that identify specific objects in the image. In these cases, encoder-decoder models are trained in a supervised manner, using labeled data. In the case of image segmentation, the model learns by comparing its predictions (generated masks) with the masks provided by experts.</p><h4>Autoencoders: a special case</h4><p>Autoencoders, on the other hand, are a special case of encoder-decoder models and are used to reconstruct the original input. <strong>Their peculiarity is that they operate in unsupervised mode</strong>: They do not require labeled data for training.</p><p>The goal of an autoencoder is to discover the hidden features within the data. However, what makes an autoencoder unique is its form of learning, which is often referred to as self-supervised. Although it doesn’t require external labels, it has a clear objective against which to measure the goodness of its prediction: the original input. This allows the autoencoder to evaluate how well it is reconstructing the data, which typically doesn’t happen in an unsupervised scenario.</p><h3>How do autoencoders work?</h3><p>Autoencoders are neural networks designed to compress and then reconstruct input data. To do this, they pass the input through a structure called a <strong>bottleneck</strong>, which forces the model to extract only the essential information. This process allows autoencoders to identify hidden patterns in data and compress large amounts of information into compact representations.</p><h4>Basic structure of an autoencoder</h4><p>All autoencoders follow a fundamental structure consisting of three main parts:</p><ul><li><strong>Encoder</strong>: It is responsible for data compression. As input data passes through the encoder layers, its dimensionality is progressively reduced. For example, if you have an image with millions of pixels, the encoder reduces this image to a much smaller set of numbers that represent only the most relevant information. This process is similar to compressing a ZIP file: you remove the unnecessary to keep only the essence of the information.</li><li><strong>Bottleneck</strong>: This is the most compact part of the architecture. The generated code represents a reduced and abstract version of the original input. It’s like summarizing a 1000-page book in a paragraph: The code must contain enough detail to allow the decoder to reconstruct the entire story.</li><li><strong>Decoder</strong>: The decoder does the opposite of the encoder: It takes the compressed code and “decompresses” it to reconstruct the original data. For example, if the original input was an image, the decoder tries to recreate the same image, pixel by pixel, from the code.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*l3LWyv0H5XIAT_rr7EhxwQ.png" /><figcaption><a href="https://contenthub-static.grammarly.com/blog/wp-content/uploads/2024/10/6303_blog-visuals-auto-encoders_1500X800.png?ref=diariodiunanalista.it">https://contenthub-static.grammarly.com/blog/wp-content/uploads/2024/10/6303_blog-visuals-auto-encoders_1500X800.png</a></figcaption></figure><h4>Unique Features of Autoencoders</h4><p>One of the main strengths of autoencoders compared to other dimensionality reduction techniques, such as<a href="https://medium.com/operations-research-bit/principal-component-analysis-with-python-a-deep-dive-0c5195bff087"> PCA (Principal Component Analysis)</a>, is their ability to capture non-linear relationships in the data. This is possible thanks to the use of nonlinear activation functions, such as the<a href="https://medium.com/thedeephub/four-key-activation-functions-relu-sigmoid-tanh-and-softmax-6d2525eb55a4"> sigmoid function or ReLU.</a></p><p>Autoencoders can be customized for different data types and applications:</p><ul><li>Sequential data: via RNN or LSTM, suitable for text or time series.</li><li>Images: using CNN, optimal for photos and videos.</li><li>Complex data: with hybrid or transformer architectures.</li></ul><h4>Key factors in designing an autoencoder</h4><p>The size of the bottleneck (also called the code) determines how compressed the data representation is. A code that is too large could lead to an almost perfect reconstruction, but it risks losing the value of compression. A code that is too small, on the other hand, might not contain enough information for a good reconstruction.</p><p>Additionally, increasing the depth of the autoencoder allows the model to capture more complex patterns, but it could slow down the training process. Then, the encoder layers progressively reduce the number of nodes, reaching the minimum at the bottleneck. After that, the decoder layers increase the number of nodes to restore the data to its original form. In conclusion, the choice of loss function depends on the type of data and the application.</p><h3>Incomplete autoencoders</h3><p>Incomplete autoencoders are one of the simplest and most fundamental variants of autoencoders, <strong>primarily designed for dimensionality reduction.</strong> The distinguishing feature of this architecture is the presence of a fixed bottleneck, with hidden layers containing fewer nodes than the input and output layers.</p><h4>How do incomplete autoencoders work?</h4><p>Bottleneck compression acts as a constraint on the network’s capacity, forcing the model to compress the input into a low-dimensional representation.</p><p>This compression<strong> forces the encoder to retain only the essential characteristics of the input,</strong> those necessary for accurate reconstruction. Without limiting the bottleneck capacity, the network could easily overfit the training data, learning the simple identity function: output = input. This happens because the model, without constraints, can copy the input data directly into the output, without ever capturing significant patterns or useful structures.</p><p>This type of autoencoder is particularly effective at compressing high-dimensional data into a more compact representation, which is useful for applications like feature extraction or visualization in reduced spaces. Despite their advantages, incomplete autoencoders have several limitations that reduce their applicability in complex scenarios. Even with a bottleneck, if the model has an encoder and decoder composed of multiple layers and nodes, it can still learn the identity function, rendering the model useless for practical applications. The fixed capacity of the bottleneck makes this architecture inflexible: It may not be well-suited for complex or diverse data, limiting its use in advanced applications such as data generation or image segmentation.</p><h4>When should you use incomplete autoencoders?</h4><p>Incomplete autoencoders are particularly useful in scenarios where</p><ul><li>The main objective is dimensionality reduction.</li><li>The data is relatively simple and doesn’t require modeling very complex nonlinear correlations.</li><li>You want to explore or visualize data in a compressed latent space.</li></ul><h3>Regularized Autoencoders</h3><p>Regularized autoencoders represent an evolution from incomplete autoencoders, designed to overcome the limitations in flexibility and generalization ability of the latter. The key to their functioning is the introduction of<a href="https://medium.com/gopenai/using-l1-l2-and-elasticnet-regularization-with-pytorch-633e79b863e0"> regularization techniques:</a> <strong>adjustments that modify or limit how the reconstruction error is calculated during training</strong>. The goal is not only to reduce the risk of overfitting but also to ensure that the model learns useful and well-structured features from the dataset.</p><h4>How do regularized autoencoders work?</h4><p>Regularization adds a term or constraint to the autoencoder’s optimization objective (often expressed through the loss function). This additional term guides the model in learning meaningful patterns, discouraging trivial solutions like simply copying the input data. Some of the most common regularization techniques are:</p><ul><li><strong>Sparse Autoencoders</strong>: which use a regularization term that forces only some neurons to be active simultaneously in the latent space, making the latter more interpretable, thus favoring a compact and essential representation of the data.</li><li><strong>Denoising Autoencoder:</strong> They introduce intentional noise to the input data and train the model to reconstruct the original noise-free input. Thus improving robustness to noisy data and increasing generalization ability.</li><li><strong>Variational Autoencoders</strong>: They impose a probabilistic distribution (usually Gaussian) on the latent space, forcing the learned representations to follow a statistical structure, thus fueling the possibility of generating new data similar to the original input.</li></ul><h3>Sparse Autoencoders</h3><p>They are a variant of autoencoders that introduce <strong>a sparsity constraint on the nodes of the neural network</strong>. This approach allows for learning meaningful representations even with networks that have a higher capacity (i.e., a large number of nodes and layers), without risking overfitting.</p><p>In a standard incomplete autoencoder, the information bottleneck is created by reducing the number of nodes in the hidden layers. Conversely, in sparse autoencoders, the number of neurons activated simultaneously for each observation is limited.</p><p>Each neuron that exceeds a specific activation threshold (e.g., a desired average activation level) is penalized through a regularization term added to the loss function. This scarcity constraint forces the network to represent data efficiently and activate specific neurons only when the features represented by those nodes are present in the input data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*JGNSG3WCa4r92iYB3si9lA.png" /><figcaption><a href="https://production-media.paperswithcode.com/methods/Screen_Shot_2020-06-28_at_3.36.11_PM_wfLA8dB.png?ref=diariodiunanalista.it">https://production-media.paperswithcode.com/methods/Screen_Shot_2020-06-28_at_3.36.11_PM_wfLA8dB.png</a></figcaption></figure><h3>Kullback-Leibler divergence</h3><p>The<a href="https://medium.com/h7w/kullback-leibler-divergence-with-keras-227ef84f2a1b"> Kullback-Leibler divergence</a> is a measure of dissimilarity between two probability distributions. When applied to sparse autoencoders, KL-divergence allows for the imposition of a sparsity constraint on neurons, penalizing activations that deviate from a desired target distribution.</p><p>Given a target distribution<em> p</em> (the desired sparsity) and an actual distribution<em> q</em> (the distribution of activation values), the KL divergence is calculated as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*RDLcZ8G0-9R2mUobCmToPg.png" /></figure><p>where:</p><ul><li>p(i) is the desired activation probability for node i.</li><li>q(i) is the actual activation probability for node i.</li><li>The logarithmic function penalizes the differences between p(i) and q(i) proportionally.</li></ul><p>(For more on KL diveregence, please see<a href="https://medium.com/h7w/kullhttps://medium.com/h7w/kullback-leibler-divergence-with-keras-227ef84f2a1bback-leibler-divergence-with-keras-227ef84f2a1b"> here</a>.)</p><p>In sparse autoencoders (SAEs), KL divergence is used as a regularization term during training. The process is as follows:</p><ul><li>Target distribution: Each hidden neuron has a desired sparsity ρ, which represents the expected average activation value. For example, if ρ = 0.05, the neuron should be active in only 5% of the observations.</li><li>Actual distribution: During training, the average of the actual activations ρ̂ of the hidden neurons is calculated for a batch of data.</li><li>Scarcity penalties: A term is added to the loss function that penalizes the network for each neuron whose ρ̂ deviates from ρ. The scarcity penalty is proportional to the KL divergence between ρ and ρ̂:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*j9cMMn77fjVukB2RQE7dug.png" /></figure><p>Where <em>j</em> is the index of the hidden node.</p><h4>Benefits of KL divergence in SAE</h4><p>KL divergence allows for precise adjustment of the desired activation level in neurons, adapting it to the complexity of the data. By ensuring that only a few neurons are activated simultaneously, SAEs promote the discovery of useful and sparse latent features. <strong>By limiting the activation of neurons, the network is less likely to memorize the specific details of the training data, thus improving generalization.</strong></p><p>KL divergence is also widely used in variational autoencoders (VAEs), but for a different purpose. In fact, in VAEs, KL divergence measures the difference between the distribution learned from the latent space (e.g., a parameterized Gaussian distribution) and a target distribution (often a standard normal distribution). This constraint guides the VAE to generate a well-organized and interpretable latent space.</p><h3>Denoising Autoencoder</h3><p>Denoising Autoencoders (DAEs) are a variant of autoencoders designed to learn to<strong> restore corrupted data by removing noise added to the inputs</strong>. Their noise reduction capability makes them ideal for preprocessing tasks and for building robust latent representations.</p><h4>How DAEs Work</h4><p>During training, the input data is deliberately modified by adding noise (e.g., Gaussian noise, salt-and-pepper noise, or masking some parts). The goal isn’t to reconstruct the corrupted data, but to recover the original, noise-free version. The encoder reduces the corrupted data to a compressed and robust representation, the decoder reconstructs the original data by removing noise, and the reconstruction error is measured by comparing the output with the original, uncorrupted version of the data (the “ground truth”).</p><p>The l<a href="https://medium.com/thedeephub/understanding-loss-and-loss-functions-c1839f110840">oss function</a> evaluates how much the reconstructed output deviates from the original data. A common example of a loss function is the mean squared error (MSE) between the original and reconstructed data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/289/1*wMx1LIYlP6j-c69DQEy3xw.png" /></figure><h4>Advantages of Denoising Autoencoders</h4><p>DAEs learn representations that are independent of irrelevant variations in the data, making them useful for working with noisy or partially corrupted inputs.</p><p>Adding noise implicitly increases the variability of the training data, acting as a form of regularization that reduces the risk of memorizing the specific details of the data. DAEs are used to “clean up” noisy data, such as images and audio files, improving data quality for further analysis. The principles of denoising have been adopted in advanced generative architectures, such as Stable Diffusion, to generate high-quality images from noisy inputs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*e54qlvVfbO9CyH0rdQ0NSg.png" /><figcaption><a href="https://production-media.paperswithcode.com/methods/Denoising-Autoencoder_qm5AOQM.png?ref=diariodiunanalista.it">https://production-media.paperswithcode.com/methods/Denoising-Autoencoder_qm5AOQM.png</a></figcaption></figure><h3>Variational Autoencoders</h3><p><a href="https://medium.com/h7w/an-introduction-to-variational-autoencoders-vaes-305bacb75fbb">Variational autoencoders (VAEs)</a> represent a sophisticated evolution of autoencoders, designed not only to compress data but also to generate new samples based on the distributions learned from the training data. This makes them particularly powerful as probabilistic generative models. See the link above for a more detailed discussion.</p><p>Unlike standard autoencoders that encode the input into a single discrete vector, VAEs model the latent space as a continuous probabilistic distribution. The latent space is represented by two vectors:</p><ul><li>Mean (μ): represents the center of the latent distribution.</li><li>Standard Deviation (σ): represents the variability around the mean.</li></ul><p>VAEs don’t map an input to a single point in latent space, but to a distribution, allowing for more diverse data generation. Once trained, a VAE can sample new points from the latent space using a standard normal distribution. Thanks to the learned probabilistic distribution, the newly generated samples are consistent with the original distribution of the training data.</p><h4>Loss function in VAEs</h4><p>The loss function in VAEs combines two components:</p><p>Reconstruction error (L_<em>rec)</em>:</p><ul><li>measures how well the decoder reconstructs the original input from the latent vectors</li><li>usually based on mean squared error (MSE) or cross-entropy loss.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*UCoExKObzcJsZojH1n5BNA.png" /></figure><p>Regularization via KL Divergence (L_<em>KL</em>):</p><ul><li>It penalizes the difference between the learned latent distribution (q(z|x)) and a predefined prior distribution (p(z)), typically a standard normal (N(0,I)).</li><li>It ensures that the samples generated from the latent space are consistent with the original data.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*1gflIwekzlAQmXCvYpmEtg.png" /></figure><p>The overall loss function is given by:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/215/1*XwuIzdAKPDpjYaYC6ZOaUw.png" /></figure><p>A key element of VAEs is the reparameterization trick, which allows for differentiable sampling from the latent space to enable training via backpropagation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/157/1*N654sbhEjZTrnFaPoTHgqw.png" /></figure><p>where</p><ul><li>ϵ∼N(0,1) is a random sample from the standard normal distribution.</li><li>μ and σ are respectively the mean and standard deviation learned by the encoder.</li></ul><p>This process separates the stochastic part (ϵ) from the learned part (μ and σ), ensuring that the gradient flow is calculable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*lISMO8gnmaIRPX8n_3N07g.png" /><figcaption><a href="https://miro.medium.com/v2/resize:fit:1400/1*kXiln_TbF15oVg7AjcUEkQ.png?ref=diariodiunanalista.it">https://miro.medium.com/v2/resize:fit:1400/1*kXiln_TbF15oVg7AjcUEkQ.png</a></figcaption></figure><h4>Advantages of VAEs</h4><p>VAEs can generate new data consistent with the original distribution, useful for applications such as image, signal, and text generation. Because the latent space is continuous, VAEs allow for interpolation between two points in the latent space, generating smooth transitions between data. Variants like Conditional VAEs allow you to control the characteristics of the generated samples by providing additional conditional inputs.</p><h3>Create an Autoencoder in Python</h3><p>Let’s write an autoencoder in Python with Keras. We will use the Credit Card Fraud dataset from Kaggle. Accessible and downloadable at the following link</p><p><a href="https://www.kaggle.com/datasets/mlg-ulb/creditcardfraud?ref=diariodiunanalista.it">Credit Card Fraud Detection</a></p><p>This dataset contains only numerical input variables that are the result of a PCA transformation. The features V1, V2, … V28 are the principal components obtained with PCA; the only features that were not transformed with PCA are ‘Time’ and ‘Amount’. The ‘Time’ feature contains the seconds elapsed between each transaction and the first transaction in the dataset. The ‘Amount’ feature is the transaction amount; this feature can be used for example-dependent cost-sensitive learning. The ‘Class’ feature is the response variable and takes the value 1 in case of fraud and 0 otherwise.</p><p>We will use Keras and TensorFlow to implement and train the autoencoder.</p><pre>from keras.layers import Input, Dense<br>from keras.models import Model, Sequential<br>from keras import regularizers<br>from sklearn.model_selection import train_test_split<br>from sklearn.linear_model import LogisticRegression<br>from sklearn.metrics import classification_report, accuracy_score<br>from sklearn.manifold import TSNE<br>from sklearn import preprocessing<br>import matplotlib.pyplot as plt<br>import pandas as pd<br>import numpy as np<br>import seaborn as sns<br>sns.set(style=&quot;whitegrid&quot;)<br>np.random.seed(203)<br><br>data = pd.read_csv(&quot;/content/drive/MyDrive/creditcard.csv&quot;)<br>data[&quot;Time&quot;] = data[&quot;Time&quot;].apply(lambda x : x / 3600 % 24)<br>data.head()</pre><pre>vc = data[&#39;Class&#39;].value_counts().to_frame().reset_index()<br>vc[&#39;percent&#39;] = vc[&quot;Class&quot;].apply(lambda x : round(100*float(x) / len(data), 2))<br>vc = vc.rename(columns = {&quot;index&quot; : &quot;Target&quot;, &quot;Class&quot; : &quot;Count&quot;})<br>vc<br><br>&gt;&gt;&gt;<br>Target  Count   percent<br>0       284315  99.83<br>1       492     0.17</pre><p>As we can see, the dataset is heavily imbalanced, but the approach we will use will be able to handle this type of problem. In our example, we will only use 1,000 of the non-fraudulent transactions.</p><pre>non_fraud = data[data[&#39;Class&#39;] == 0].sample(1000)<br>fraud = data[data[&#39;Class&#39;] == 1]<br><br>df = pd.concat([non_fraud, fraud]).sample(frac=1).reset_index(drop=True)<br>X = df.drop([&#39;Class&#39;], axis=1).values<br>Y = df[&#39;Class&#39;].values</pre><p>Now we visualize the transactions thru a particular type of distribution called T-SNE (t-Distributed Stochastic Neighbor Embedding). This is a dataset decomposition technique that allows you to reduce the dimensionality of the data and produce a specific number of components with the maximum information.</p><p>Each dot represents a transaction; non-fraudulent ones are shown as green dots, while fraudulent ones are shown as red dots. The two axes are two components extracted from the t-SNE.</p><pre>import matplotlib.pyplot as plt<br>from sklearn.manifold import TSNE<br>import numpy as np<br><br>def tsne_plot(x1, y1, name=&quot;graph.png&quot;):<br>    tsne = TSNE(n_components=2, random_state=0)<br>    X_t = tsne.fit_transform(x1)<br><br>    plt.figure(figsize=(12, 8))<br>    plt.scatter(X_t[np.where(y1 == 0), 0], X_t[np.where(y1 == 0), 1], marker=&#39;o&#39;, color=&#39;g&#39;, linewidth=1, alpha=0.8, label=&#39;Non Fraud&#39;)<br>    plt.scatter(X_t[np.where(y1 == 1), 0], X_t[np.where(y1 == 1), 1], marker=&#39;o&#39;, color=&#39;r&#39;, linewidth=1, alpha=0.8, label=&#39;Fraud&#39;)<br><br>    plt.legend(loc=&#39;best&#39;)<br>    plt.savefig(name)<br>    plt.show()<br><br># Assuming X and Y are your data and labels<br>tsne_plot(X, Y, &quot;original.png&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*3BjJK7PD6FV5SdHBWXDTyQ.png" /></figure><p>From the graph, it can be seen that numerous non-fraudulent transactions are incredibly close to each other, which will make it difficult for the model to classify them correctly.</p><p>At this point, we’ll use an autoencoder model to which we will show only the non-fraudulent cases. The model will try to learn the best possible representation of non-fraudulent cases. The same model will be used to generate representations of fraudulent cases, and we expect these to be different from those of non-fraudulent cases.</p><p>We create a network with an input layer and an output layer of identical dimensions, meaning the same shape as the non-fraudulent cases.</p><pre>## input layer<br>input_layer = Input(shape=(X.shape[1],))<br><br>## encoding part<br>encoded = Dense(100, activation=&#39;tanh&#39;, activity_regularizer=regularizers.l1(10e-5))(input_layer)<br>encoded = Dense(50, activation=&#39;relu&#39;)(encoded)<br><br>## decoding part<br>decoded = Dense(50, activation=&#39;tanh&#39;)(encoded)<br>decoded = Dense(100, activation=&#39;tanh&#39;)(decoded)<br><br>## output layer<br>output_layer = Dense(X.shape[1], activation=&#39;relu&#39;)(decoded)<br><br>autoencoder = Model(input_layer, output_layer)<br>autoencoder.compile(optimizer=&quot;adadelta&quot;, loss=&quot;mse&quot;)<br><br>#min max scaling<br>x = data.drop([&quot;Class&quot;], axis=1)<br>y = data[&quot;Class&quot;].values<br><br>x_scale = preprocessing.MinMaxScaler().fit_transform(x.values)<br>x_norm, x_fraud = x_scale[y == 0], x_scale[y == 1]</pre><p><strong>The beauty of this approach lies in the fact that we don’t need a large number of samples to learn good representations.</strong> We will only use 2000 lines of non-fraudulent cases to train the autoencoder. Additionally, it’s not necessary to run the model for a large number of epochs.</p><blockquote>The selection of small samples from the original dataset is based on the intuition that the characteristics of one class (non-fraudulent) will differ from those of the other (fraudulent). To distinguish these characteristics, it is sufficient to show the autoencoders only one class of data. This is because the autoencoder will try to learn only one class and will automatically distinguish the other.</blockquote><pre>autoencoder.fit(x_norm[0:2000], x_norm[0:2000],<br>                batch_size = 256, epochs = 10,<br>                shuffle = True, validation_split = 0.20);<br>Epoch 1/10<br>7/7 ━━━━━━━━━━━━━━━━━━━━ 3s 33ms/step - loss: 0.9299 - val_loss: 0.8239<br>Epoch 2/10<br>7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - loss: 0.9292 - val_loss: 0.8233<br>Epoch 3/10<br>7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - loss: 0.9287 - val_loss: 0.8228<br>Epoch 4/10<br>7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - loss: 0.9285 - val_loss: 0.8222<br>Epoch 5/10<br>7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 10ms/step - loss: 0.9277 - val_loss: 0.8216<br>Epoch 6/10<br>7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - loss: 0.9269 - val_loss: 0.8210<br>Epoch 7/10<br>7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - loss: 0.9264 - val_loss: 0.8204<br>Epoch 8/10<br>7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 11ms/step - loss: 0.9253 - val_loss: 0.8198<br>Epoch 9/10<br>7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - loss: 0.9246 - val_loss: 0.8192<br>Epoch 10/10<br>7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - loss: 0.9243 - val_loss: 0.8186<br><br></pre><p>Now the model has been trained, and we are interested in obtaining the latent representation of the input learned by the model. This representation can be obtained through the weights of the trained model.</p><pre>hidden_representation = Sequential()<br>hidden_representation.add(autoencoder.layers[0])<br>hidden_representation.add(autoencoder.layers[1])<br>hidden_representation.add(autoencoder.layers[2])<br><br>norm_hid_rep = hidden_representation.predict(x_norm[:3000])<br>fraud_hid_rep = hidden_representation.predict(x_fraud)<br><br>94/94 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step<br>16/16 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step</pre><p>Now let’s create a training dataset using the obtained latent representations and visualize the fraudulent cases compared to the non-fraudulent ones.</p><pre>rep_x = np.append(norm_hid_rep, fraud_hid_rep, axis = 0)<br>y_n = np.zeros(norm_hid_rep.shape[0])<br>y_f = np.ones(fraud_hid_rep.shape[0])<br>rep_y = np.append(y_n, y_f)<br>tsne_plot(rep_x, rep_y, &quot;latent_representation.png&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*BIiw7rYYBuqUD71drgDH4A.png" /></figure><p>From this graph, we can observe how fraudulent and non-fraudulent transactions are clearly visible and linearly separable. A complex model is no longer needed to classify these transactions. Even the simplest models can be used to make predictions.</p><pre>train_x, val_x, train_y, val_y = train_test_split(rep_x, rep_y, test_size=0.25)<br>clf = LogisticRegression(solver=&quot;lbfgs&quot;).fit(train_x, train_y)<br>pred_y = clf.predict(val_x)<br><br>print (&quot;&quot;)<br>print (&quot;Classification Report: &quot;)<br>print (classification_report(val_y, pred_y))<br><br>print (&quot;&quot;)<br>print (&quot;Accuracy Score: &quot;, accuracy_score(val_y, pred_y))<br><br>Classification Report: <br>              precision    recall  f1-score   support<br><br>         0.0       0.98      1.00      0.99       744<br>         1.0       1.00      0.86      0.93       129<br><br>    accuracy                           0.98       873<br>   macro avg       0.99      0.93      0.96       873<br>weighted avg       0.98      0.98      0.98       873<br><br><br>Accuracy Score:  0.97</pre><h3>Summary</h3><p>Autoencoders are a fascinating and versatile tool. From simple latent representations to sophisticated applications like noise reduction and data generation, these neural networks offer elegant solutions to complex problems. The variety of approaches, from incomplete autoencoders to variational ones, shows how to adapt these architectures to the specific needs of a project.</p><p>Understanding the differences between autoencoders and encoder-decoders, as well as the distinguishing features of each type, is crucial to fully utilize their potential. With their increasing use in areas like anomaly detection, data compression, and image generation, autoencoders continue to revolutionize the way we interpret and utilize data.</p><p>Exploring this technology not only enriches our technical knowledge but also opens doors to innovative solutions and a deeper understanding of the data we analyze.</p><h3>References</h3><ul><li><strong>Goodfellow, I., Bengio, Y., &amp; Courville, A. (2016). <em>Deep Learning</em>.</strong> MIT Press. (Chapter 14 covers autoencoders comprehensively)</li><li><strong>Bishop, C. M. (2006). <em>Pattern Recognition and Machine Learning</em>.</strong> Springer. (Chapter 12 discusses related probabilistic models)</li><li>Shafkat, I. (2018, April 5). Intuitively Understanding Variational Autoencoders. Retrieved from <a href="https://towardsdatascience.com/intuitively-understanding-variational-autoencoders-1bfe67eb5daf">https://towardsdatascience.com/intuitively-understanding-variational-autoencoders-1bfe67eb5daf</a></li><li>Rocca, J. (2019, December 8). Understanding Variational Autoencoders (VAEs). Retrieved from <a href="https://towardsdatascience.com/understanding-variational-autoencoders-vaes-f70510919f73">https://towardsdatascience.com/understanding-variational-autoencoders-vaes-f70510919f73</a></li><li>Jordan, J. (2018A, July 16). Variational autoencoders. Retrieved from <a href="https://www.jeremyjordan.me/variational-autoencoders/">https://www.jeremyjordan.me/variational-autoencoders/</a></li><li>Jordan, J. (2018B, March 19). Introduction to autoencoders. Retrieved from <a href="https://www.jeremyjordan.me/autoencoders/">https://www.jeremyjordan.me/autoencoders/</a></li><li>Keras Blog. (n.d.). Building Autoencoders in Keras. Retrieved from <a href="https://blog.keras.io/building-autoencoders-in-keras.html">https://blog.keras.io/building-autoencoders-in-keras.html</a></li><li>Kingma, D. P., &amp; Welling, M. (2013). <a href="https://arxiv.org/abs/1312.6114">Auto-encoding variational bayes</a>. <em>arXiv preprint arXiv:1312.6114</em>.</li><li>Theis, L., van den Oord, A., &amp; Bethge, M. (2016). <em>A note on the evaluation of generative models</em>. ICLR.<br><a href="https://arxiv.org/abs/1511.01844">arXiv:1511.01844</a></li><li>Rosca, M., et al. (2017). <em>Variational Approaches for Auto-Encoding Generative Adversarial Networks</em>.<br><a href="https://arxiv.org/abs/1706.04987">arXiv:1706.04987</a></li><li>Larsen, A. B. L., et al. (2016). <em>Autoencoding beyond pixels using a learned similarity metric</em>. ICML.<br><a href="https://arxiv.org/abs/1512.09300">arXiv:1512.09300</a></li><li>Karras, T., et al. (2019). <em>A Style-Based Generator Architecture for Generative Adversarial Networks</em>. CVPR.<br><a href="https://arxiv.org/abs/1812.04948">arXiv:1812.04948</a></li><li>Ng, A. (2011). “Sparse autoencoder.” <em>CS294A Lecture notes</em>, Stanford University. (Classic tutorial notes)</li><li>Makhzani, A., &amp; Frey, B. (2013). “k-Sparse autoencoders.” <em>arXiv preprint arXiv:1312.5663</em>.</li><li>Vincent, P., et al. (2008). “Extracting and composing robust features with denoising autoencoders.” <em>ICML</em>.</li></ul><h4>A Message from AI Mind</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5Wm7sOfTpe5DEbhg.gif" /></figure><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://pub.aimind.so/">AI Mind Publication</a></li><li>🧠 Improve your <a href="https://www.aimind.so/prompt-generator?utm_source=pub&amp;utm_medium=message">AI prompts effortlessly and FREE</a></li><li><strong>🧰 Discover </strong><a href="https://www.aimind.so/?utm_source=pub&amp;utm_medium=message"><strong>Intuitive AI Tools</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d750c851054e" width="1" height="1" alt=""><hr><p><a href="https://pub.aimind.so/introduction-to-autoencoders-with-tensorflow-and-keras-d750c851054e">Introduction to Autoencoders with TensorFlow and Keras</a> was originally published in <a href="https://pub.aimind.so/">AI Mind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>