<?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 Uttam Singh on Medium]]></title>
        <description><![CDATA[Stories by Uttam Singh on Medium]]></description>
        <link>https://medium.com/@study.uttam?source=rss-1c74b53da0a9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lUM0tzPeYgQBpaRPET8b-g.png</url>
            <title>Stories by Uttam Singh on Medium</title>
            <link>https://medium.com/@study.uttam?source=rss-1c74b53da0a9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 06:49:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@study.uttam/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[Unleashing the Power of Python Generators in Data Science: Efficient Data Handling and Beyond]]></title>
            <link>https://medium.com/@study.uttam/unleashing-the-power-of-python-generators-in-data-science-efficient-data-handling-and-beyond-13a126ba536?source=rss-1c74b53da0a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/13a126ba536</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data]]></category>
            <dc:creator><![CDATA[Uttam Singh]]></dc:creator>
            <pubDate>Thu, 27 Jul 2023 16:56:41 GMT</pubDate>
            <atom:updated>2023-07-27T16:56:41.964Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Introduction</strong></p><p>In the fast-paced world of data science and artificial intelligence, efficient handling of large datasets is a constant challenge. However, my recent encounter with Python generators while working with ImageDataGenerator from Keras for data augmentation has been nothing short of revelatory. I was pleasantly surprised by their remarkable efficiency in memory utilization and their ability to streamline data processing.</p><p>In this blog, I am excited to share my newfound insights into the power of Python generators and their immense benefits for handling extensive datasets. As we navigate the era of Large Language Models (LLMs) and Generative AI, the understanding of effective data management becomes a crucial asset that can set you apart in the competitive landscape of data science.</p><p>After reading this blog post, you will know :</p><ol><li>How to create Generators in Python.</li><li>How memory efficient Python Generators are.</li><li>Step by Step process of importing data using Generators.</li><li>Advantages of using generators in Data Science Applications.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5iFljARu_PYh2Svu" /><figcaption>Photo by <a href="https://unsplash.com/@theshubhamdhage?utm_source=medium&amp;utm_medium=referral">Shubham Dhage</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>What are Python Generators?</strong></p><p>Generators in Python are a special type of iterable, similar to lists or tuples, but with a crucial difference — they do not store all their values in memory at once. Instead, generators produce values on-the-fly as you iterate through them. This lazy evaluation makes generators an excellent choice for handling large datasets or infinite sequences, as they consume significantly less memory compared to conventional data structures.</p><p><strong>Creating Generators</strong></p><p>Generators can be created using generator functions or using generator expressions.</p><ol><li><strong>Generator Functions</strong>: A generator function is defined like any other Python function but with one key difference — it uses the yield keyword to produce values instead of return. When the function encounters yield, it temporarily suspends its execution and &quot;yields&quot; the value to the caller, maintaining its state to continue from where it left off.</li></ol><pre>def number_generator(n):<br>    for i in range(n):<br>        yield i<br><br># Using the generator function<br>gen = number_generator(5)<br>for num in gen:<br>    print(num)</pre><p>2. <strong>Generator Expressions</strong>: Generator expressions are a compact way of creating generators using a syntax similar to list comprehensions. They are enclosed within parentheses () instead of brackets [].</p><pre># Generator expression<br>gen = (x for x in range(5))<br>for num in gen:<br>    print(num)</pre><p>Now let’s learn about generators through some examples.</p><p>The following example shows a function that generates the squares of numbers up to a given &#39;n&#39; value. The function returns a list, containing the square of numbers, which is stored in memory. In contrast, the generator function yields one squared value at a time and can be iterated over using a for loop. If &#39;n&#39; is small, the difference in memory usage may not be significant. However, if &#39;n&#39; is large, the memory usage may vary greatly.</p><pre>#Normal Python function for generating square of numbers unpto n<br>def sq_num(n):<br><br>    result = []<br><br>    for num in range(1,n):<br>        result.append(num**2)<br>    <br>    return result<br><br>sqaure = sq_num(4)<br>print(sqaure)</pre><pre>#Python Generator for generating squares of numbers upto n<br>def square_gen(n):<br><br>    for num in range(1,n):<br>        yield num**2<br><br><br>for square in square_gen(4):<br>    print(square)<br></pre><p>To further illustrate the capabilities of generators, I would like to provide a popular example. The code below generates dictionaries for a specified number of individuals, determined by the ‘num_people’ variable. These dictionaries include attributes such as id, name, and major, with the later two being randomly selected from a list of names and majors. We have created two functions — a standard python function and a python generator — to accomplish this task. We will test the memory usage of both functions by setting a very high value for ‘num_people’, specifically 1000000.</p><pre>import memory_profiler as mem_profile<br>import random<br>import time<br><br>names = [&#39;John&#39;, &#39;Corey&#39;, &#39;Adam&#39;, &#39;Steve&#39;, &#39;Rick&#39;, &#39;Thomas&#39;]<br>majors = [&#39;Math&#39;, &#39;Engineering&#39;, &#39;CompSci&#39;, &#39;Arts&#39;, &#39;Business&#39;]<br><br># print(&#39;Memory (Before): {}Mb &#39;.format(mem_profile.memory_usage_psutil()))<br>print(&#39;Memory (Before): &#39; + str(mem_profile.memory_usage()) + &#39;MB&#39; )<br><br>def people_list(num_people):<br>    result = []<br>    for i in range(num_people):<br>        person = {<br>                    &#39;id&#39;: i,<br>                    &#39;name&#39;: random.choice(names),<br>                    &#39;major&#39;: random.choice(majors)<br>                }<br>        result.append(person)<br>    return result<br><br>def people_generator(num_people):<br>    for i in range(num_people):<br>        person = {<br>                    &#39;id&#39;: i,<br>                    &#39;name&#39;: random.choice(names),<br>                    &#39;major&#39;: random.choice(majors)<br>                }<br>        yield person<br><br>&quot;&quot;&quot;t1 = time.time()<br>people = people_list(1000000)<br>t2 = time.time()&quot;&quot;&quot;<br><br>t1 = time.time()<br>people = people_generator(1000000)<br>t2 = time.time()<br><br># print &#39;Memory (After) : {}Mb&#39;.format(mem_profile.memory_usage_psutil())<br>print(&#39;Memory (After) : &#39; + str(mem_profile.memory_usage()) + &#39;MB&#39;)<br><br># print &#39;Took {} Seconds&#39;.format(t2-t1)<br>print (&#39;Took &#39; + str(t2-t1) + &#39; Seconds&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*dIW_Sk_ice53xXv89W4OuQ.png" /><figcaption>Memory Usage using normal Python Function</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*1Rj--Dendu5TF_nAMUe7kA.png" /><figcaption>Memory usage using Python Generator</figcaption></figure><p>As shown above, using a normal function results is much higher memory usage compared to a generator which utilizes very little memory.</p><p>Now that you have witnessed the incredible memory efficiency of using generators, let’s explore a <strong><em>step-by-step guide on how to import data using generators.</em></strong></p><ol><li><strong>Define the Data Source</strong>: First, you need to define the data source, which could be a large file, a database, or a web API. For the sake of illustration, let’s assume we have a large CSV file containing rows of data.</li><li><strong>Create the Generator Function</strong>: Next, create a generator function that will read the data from the source and yield one piece of data at a time. The generator function typically involves opening the file, reading it line by line, processing the data as needed, and yielding the data one by one.</li></ol><pre>def data_generator(file_path):<br>    with open(file_path, &#39;r&#39;) as file:<br>        for line in file:<br>            # Process the data if required (e.g., split CSV values)<br>            data = line.strip().split(&#39;,&#39;)<br>            yield data</pre><p>3. <strong>Iterate through the Data</strong>: Now, you can use the generator function in a loop to iterate through the data. Each iteration will produce the next data point without loading the entire dataset into memory.</p><pre>data_file_path = &#39;path/to/your/large_data.csv&#39;<br>data_gen = data_generator(data_file_path)<br><br>for data_point in data_gen:<br>    # Process each data_point as needed<br>    print(data_point)</pre><p>By following these steps, you can efficiently import large datasets without worrying about memory constraints. Python generators offer an elegant solution for handling extensive data sources, such as large files or real-time data streams, while keeping your code concise and memory-efficient.</p><p>Now, let’s list out some of the key advantages of incorporating generators into your coding for any kind of Data Science application or even in general.</p><p><strong>Advantages of using generators</strong></p><ol><li><strong>Memory Efficiency</strong>: Generators consume minimal memory, enabling efficient processing of large datasets without risking memory overflows or slowdowns.</li><li><strong>Stream Processing</strong>: Generators facilitate stream processing, allowing data to be processed in real-time or on-the-fly, which is essential for scenarios like live data streaming or working with continuous data sources.</li><li><strong>Scalability</strong>: Generators empower developers to handle massive datasets and complex computations that might not be possible with traditional data structures.</li><li><strong>Faster Execution</strong>: By lazily generating data, generators can significantly speed up execution times, especially in cases where not all data needs to be computed upfront.</li></ol><p>As a data scientist, embracing Python generators offers a competitive edge, enabling you to push the boundaries of your data-driven projects. By optimizing memory utilization and execution times, generators allow you to focus more on the core of your research, analysis, and model development, rather than worrying about memory constraints.</p><p>In conclusion, the understanding of Python generators is a crucial skill for data scientists in today’s data-rich world. By efficiently handling large datasets and embracing the power of on-the-fly data processing, you empower yourself to explore new possibilities and accelerate your data science journey.</p><p>For more updates on topics such as Data Science and Machine Learning, <a href="https://medium.com/@study.uttam"><em>follow me</em></a>.</p><p><em>References :</em></p><ol><li>‘Tf.Keras.Preprocessing.Image.ImageDataGenerator | TensorFlow v2.13.0’. <em>TensorFlow</em>, <a href="https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator.">https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator.</a> Accessed 27 July 2023.</li><li><em>Generators — Python Wiki</em>. <a href="https://wiki.python.org/moin/Generators.">https://wiki.python.org/moin/Generators.</a> Accessed 27 July 2023.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13a126ba536" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Main Challenges of Machine Learning]]></title>
            <link>https://medium.com/@study.uttam/main-challenges-of-machine-learning-eb06dffac3da?source=rss-1c74b53da0a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb06dffac3da</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Uttam Singh]]></dc:creator>
            <pubDate>Mon, 10 Apr 2023 17:59:12 GMT</pubDate>
            <atom:updated>2023-04-10T18:03:03.221Z</atom:updated>
            <content:encoded><![CDATA[<p>In this era of Generative AI, it seems that the fundamentals of machine learning are often overlooked. Achieving the level of accuracy and precision seen in popular models such as GPT-4 requires a significant investment of time and effort, as well as overcoming numerous obstacles. That’s why I’m writing this blog post, to shed light on some of the challenges involved in creating machine learning models and to emphasize the tremendous amount of work required to build a flawless model.</p><p><strong>Challenge 1</strong>. <strong>Insufficient amount of Data</strong></p><p>For a toddler to learn about a cat or a dog, you just need to show them a cat or a dog once, and they can recognize them most of the time, even in different colors or breeds. However, for machine learning to learn about cats and dogs, it requires millions of images.</p><p>In a famous paper published in 2001, Microsoft researchers Michele Banko and Eric Brill showed that very different machine learning algorithms, including fairly simple ones, performed almost identically if they are supplied with enough amount of data. It should be noted that small and medium-sized datasets are still very common, and it is not always easy or cheap to get extra training data.</p><p><strong>Challenge 2. Non-representative Training Data</strong></p><p>Let’s take the example of building a model to predict animal classes. Imagine that your training data only contains images of cats and dogs, but your test data includes images of horses and rabbits. In this scenario, your training data is not representative of the real-world problem you are trying to solve. You might wonder why you would use a model that only classifies cats and dogs to classify horses and rabbits. It’s clear that the training data needs to be more diverse and representative of the real-world problem to achieve accurate results.</p><p>Let me provide a different example that may make more sense. Suppose you are trying to predict crude oil prices using data that only includes crude oil prices from the previous 20 years. Even if you use the best possible model, you may not be able to reduce your errors. The reason for this is that your training data is not representative of the complex web of factors that affect crude oil prices, such as the balance between supply and demand, geopolitical events, OPEC policies, economic conditions, natural disasters, and currency exchange rates. Therefore, to achieve accurate results, you need to incorporate a more diverse range of data that is representative of the real-world factors that impact crude oil prices.</p><p>Still, the best example of nonrepresentative data is the <a href="https://www.cambridge.org/core/journals/social-science-history/article/president-landon-and-the-1936-literary-digest-poll/E360C38884D77AA8D71555E7AB6B822C">Literary Digest Poll</a>.</p><p><strong>Challenge 3. Poor Quality Data</strong></p><p>If your training data is full of errors, outliers, and noise (e.g., due to poor-quality measurements), it will make it harder for the system to detect the underlying patterns, so your system is less likely to perform well.</p><p>It is often well worth the effort to spend time cleaning up your training data. The truth is, most data scientists spend almost 80 % of their time doing just that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IEXIz7LExiBzb--LxHlBmg.jpeg" /><figcaption>e.g. of a Outlier (Credit — <a href="https://unsplash.com/photos/ku_ttDpqIVc">https://unsplash.com/photos/ku_ttDpqIVc</a>)</figcaption></figure><p><strong>Challenge 4. Irrelevant features.</strong></p><p>As the saying goes — “Garbage in garbage out.” The performance of a machine learning model is highly dependent upon the relevance of the features present in the datasets. If your data contains too many irrelevant features, your model will also learn from those irrelevant features which ultimately impacts the model&#39;s performance.</p><p>A critical part of the success of a machine learning project is coming up with a good set of features to train on. This process, called <em>feature engineering</em>, involves the following steps:</p><ol><li>Feature Selection — Selecting the most relevant features to train on among all existing features.</li><li>Feature Extraction — (combining existing features to produce a more useful one e.g. dimensionality reduction algorithms like PCA, tSNE can help)</li><li>Creating New Features — by gathering new data.</li></ol><p><strong>Challenge 5. Overfitting the Training Data</strong></p><p>Say for example you joined your masters and you meet a person who is not social at all, and you make a presumption that everyone in masters is not social and doesn’t help each other. Here you are overgeneralizing your assumption after interacting with just one person. The person you met might be an outlier from the whole class.</p><p>Overgeneralizing is something that we humans do all too often, and unfortunately machines can fall into the same trap if we are not careful. In machine learning this is called <em>overfitting</em>: it means that the model performs well on the training data, but it does not generalize well.</p><p>Just think if our model learns from the below messy room that this is the best way to keep everything in a room.</p><figure><img alt="A photo depicting a messy room" src="https://cdn-images-1.medium.com/max/1024/1*_X-1b-YXKX0S45NMU2-PsA.jpeg" /></figure><p>Overfitting happens usually when there is lots of noise in the data or there is not enough data to learn from.</p><p><strong>Challenge 6. Underfitting the Training Data</strong></p><p>An example of underfitting in real life could be a student who is studying for an exam. If the student only reads the material once and does not practice any questions or review the material in depth, they may not perform well on the exam. This is because they have not learned the material well enough to apply it to different scenarios or questions.</p><p>In this case, the student is underfitting the material by not studying it thoroughly enough to achieve a good understanding of it. This is similar to underfitting in machine learning, where the model is not complex enough to accurately capture the relationships between the features and the target variable.</p><p>Just like the student needs to practice and review the material to perform well on the exam, a machine learning model needs to be trained with sufficient data and a suitable level of complexity to avoid underfitting and achieve good performance on the task at hand.</p><p>These are just a few of the challenges that a data scientist or machine learning engineer faces when building an effective machine learning model. There are ways to overcome these challenges, but for now, I want to focus on understanding the challenges themselves. In my next post, I will delve into ways to tackle these challenges.</p><p>For future updates please follow me.</p><p><em>References</em></p><ol><li><em>Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow 3e: Concepts, Tools, and Techniques to Build Intelligent Systems — by </em><a href="https://www.amazon.co.uk/s/ref=dp_byline_sr_book_1?ie=UTF8&amp;field-author=Aurelien+Geron&amp;text=Aurelien+Geron&amp;sort=relevancerank&amp;search-alias=books-uk">Aurelien Geron</a>.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb06dffac3da" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>