<?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 Ian Nyingi on Medium]]></title>
        <description><![CDATA[Stories by Ian Nyingi on Medium]]></description>
        <link>https://medium.com/@iannyingi04?source=rss-92c29ce0be4a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Sc6l_DK9Wk5NTX4jU3gN7Q.jpeg</url>
            <title>Stories by Ian Nyingi on Medium</title>
            <link>https://medium.com/@iannyingi04?source=rss-92c29ce0be4a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 14:18:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@iannyingi04/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[Preprocessing with Pipelines in Python]]></title>
            <link>https://medium.com/@iannyingi04/preprocessing-with-pipelines-in-python-d6364192310f?source=rss-92c29ce0be4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6364192310f</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[pipeline]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-preprocessing]]></category>
            <dc:creator><![CDATA[Ian Nyingi]]></dc:creator>
            <pubDate>Tue, 01 Nov 2022 13:54:19 GMT</pubDate>
            <atom:updated>2022-11-01T13:54:19.013Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*wlRtVqwLHYDW31ibDGnfuQ.jpeg" /></figure><h4>Introduction</h4><p>Pipelines are a simple way to keep your data preprocessing and modeling code organized. Specifically, a pipeline bundles preprocessing and modeling steps so you can use the whole bundle as if it were a single step.</p><h4>Benefits of using pipelines</h4><ol><li>Cleaner Code: Accounting for data at each step of preprocessing can get messy. With a pipeline, you won’t need to manually keep track of your training and validation data at each step.</li><li>Fewer Bugs: There are fewer opportunities to misapply a step or forget a preprocessing step.</li><li>Easier in Production: It can be surprisingly hard to transition a model from a prototype to something deployable at scale.</li></ol><p>Let’s look at a simple example. We’ll need to create a data frame as shown below.</p><pre>import pandas as pd<br>data = {&quot;Name&quot;: [&quot;Mike&quot;, &quot;Brian&quot;, &quot;Simon&quot;,&quot;Vallery&quot;,&quot;Ashley&quot;],<br>       &quot;Age&quot;:[22,19,21,None,22],<br>       &quot;Gender&quot;:[&quot;m&quot;,&quot;m&quot;,&quot;m&quot;,&quot;f&quot;,&quot;f&quot;],<br>       &quot;Job&quot;:[&quot;Mechanic&quot;,&quot;Teacher&quot;,&quot;Doctor&quot;, &quot;Teacher&quot;,&quot;Chef&quot;]}</pre><pre>df = pd.DataFrame(data)<br>df</pre><p>Now let’s preprocess the data manually without using a pipeline as shown in the code below.</p><pre>from sklearn.impute import SimpleImputer<br>from sklearn.preprocessing import OneHotEncoder</pre><pre>#Drop name features<br>df = df.drop([&quot;Name&quot;], axis =1 )</pre><pre># Impute Ages</pre><pre>imputer = SimpleImputer(strategy=&quot;mean&quot;)<br>df[&#39;Age&#39;] = imputer.fit_transform(df[[&quot;Age&quot;]])</pre><pre>#Numeric Gender<br>gender_dct = {&quot;m&quot;: 0,&quot;f&quot;: 1}<br>df[&#39;Gender&#39;] = [gender_dct[g] for g in df[&#39;Gender&#39;]]</pre><pre>#OneHotEncode Jobs<br>encoder = OneHotEncoder()<br>matrix = encoder.fit_transform(df[[&#39;Job&#39;]]).toarray()</pre><pre>column_names = [&quot;Mechanic&quot;,&quot;Teacher&quot;,&quot;Doctor&quot;, &quot;Teacher&quot;,&quot;Chef&quot;]</pre><pre>for i in range(len(matrix.T)):<br>    df[column_names[i]]=matrix.T[i]<br>    <br>df = df.drop([&#39;Job&#39;],axis = 1)<br>#we are dropping this column since it has already been encoded<br></pre><p>Now let’s say you want to do the same to other data frames but in a simpler way using pipelines. Below is an example of a new data frame.</p><pre>data2= {&quot;Name&quot;: [&quot;Maureen&quot;, &quot;Ian&quot;, &quot;Chinedu&quot;,&quot;Kamau&quot;,&quot;Laura&quot;],<br>       &quot;Age&quot;:[None,22,21,None,22],<br>       &quot;Gender&quot;:[&quot;f&quot;,&quot;m&quot;,&quot;m&quot;,&quot;m&quot;,&quot;f&quot;],<br>       &quot;Job&quot;:[&quot;Mechanic&quot;,&quot;Engineer&quot;,&quot;Doctor&quot;, &quot;Programmer&quot;,&quot;Nurse&quot;]}</pre><pre>df2 = pd.DataFrame(data2)<br>df2</pre><p>To preprocess the above data frame, we will have to repeat the same process as we did in the first process. This can sometimes be very tedious as one can get multiple datasets to work on. Hence, the need to create a pipeline that will make the preprocessing work easier and non-repetitive.</p><p>The code below shows how you can create a pipeline based on the steps that we implemented in our first data frame. This pipeline can be used on subsequent data frames.</p><pre>#Pipelines<br>from sklearn.base import BaseEstimator, TransformerMixin</pre><pre>class NameDropper(BaseEstimator, TransformerMixin):<br>    <br>    def fit(self, X, y=None):<br>        return self<br>    <br>    def transform(self,X):<br>        return X.drop([&#39;Name&#39;],axis =1 )<br>    <br>class AgeImputer(BaseEstimator, TransformerMixin):<br>    <br>    def fit(self, X, y=None):<br>        return self<br>    <br>    def transform(self,X):<br>        imputer = SimpleImputer(strategy=&quot;mean&quot;)<br>        X[&#39;Age&#39;] = imputer.fit_transform(X[[&#39;Age&#39;]])<br>        <br>        return X<br>    <br>    <br>class FeatureEncoder(BaseEstimator, TransformerMixin):<br>    <br>    def fit(self, X, y=None):<br>        return self<br>    <br>    def transform(self,X):<br>        gender_dct = {&quot;m&quot;: 0,&quot;f&quot;: 1}<br>        X[&#39;Gender&#39;] = [gender_dct[g] for g in X[&#39;Gender&#39;]]</pre><pre>encoder = OneHotEncoder()<br>        matrix = encoder.fit_transform(X[[&#39;Job&#39;]]).toarray()</pre><pre>column_names = [&quot;Mechanic&quot;,&quot;Teacher&quot;,&quot;Doctor&quot;, &quot;Teacher&quot;,&quot;Chef&quot;]</pre><pre>for i in range(len(matrix.T)):<br>            X[column_names[i]]=matrix.T[i]</pre><pre>return X.drop([&#39;Job&#39;],axis = 1)</pre><p>We can either do it manually as shown below.</p><pre>dropper = NameDropper()<br>imp = AgeImputer()<br>enc = FeatureEncoder()</pre><pre>#doing it mannually <br>enc.fit_transform(imp.fit_transform(dropper.fit_transform(df2)))<br></pre><p>Or we can opt to use a pipeline as shown below which becomes easier.</p><pre>#using pipelines</pre><pre>from sklearn.pipeline import Pipeline</pre><pre>pipe = Pipeline ([<br>    (&quot;dropper&quot;,NameDropper()),<br>    (&quot;imputer&quot;, AgeImputer()),<br>    (&quot;encoder&quot;, FeatureEncoder())<br>])</pre><pre>pipe.fit_transform(df2)</pre><h4>Conclusion</h4><p>Pipelines are valuable for cleaning up machine learning code and avoiding errors and are especially useful for workflows with sophisticated data preprocessing. Scikit-learn pipelines are a tool to simplify this process. They have several key benefits: <strong>They make your workflow much easier to read and understand</strong>. They enforce the implementation and order of steps in your project.</p><p>View the link to this notebook <a href="https://github.com/Nyingiian/Preprocessing-using-pipeline"><em>here</em></a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6364192310f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OBJECT ORIENTED PROGRAMMING]]></title>
            <link>https://medium.com/@iannyingi04/object-oriented-programming-abc362e4d77c?source=rss-92c29ce0be4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/abc362e4d77c</guid>
            <dc:creator><![CDATA[Ian Nyingi]]></dc:creator>
            <pubDate>Mon, 31 Oct 2022 18:10:11 GMT</pubDate>
            <atom:updated>2023-03-16T05:11:12.420Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/358/0*t4lNN3pcvKfIMLEU" /></figure><h4>Introduction</h4><p>Object-oriented programming, also known as OOP, is a computer science model that revolves around the use of objects as basic entities in a program. The goal of OOP is to prevent data from being accessed outside of the designated functions, as well as to reduce redundant or duplicate codes and provide users with an easy-to-use and simple interface. OOP is made up of objects, classes, methods, and the four pillars of OOP.</p><p>In order to understand what OOP is, it is best first to understand what objects are, and in order to understand what objects are, it is best first to understand what primitive data types are. Primitive data types are variables that store single, simple values. Examples of primitive data types are Byte, int, float Boolean, Double, and char.</p><p>In Python, you will use classes to create objects and write methods for them. A class is just a dictionary with two keys: __class__ and __dict__. The __class__ key will contain an object’s class name, while the __dict__ key will contain any attributes that have been defined on the object (e.g., attributes that are not part of its public interface).</p><p>For example, let’s say you have an object called “car” which has an attribute called “name”. If you wanted to write a method that would return the name of the car, you would do so like this:</p><p>class Car(object): def getName(self): return self.name.</p><p><em>Another example (is the </em><a href="https://levelup.gitconnected.com/chess-python-ca4532c7f5a4"><em>chess game</em></a><em>)</em></p><p>When programming a knight, queen, or any other piece on the chess board, there are plenty of variables that you will need in order for the given piece to work how it should. For instance, you’ll need a position variable to keep track of where the piece is. Additionally, you’ll need a boolean to represent whether a piece has been captured or not. You may also need a color variable to distinguish the player’s pieces.</p><p><strong>Object</strong>: An object is a real-life entity that can be anything, such as a car, person, employee, or something abstract that has certain properties and functions. Objects are instances of a class.</p><p><strong>Class:</strong> The blueprint from which objects are created is called a class. They define the objects’ structure and functions. The class makes it possible to create multiple objects of the same type without having to repeat code. Classes are templates for objects.</p><p><strong>Method</strong>: A method is a group of lines of code that perform a specific task. This could be as simple as printing to the console or as complex as performing calculations. They enable code reusability and save time.</p><h4><strong>The 4 pillars of OOP</strong></h4><p><strong>Abstraction</strong></p><p>Abstraction is a property that allows us to hide unnecessary details from the user. For example, if a user wants to order food, he or she should only have to provide order details and receive an order acknowledgment; they should not need to know how the order is processed internally or what mechanisms we use. Abstraction simplifies the interface so that the user only has to think about the input and output, not the process. It is best to think of your code in terms of interface and implementation.</p><p>The interface refers to how sections of code can communicate with one another. This is typically done through methods that each class is able to access. The implementation of these methods or how these methods are coded should be hidden. Abstraction allows the program to be worked on incrementally and prevents it from becoming entangled and complex. Therefore, it is inherent to determine specific points of contact that can act as an interface between classes and only worry about the implementation when coding it.</p><p><strong>Encapsulation</strong></p><p>Encapsulation is the process of tying data to code so that it is not accessible externally or to unauthorized personnel. This is accomplished by restricting access with access modifiers.</p><p>It also refers to bundling data with methods that can operate on that data within a class. It is the idea of hiding data within a class, preventing anything outside that class from directly interacting with it. However, this does not mean that members of other classes cannot interact at all with the attributes of another object. Members of other classes can interact with the attributes of another object through its methods. In practice, this is enabled by creating the getting methods (for retrieving information) and setting methods (for changing information). Setting methods also allow the programmer to easily keep track of attributes that depends on one another.</p><p><strong>Inheritance</strong></p><p>Inheritance is the property that allows one class to access the fields and methods of another. The class that inherits these fields is referred to as the subclass, and the class whose fields are inherited is referred to as the superclass. The subclass may have more fields and methods than the superclass. Inheritance is used to increase code reusability and make code more readable. Access modifiers change which classes have access to other classes, methods, or attributes.</p><p>There are three main types of Access modifiers and they include, public, private, and protected.</p><p>1. <em>Public members </em>can be accessed from anywhere in your program. This includes anywhere both inside of the class hierarchy it is defined as well as outside the rest of the program.</p><p>2. <em>Private members </em>can only be accessed from within the same class that the member is defined. This allows you to create multiple private members of the same name in different locations so that they do not conflict with one another.</p><p><em>3. Protected members </em>can be accessed within the class it is defined as well as any subclasses of that class. This essentially makes protect members private to the hierarchy in which they are defined.</p><p><strong>Polymorphism</strong></p><p>Polymorphism refers to the ability to have multiple methods with the same name but perform different functions. Method overriding or method overloading can be used to accomplish this. Method overriding refers to a subclass that implements a method different than the parent class, whereas method overloading refers to a function that has different parameter types or numbers of parameters. Polymorphism describes methods that are able to take on many forms.</p><ol><li><em>Dynamic polymorphism</em></li></ol><p>Occurs during the runtime of the program. It describes when a method signature is in both a subclass and a superclass. The methods share the same name or parameter but have different implementations. In this case, the implementation of the subclass that the object is an instance of overrides that of the superclass.</p><p><em>2. Static polymorphism</em></p><p>Occurs during compile-time rather than during runtime. It refers to when multiple methods within the same name but different arguments are defined in the same class.</p><p>In this case, the methods are differentiated by their parameters. Either they take a different number of parameters or different types of parameters or different orders of parameters. This is known as method overload. Despite the methods having the same name, their signatures are different due to their different arguments.</p><h4>Beginners Level Python OOP Projects</h4><p>Hangman: Hangman is a guessing game in which you have some letters of the word visible and you have to guess the rest without making too many mistakes or you get hanged.</p><p>Bank Account: A simple program to simulate your bank account with withdrawing and deposit functions.</p><p>Student Management: A simple project to display, search, add and delete new students.</p><p>Card Game: A simple care game in which each player can draw a card from the deck and the player with the highest card wins the game.</p><h4>Intermediate Level Python OOP Projects</h4><p>Hospital Management System: A Tkinter-based GUI app with SQL database for managing appointments.</p><p>Tic Tac Toe: A single-player or multiplayer tic tac toe game using python.</p><p>Expense Tracker App: A python application for adding income, and expenses, viewing, and updating details like salary and expenses.</p><h4>Advanced Python Python OOP Projects</h4><p>Speed Typing Test: Create a useful python project for checking your type speed using <a href="https://pypi.org/project/pygame/">pygame</a> GUI.</p><p>Music Player: Create an mp3 music player in python with Tkinter GUI.</p><p>Online Restaurant Food Ordering &amp; Management System: A Django-based website that showcases the restaurant details and menu and is able to take orders, add offers and different items, and manage delivery.</p><h4>Conclusion</h4><p>Object-oriented programming is a way to write programs that are organized around objects. In object-oriented programming, each object is an entity that has its own properties and behavior.OOP implementation is not restricted to simple or basic python projects. They are used in designing complex projects using other technologies in python also like web development with Django, game development, and machine learning.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abc362e4d77c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>