<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arum Puri </title>
    <description>The latest articles on DEV Community by Arum Puri  (@arum_puripratamawati_ef5).</description>
    <link>https://dev.to/arum_puripratamawati_ef5</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1613460%2Fd358efb1-bf82-48f3-91a5-86d2d759b207.jpeg</url>
      <title>DEV Community: Arum Puri </title>
      <link>https://dev.to/arum_puripratamawati_ef5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arum_puripratamawati_ef5"/>
    <language>en</language>
    <item>
      <title>Authentication and Authorization in Django: Django session</title>
      <dc:creator>Arum Puri </dc:creator>
      <pubDate>Thu, 05 Dec 2024 10:15:48 +0000</pubDate>
      <link>https://dev.to/arum_puripratamawati_ef5/authentication-and-authorization-in-django-django-session-ge6</link>
      <guid>https://dev.to/arum_puripratamawati_ef5/authentication-and-authorization-in-django-django-session-ge6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction to Django Sessions
&lt;/h2&gt;

&lt;p&gt;In modern web applications, maintaining user state across multiple requests is essential for creating personalized experiences. Django simplifies this with its built-in session framework, enabling developers to manage user data securely and efficiently.&lt;/p&gt;

&lt;p&gt;The built-in sessions in Django are responsible for managing user data over multiple requests. When users log in Django app, the server creates a session ID, usually stored in a cookie on the client’s browser. This session ID serves as a key for retrieving data stored on the server, and linking requests to a particular user. That is why authentication status will persist across different pages.&lt;/p&gt;




&lt;h2&gt;
  
  
  Using Session Middleware in Django
&lt;/h2&gt;

&lt;p&gt;Django’s session middleware automates session management. It processes incoming requests to retrieve session data and prepares outgoing responses to update or set session cookies. To check if session middleware is enabled, look in your &lt;code&gt;settings.py&lt;/code&gt; file under the MIDDLEWARE section:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# settings.py
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    # Other middleware
]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Types of Session Storage in Django
&lt;/h2&gt;

&lt;p&gt;We have several options for saving session data. Depending on the application you want to build, each has advantages and disadvantages.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Database-backed sessions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: Imagine the theater has a secure storage room with lockers where all coats are stored. Each locker is assigned a unique number that matches the number on your ticket. When you come back with your ticket, the attendant looks up the locker number in a logbook and retrieves your coat.&lt;/p&gt;

&lt;p&gt;Database-backed sessions save session data on a database server. So sensitive information such as user preferences, login status, and cart details remain saved securely on the backend. This type of session may be safer but causes some inconvenience when involving the writing and reading process. Database-backed sessions are slower compared to cache-backed sessions, so if you are building an application where the traffic is high then you should think again. Storing sessions in the database can increase the load on the database, impacting overall performance if not managed well. &lt;/p&gt;

&lt;p&gt;If you want to use a database-backed session, you need to add &lt;code&gt;django.contrib.sessions&lt;/code&gt; to your &lt;code&gt;INSTALLED_APPS&lt;/code&gt; setting. Please make sure to run &lt;code&gt;manage.py migrate&lt;/code&gt; to install the single database table that stores session data. &lt;/p&gt;

&lt;h3&gt;
  
  
  2. File-based sessions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: In this case, each coat is stored in a different, labeled locker in a large room at the back of the theater. Each locker has a unique tag or file with the coat details, and when you present your ticket, the attendant goes to the locker room, finds the corresponding tag, and retrieves your coat.&lt;/p&gt;

&lt;p&gt;File-based sessions use the server’s filesystem to save session data. This means each user session is stored in a separate file on the server. By default, Django stores session files in the django_session directory under &lt;code&gt;/tmp&lt;/code&gt; (on Unix-based systems) or in a directory specified in Django’s settings.&lt;/p&gt;

&lt;p&gt;To enable file-based sessions, set the &lt;code&gt;SESSION_ENGINE&lt;/code&gt; to &lt;code&gt;django.contrib.sessions.backends.file&lt;/code&gt; in your settings.py file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# settings.py
SESSION_ENGINE = 'django.contrib.sessions.backends.file'  # Use file-based session storage
SESSION_FILE_PATH = '/path/to/session/files/'  # Specify a directory for session files (optional)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Cache-backed sessions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: Here, the theater uses a temporary coat rack near the entrance, where coats are kept only shortly. This makes it very quick to fetch coats, but if the rack becomes full, the oldest coats may be moved to secondary storage or removed entirely.&lt;/p&gt;

&lt;p&gt;This type of session storage is where a caching system (such as Memcached or Redis) stores session data. Saving sessions in-memory caching will help applications with high traffic or requiring quick response times as the writing or reading process is very swift.&lt;/p&gt;

&lt;p&gt;To use cache-backed sessions, configure the &lt;code&gt;SESSION_ENGINE&lt;/code&gt; setting in your &lt;code&gt;settings.py&lt;/code&gt; file. You must also configure the cache depending on what cache memory you use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# settings.py
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'  # For caching session storage
SESSION_CACHE_ALIAS = 'default'  # Specify the cache alias if needed (e.g., 'redis' or 'memcached')

# Cache configuration (example with Redis)
CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': 'redis://127.0.0.1:6379/1',  # Redis URL
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, you can use &lt;code&gt;django.contrib.sessions.backends.cached_db&lt;/code&gt; which stores session data in both the cache and the database, falling back to the database if the cache is unavailable.&lt;/p&gt;

&lt;p&gt;The best advantages of using this type of session are scalability and speed. Cache-backed sessions are not only fast because saving data in memory but also reduce the load on the database session Data can be shared across servers making multiserver setup possible.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Signed cookie sessions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: Here, instead of keeping your coat in storage, the theater allows you to carry it around but requires you to have a special stamp on the ticket that verifies it’s your coat. You bring the coat (session data) with you, and each time you enter the theater, the attendant checks the stamp on the ticket to ensure it hasn’t been tampered with.  &lt;/p&gt;

&lt;p&gt;Signed cookie sessions in Django store session data directly on the client’s browser within a signed and encrypted cookie, rather than storing it on the server side (database or cache).&lt;/p&gt;

&lt;p&gt;To enable signed cookie sessions, set the &lt;code&gt;SESSION_ENGINE&lt;/code&gt; in Django’s &lt;code&gt;settings.py&lt;/code&gt; file to use the signed cookie backend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# settings.py
SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'
SECRET_KEY = 'your-secret-key'  # Make sure this key is kept secure and unique for your app

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Signed Cookie Sessions this reduces server load and frees up server resources. But, with Cookies's size limit ( around 4 KB), signed cookie sessions are unsuitable for storing large amounts of session data. Larger cookie sizes can lead to slower requests, as the cookie data is sent with every request.&lt;/p&gt;




&lt;h2&gt;
  
  
  Session Configuration Settings
&lt;/h2&gt;

&lt;p&gt;Django offers several settings to configure session behavior:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;SESSION_COOKIE_AGE&lt;/code&gt;: Sets the session expiration time (in seconds).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;SESSION_COOKIE_SECURE&lt;/code&gt;: Requires sessions to be transmitted over HTTPS.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;SESSION_EXPIRE_AT_BROWSER_CLOSE&lt;/code&gt;: Ends the session when the browser closes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;SESSION_COOKIE_HTTPONLY&lt;/code&gt;: Restricts JavaScript access to session cookies, enhancing security.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These settings help tailor session behavior to specific application needs. For more about session configuration please read &lt;a href="https://docs.djangoproject.com/en/5.1/ref/settings/#settings-sessions" rel="noopener noreferrer"&gt;Django documentation&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Implementing Sessions in Django Views
&lt;/h2&gt;

&lt;p&gt;To interact with sessions in Django views, use the &lt;code&gt;request.session object&lt;/code&gt;, which behaves like a dictionary. Here are some basic operations:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Storing data:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;request.session['username'] = 'Harry Potter'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Retrieving data:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;username = request.session.get('username')

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Deleting data:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;del request.session['username']

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A common use for sessions is to track user login status. Here’s how to implement a simple login system using sessions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# views.py
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login

def user_login(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user:
            login(request, user)
            request.session['username'] = username  # Store session data
            return redirect('home')
        else:
            return render(request, 'login.html', {'error': 'Invalid login'})
    return render(request, 'login.html')

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are still many methods for sessions in Django views. For a complete list, please check the &lt;a href="https://docs.djangoproject.com/en/5.1/topics/http/sessions/#using-sessions-in-views" rel="noopener noreferrer"&gt;Django documentation&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Session Best Practise
&lt;/h2&gt;

&lt;p&gt;Django periodically deletes expired sessions. You can customize the frequency by configuring the session cleanup process or running management commands like &lt;code&gt;django-admin clearsessions&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Avoid storing large amounts of data in sessions, as this may increase server load and slow response times. Lastly enable secure &lt;code&gt;cookies&lt;/code&gt;, &lt;code&gt;HttpOnly&lt;/code&gt;, and &lt;code&gt;HTTPS settings&lt;/code&gt; to protect session data.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Django’s session framework is powerful, flexible, and secure, making it easy to implement session management in your web applications. With proper configuration and secure practices, you can leverage Django sessions to create efficient, personalized user experiences while maintaining robust security.&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>webdev</category>
      <category>security</category>
    </item>
    <item>
      <title>Comprehensive Tutorial: Exploring Seaborn Lineplot</title>
      <dc:creator>Arum Puri </dc:creator>
      <pubDate>Sat, 30 Nov 2024 15:01:31 +0000</pubDate>
      <link>https://dev.to/arum_puripratamawati_ef5/comprehensive-tutorial-exploring-seaborn-lineplot-4p0</link>
      <guid>https://dev.to/arum_puripratamawati_ef5/comprehensive-tutorial-exploring-seaborn-lineplot-4p0</guid>
      <description>&lt;p&gt;Seaborn's lineplot is a powerful tool for visualizing trends and relationships in your data. In this tutorial, we’ll use lineplot to analyze how student attendance impacts exam scores, customizing our visualization with colors, markers, styles, and more.&lt;/p&gt;




&lt;h2&gt;
  
  
  Who is This Tutorial For?
&lt;/h2&gt;

&lt;p&gt;This tutorial is designed for those who:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Have experience using Python and libraries like Pandas.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Familiarity with code editors such as Visual Studio Code, Jupyter Notebook, or similar tools is recommended. For this tutorial, we’ll be using Visual Studio Code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ensure that Matplotlib and Seaborn are installed on your system. If you encounter any issues during installation, refer to the &lt;a href="https://matplotlib.org/stable/users/explain/quick_start.html#quick-start" rel="noopener noreferrer"&gt;Matplotlib documentation&lt;/a&gt; and &lt;a href="https://seaborn.pydata.org/installing.html" rel="noopener noreferrer"&gt;Seaborn documentation&lt;/a&gt; for guidance.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're new to Pandas, check out this &lt;a href="https://dev.to/arum_puripratamawati_ef5/mastering-pandas-in-python-a-beginners-guide-to-data-analysis-210c"&gt;Pandas crash course&lt;/a&gt; to get started.&lt;/p&gt;




&lt;h2&gt;
  
  
  What You'll Learn
&lt;/h2&gt;

&lt;p&gt;By the end of this tutorial, you’ll know how to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Load and prepare a dataset.&lt;/li&gt;
&lt;li&gt;Create basic and enhanced line plots.&lt;/li&gt;
&lt;li&gt;Customize plots using attributes like background styles, colors, error bars, markers, and more.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Step 1: Setting Up Your Project
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Download the Dataset&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Download the Student Performance Factors dataset from &lt;a href="https://www.kaggle.com/datasets/lainguyn123/student-performance-factors" rel="noopener noreferrer"&gt;Kaggle&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Extract the ZIP file and locate &lt;code&gt;StudentPerformanceFactors.csv&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Organize Your Files&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a folder named &lt;code&gt;data_visualization&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Move the dataset to this folder.&lt;/li&gt;
&lt;li&gt;Create a new Python script file named &lt;code&gt;visualization.py&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Step 2: Loading the Dataset
&lt;/h2&gt;

&lt;p&gt;Start loading the data into a Pandas DataFrame.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Import the libraries.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Import libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Loading Data&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Path of the file to read
filepath = "StudentPerformanceFactors.csv"

# Fill in the line below to read the file into a variable data
student_data= pd.read_csv(filepath)

# View the first few rows of the dataset
print(student_data.head())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
If your dataset is located in a different folder, update filepath to reflect the correct relative path.&lt;/p&gt;


&lt;h2&gt;
  
  
  Step 3: Creating a Basic Line Plot
&lt;/h2&gt;

&lt;p&gt;We’ll start by plotting how attendance affects exam scores.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic line plot&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Basic line plot
# This line is where you will change your code
sns.lineplot(data=student_data, x="Attendance", y="Exam_Score")

# Add title and labels
plt.title("How Attendance Affects Exam Scores")
plt.xlabel("Attendance (days)")
plt.ylabel("Exam Score")
plt.show()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execute the code by running &lt;strong&gt;python3 visualization.py&lt;/strong&gt; in the command line each time you want to test your changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75vp88p2gsr2z5ba9i21.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75vp88p2gsr2z5ba9i21.png" alt="Image description" width="640" height="480"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Step 4: Enhancing the Visualization
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Adding Categories with Hue&lt;/strong&gt;&lt;br&gt;
Add &lt;code&gt;hue&lt;/code&gt; attribute to add a gender category on your graph.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.lineplot(data=student_data, x="Attendance", y="Exam_Score", hue="Gender")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzsfawyy3nbh31awhe5zw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzsfawyy3nbh31awhe5zw.png" alt="Image description" width="640" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Customizing Colors&lt;/strong&gt;&lt;br&gt;
Use either predefined palettes or define custom colors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use a Predefined Palette&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use a predefined palette
sns.lineplot(data=student_data, x="Attendance", y="Exam_Score", hue="Gender", palette="coolwarm")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F03bc3dfyepovavbt7faz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F03bc3dfyepovavbt7faz.png" alt="Image description" width="640" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use a Custom Palette&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Define and apply a custom color palette
custom_palette = sns.color_palette(["#FF5733", "#33FF57"])  # Hex colors
sns.lineplot(data=student_data, x="Attendance", y="Exam_Score", hue="Gender", palette=custom_palette)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs09q9don2bchi3996ski.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs09q9don2bchi3996ski.png" alt="Image description" width="640" height="480"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Step 5: Adding Additional Attributes
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Error Bars&lt;/strong&gt;&lt;br&gt;
Visualize variability or confidence intervals using the &lt;code&gt;errorbar&lt;/code&gt; attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Add error bars (standard deviation)
sns.lineplot(data=student_data, x="Attendance", y="Exam_Score", hue="Gender", errorbar="sd")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdtur41eooldvrprl177o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdtur41eooldvrprl177o.png" alt="Image description" width="640" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Differentiating Line Styles&lt;/strong&gt;&lt;br&gt;
Use the &lt;code&gt;style&lt;/code&gt; attribute to represent categories with different line patterns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Differentiate line styles by gender
sns.lineplot(data=student_data, x="Attendance", y="Exam_Score", hue="Gender", style="Gender")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpka3ep3trkjt1k6w5ij3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpka3ep3trkjt1k6w5ij3.png" alt="Image description" width="640" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Customize Line Dashes&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Apply custom dashes for different categories
sns.lineplot(data=student_data, x="Attendance", y="Exam_Score", hue="Gender", style="Gender", dashes=[(2, 2), (4, 4)])

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp1dy5ngzvehgvmadmn15.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp1dy5ngzvehgvmadmn15.png" alt="Image description" width="640" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Add Markers to Highlight Data Points&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Add markers to the plot
sns.lineplot(data=student_data, x="Attendance", y="Exam_Score", hue="Gender", style="Gender", markers=True, dashes=False)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fps9on3o9lupd0pvgsvv4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fps9on3o9lupd0pvgsvv4.png" alt="Image description" width="640" height="480"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Step 6: Combining All Features
&lt;/h2&gt;

&lt;p&gt;Finally, all these features are combined into a comprehensive line plot.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Comprehensive line plot

sns.lineplot(
    data=student_data,
    x="Attendance",
    y="Exam_Score",
    hue="Gender",
    style="Gender",
    palette="coolwarm",
    markers=True,
    dashes=[(2, 2), (4, 4)],
    errorbar="sd"
)

# Add title and axis labels
plt.title("Comprehensive Line Plot: Attendance vs Exam Scores")
plt.xlabel("Attendance (days)")
plt.ylabel("Exam Score")

# Show the plot
plt.show()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgk6nk0n4fwja31p8cdug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgk6nk0n4fwja31p8cdug.png" alt="Image description" width="640" height="480"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Step 7: Additional Customizations
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Change Background Color&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Customize background color
plt.gca().set_facecolor("#EAEAF2")  # Light greyish-blue
plt.show()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frapobj5uvsnk8j2d24bt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frapobj5uvsnk8j2d24bt.png" alt="Image description" width="640" height="480"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Seaborn’s lineplot is a flexible and customizable tool for visualizing data trends. In this tutorial, you’ve learned to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create basic and enhanced line plots.&lt;/li&gt;
&lt;li&gt;Use features like hue, palette, errorbar, style, and markers.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Want to learn more? Check out my &lt;a href="https://dev.to/arum_puripratamawati_ef5/seaborn-plot-selection-made-easy-how-to-visualize-your-data-effectively-kan"&gt;Seaborn Cheatsheet&lt;/a&gt; or read the &lt;a href="https://dev.to/arum_puripratamawati_ef5/seaborn-plot-selection-made-easy-how-to-visualize-your-data-effectively-kan"&gt;Plot Selection Guide&lt;/a&gt; for inspiration on choosing the right plot for your data.&lt;/p&gt;

</description>
      <category>seaborn</category>
      <category>datascience</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Seaborn Cheat Sheet</title>
      <dc:creator>Arum Puri </dc:creator>
      <pubDate>Sat, 30 Nov 2024 03:16:12 +0000</pubDate>
      <link>https://dev.to/arum_puripratamawati_ef5/seaborn-cheat-sheet-3p26</link>
      <guid>https://dev.to/arum_puripratamawati_ef5/seaborn-cheat-sheet-3p26</guid>
      <description>&lt;p&gt;A concise guide to Seaborn for creating attractive and informative statistical graphics in Python.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install seaborn

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Importing Seaborn
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import seaborn as sns
import matplotlib.pyplot as plt

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  1. Relational Plots
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Function: &lt;code&gt;sns.relplot()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Creates scatter or line plots to show relationships between variables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbu2g1r258kpcz2inwhf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbu2g1r258kpcz2inwhf.png" alt="Image description" width="800" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Example: Scatter Plot&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.relplot(data=tips, x="total_bill", y="tip", hue="time", kind="scatter")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- Example: Line Plot&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.relplot(data=tips, x="size", y="tip", kind="line", hue="day", style="time")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Common Attribute for Relational Plots&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F64f3fnc83hqoa0uu4xgw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F64f3fnc83hqoa0uu4xgw.png" alt="Image description" width="800" height="296"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  2. Distribution Plots
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Function: &lt;code&gt;sns.displot()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Visualizes distributions with histograms, KDEs, rugs, or ECDFs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frrrrpua9c9unz7jurclu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frrrrpua9c9unz7jurclu.png" alt="Image description" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Histogram Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.displot(data=penguins, x="flipper_length_mm", kind="hist", bins=20)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- KDE Plot Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.displot(data=penguins, x="flipper_length_mm", kind="kde", fill=True)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Common Attribute for Distribution Plots&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9bfd32n9hvgdt14an05n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9bfd32n9hvgdt14an05n.png" alt="Image description" width="800" height="883"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  3. Categorical Plots
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Function: &lt;code&gt;sns.catplot()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Visualizes categorical data using multiple plot types.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2irdk2lkx7thnk983rpk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2irdk2lkx7thnk983rpk.png" alt="Image description" width="800" height="635"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Example: Bar Plot&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.catplot(data=tips, x="day", y="total_bill", kind="bar", hue="sex")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- Example: Violin Plot&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.catplot(data=tips, x="day", y="total_bill", kind="violin", split=True, hue="sex")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Common Attribute for Categorical Plots&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjfdvhr2m2w46n2a5r78c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjfdvhr2m2w46n2a5r78c.png" alt="Image description" width="800" height="250"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Color Palettes (palette)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwulmy56ro21be38jur2g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwulmy56ro21be38jur2g.png" alt="Image description" width="800" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Custom Palettes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HUSL Palette: &lt;code&gt;sns.color_palette("husl", n_colors=8)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;CUBEHELIX Palette: &lt;code&gt;sns.color_palette("cubehelix", n_colors=8)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Custom HEX Colors: [&lt;code&gt;"#4c72b0"&lt;/code&gt;, &lt;code&gt;"#55a868"&lt;/code&gt;, &lt;code&gt;"#c44e52"&lt;/code&gt;]&lt;/li&gt;
&lt;li&gt;Blend Two Colors: &lt;code&gt;sns.blend_palette(["red", "blue"], n_colors=8)
&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Style Attributes
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9w6fett1mqdnxhdsb4kf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9w6fett1mqdnxhdsb4kf.png" alt="Image description" width="800" height="306"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Advanced Distribution Attributes
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyjydj9zhk8ynmpy44wft.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyjydj9zhk8ynmpy44wft.png" alt="Image description" width="800" height="497"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Common Attributes for All Plots
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxm307w9ranpgugx7snnd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxm307w9ranpgugx7snnd.png" alt="Image description" width="800" height="750"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>seaborn</category>
      <category>datascience</category>
      <category>cheatsheet</category>
      <category>dataengineering</category>
    </item>
    <item>
      <title>Seaborn Plot Selection Made Easy: How to Visualize Your Data Effectively</title>
      <dc:creator>Arum Puri </dc:creator>
      <pubDate>Fri, 29 Nov 2024 02:35:45 +0000</pubDate>
      <link>https://dev.to/arum_puripratamawati_ef5/seaborn-plot-selection-made-easy-how-to-visualize-your-data-effectively-kan</link>
      <guid>https://dev.to/arum_puripratamawati_ef5/seaborn-plot-selection-made-easy-how-to-visualize-your-data-effectively-kan</guid>
      <description>&lt;p&gt;Data visualization is one of the most powerful tools for analyzing and presenting data. Seaborn, a Python library built on Matplotlib, provides a high-level interface for creating informative and diverse visualizations. This article will guide you through choosing the right Seaborn plot, customizing it for clarity, and avoiding common pitfalls.&lt;/p&gt;

&lt;p&gt;Why Choosing the Right Plot Type Matters?&lt;/p&gt;

&lt;p&gt;The type of plot you choose directly impacts how effectively your data presents its insight and information. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;scatterplot&lt;/strong&gt; reveals correlations between variables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;heatmap&lt;/strong&gt; simplifies large-scale comparisons.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using the wrong plot type can lead to misinterpretation, and sometimes those insights from data are buried and never revealed because we choose the wrong visualization. &lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Seaborn Plot Categories
&lt;/h2&gt;

&lt;p&gt;Seaborn plots fall into three main categories: &lt;strong&gt;Relational&lt;/strong&gt;, &lt;strong&gt;Distribution&lt;/strong&gt;, and &lt;strong&gt;Categorical&lt;/strong&gt;. Here's how to choose and use each.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3comje47pqcfmaulbpix.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3comje47pqcfmaulbpix.png" alt="Image description" width="633" height="457"&gt;&lt;/a&gt;&lt;br&gt;
source:&lt;a href="https://seaborn.pydata.org/_images/function_overview_8_0.png" rel="noopener noreferrer"&gt;https://seaborn.pydata.org/_images/function_overview_8_0.png&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Relational Plots
&lt;/h3&gt;

&lt;p&gt;Relational plots visualize the relationship between two variables, typically numerical. Seaborn provides two main types of relational plots: scatter plots and line plots. You can create these plots using the&lt;code&gt;relplot()&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.relplot(
    data=tips,
    x="total_bill", y="tip", hue="smoker", style="time",
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4xbs0rb0nlo79n1yh3q7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4xbs0rb0nlo79n1yh3q7.png" alt="Image description" width="514" height="426"&gt;&lt;/a&gt;&lt;br&gt;
source: &lt;a href="https://seaborn.pydata.org/_images/relational_10_0.png" rel="noopener noreferrer"&gt;seaborn documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alternatively, you can use the &lt;code&gt;scatterplot()&lt;/code&gt; function directly for scatter plots, which produce the same result. For line plots, you can either use &lt;code&gt;relplot()&lt;/code&gt; with &lt;code&gt;kind="line"&lt;/code&gt; or the more direct &lt;code&gt;lineplot()&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fmri = sns.load_dataset("fmri")
sns.relplot(data=fmri, x="timepoint", y="signal", kind="line")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or you can write like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fmri = sns.load_dataset("fmri")
sns.lineplot(data=fmri, x="timepoint", y="signal")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result will still the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjo9s7bcdnpv7bhrqjx8x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjo9s7bcdnpv7bhrqjx8x.png" alt="Image description" width="425" height="425"&gt;&lt;/a&gt;&lt;br&gt;
source: &lt;a href="https://seaborn.pydata.org/_images/relational_23_0.png" rel="noopener noreferrer"&gt;seaborn documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Scatter plots display individual data points, making it easy to identify patterns or correlations. On the other hand, line plots are ideal for showcasing trends over time or across categories.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Distribution Plots
&lt;/h3&gt;

&lt;p&gt;Understanding the distribution of variables is a critical first step in analyzing or modeling data. Distribution plots are designed to reveal the spread or dispersion of a single variable. These visualizations can quickly address key questions, such as: What range does the data cover? What is its central tendency? Is the data skewed in a particular direction?&lt;/p&gt;

&lt;p&gt;Like relational plots, distribution plots can be created using the &lt;code&gt;displot()&lt;/code&gt;function by specifying the kind parameter to select the desired plot type. Alternatively, you can directly use functions like &lt;code&gt;histplot()&lt;/code&gt;, &lt;code&gt;kdeplot()&lt;/code&gt;, &lt;code&gt;ecdfplot()&lt;/code&gt;, or &lt;code&gt;rugplot()&lt;/code&gt; for specific distribution visualizations.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;histplot()&lt;/code&gt; function is excellent for visualizing frequency distributions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.displot(penguins, x="flipper_length_mm", hue="sex", multiple="dodge")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedkyc9klei0y29cvn35t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedkyc9klei0y29cvn35t.png" alt="Image description" width="528" height="425"&gt;&lt;/a&gt;&lt;br&gt;
source:&lt;a href="https://seaborn.pydata.org/_images/distributions_23_0.png" rel="noopener noreferrer"&gt;seaborn documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;kdeplot()&lt;/code&gt; is more suited for displaying smooth distribution curves, while the &lt;code&gt;ecdfplot()&lt;/code&gt; emphasizes cumulative proportions. The &lt;code&gt;rugplot()&lt;/code&gt; adds detailed markers for raw data points, enhancing other visualizations with finer detail.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqg6xum802cedg1qaktum.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqg6xum802cedg1qaktum.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Seaborn also supports visualizing bivariate distributions using tools like &lt;code&gt;heatmap()&lt;/code&gt;. Heatmaps are particularly effective for illustrating correlation matrices or making comparisons.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F18mescsn5t1qobhqfhqi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F18mescsn5t1qobhqfhqi.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Categorical Plots
&lt;/h3&gt;

&lt;p&gt;Categorical plots are designed to visualize data organized into categories. The general approach for creating these plots is using the &lt;code&gt;catplot()&lt;/code&gt; function, specifying the kind parameter to select the desired plot type. These plots are categorized into three main families.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F64plszfohvimmx8aob1b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F64plszfohvimmx8aob1b.png" alt="Image description" width="800" height="600"&gt;&lt;/a&gt;&lt;br&gt;
Choosing the right type of categorical plot depends on the specific question you aim to answer. These plots provide multiple perspectives for analyzing categorical data:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Categorical scatterplots&lt;/strong&gt;&lt;br&gt;
These plots display individual data points within categories, helping to identify patterns or distributions. Examples include &lt;code&gt;stripplot()&lt;/code&gt; and&lt;code&gt;swarmplot()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tips = sns.load_dataset("tips")
sns.catplot(data=tips, x="day", y="total_bill")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgi0jhso75gsdymjjq8p9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgi0jhso75gsdymjjq8p9.png" alt="Image description" width="444" height="425"&gt;&lt;/a&gt;&lt;br&gt;
source: &lt;a href="https://seaborn.pydata.org/_images/categorical_3_0.png" rel="noopener noreferrer"&gt;seaborn documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Categorical distribution plots&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These plots summarize data distribution within categories, offering insights into variability, spread, and central tendencies. Examples include &lt;code&gt;boxplot()&lt;/code&gt;, &lt;code&gt;violinplot()&lt;/code&gt;, and &lt;code&gt;boxenplot()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F79r5km93vyxw9djp90a0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F79r5km93vyxw9djp90a0.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Categorical estimate plots&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These plots calculate aggregated estimates (e.g., mean) and include error bars to show variability or confidence intervals. Examples include &lt;code&gt;barplot()&lt;/code&gt;,&lt;code&gt;pointplot()&lt;/code&gt;, and &lt;code&gt;countplot()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe99lcnit82upi27fdq4u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe99lcnit82upi27fdq4u.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How to Choose the Right Seaborn Plot
&lt;/h2&gt;

&lt;p&gt;Before plotting, ask yourself these questions:&lt;/p&gt;

&lt;p&gt;Is the data categorical, numerical, or both?&lt;/p&gt;

&lt;p&gt;Are you exploring relationships, distributions, or comparisons?&lt;/p&gt;

&lt;p&gt;What size and scale is the dataset?&lt;/p&gt;

&lt;p&gt;Knowing your data guides you to the most appropriate visualization tools. The schema below is from Kaggle and shows how to choose your graph based on what kind of data you have.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi15dzngi4e6q043bl0ih.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi15dzngi4e6q043bl0ih.png" alt="Image description" width="800" height="410"&gt;&lt;/a&gt;&lt;br&gt;
source: &lt;a href="https://storage.googleapis.com/kaggle-media/learn/images/LPWH19I.png" rel="noopener noreferrer"&gt;kaggle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s work with real-world data to make this practical. Consider a &lt;a href="https://www.kaggle.com/datasets/lainguyn123/student-performance-factors" rel="noopener noreferrer"&gt;dataset from Kaggle&lt;/a&gt; containing 20 columns, including features such as Hours Studied, Attendance, Parental Involvement, Access to Resources, Extracurricular Activities, Sleep Hours, Previous Scores, Motivation Level, Internet Access, Tutoring Sessions, Family Income, Teacher Quality, School Type, Peer Influence, Physical Activity, Learning Disabilities, Parental Education Level, Distance from Home, Gender, and Exam Score.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Understand Your Data
Begin by analyzing the types of variables in your dataset to understand the data. Numerical variables are best suited for relational or distribution plots, while categorical variables work well for grouping or comparisons. For instance, you can use a line plot to analyze trends in math performance based on attendance. Similarly, a histplot can be utilized to examine the distribution of Sleep Hours, helping to determine whether most students are getting enough rest.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.lineplot(data=student_data, x="Attendance", y="Exam_Score", hue="Gender")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhxrdqxa06i67xncu0vi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhxrdqxa06i67xncu0vi.png" alt="Image description" width="516" height="398"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.histplot(data=student_data, x="Sleep_Hours", hue="Gender", multiple="dodge")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffn7r6vl9bu20u304m7ug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffn7r6vl9bu20u304m7ug.png" alt="Image description" width="471" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define Your Objective
Determine your objective by asking what insights you aim to convey. Want to compare groups? Opt for a categorical plot like a barplot or boxplot. Interested in exploring relationships? A relational plot such as a scatterplot is a great choice. Looking to understand variability? Go with a distribution plot like a histplot. For example, a scatterplot effectively displays the relationship between two numerical variables, with each point representing an observation. This makes it easy to spot correlations, clusters, or outliers. Visualizing how Hours Studied impact Exam Scores can reveal whether more study time correlates with higher scores.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.scatterplot(data=student_data, x="Sleep_Hours", y="Exam_Score")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mvyehnrghd3f9fgdk3p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mvyehnrghd3f9fgdk3p.png" alt="Image description" width="478" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Match the Plot to Your Data and Goal
Selecting the appropriate plot for your data and analysis objectives is essential. The right visualization allows you to extract meaningful insights effectively. For instance, a line plot is more suitable for observing trends over time compared to a histogram. Using an incorrect plot can obscure important patterns or insights, rendering even a rich dataset less useful. For example, a barplot is ideal for comparing average exam scores across different levels of parental involvement. This plot highlights the mean (or other summary statistics) of a numerical variable across categories, making it perfect for high-level comparisons.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sns.barplot(data=student_data, x="Parental_Involvement", y="Exam_Score", ci="sd", palette="Set2")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpptvay3f86vvebc02kk3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpptvay3f86vvebc02kk3.png" alt="Image description" width="468" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Tips for Customizing Seaborn Plots
&lt;/h3&gt;

&lt;p&gt;Increase clarity in your visualizations by adding titles and labels using functions like &lt;code&gt;plt.title()&lt;/code&gt;, &lt;code&gt;plt.xlabel()&lt;/code&gt;, and &lt;code&gt;plt.ylabel()&lt;/code&gt;. To incorporate categorical dimensions, leverage the hue attribute in Seaborn, which allows you to distinguish data points based on a specific column in your dataset. Customize the color scheme with palettes such as &lt;code&gt;coolwarm&lt;/code&gt;,&lt;code&gt;husl&lt;/code&gt;, or &lt;code&gt;Set2&lt;/code&gt; by using the &lt;code&gt;set_palette()&lt;/code&gt; function. Additionally, differentiate data points by adjusting their style or size with &lt;code&gt;sns.set_theme()&lt;/code&gt; and defining the figure dimensions using &lt;code&gt;plt.figure(figsize=(width, height))&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Common Pitfalls to Avoid
&lt;/h3&gt;

&lt;p&gt;To effectively communicate insights through data visualization, it’s crucial to balance between providing sufficient information and avoiding overcrowding the plots. Overloading a graph with excessive data points can overwhelm viewers, while insufficient details may lead to confusion. Always include clear axis labels and a legend, and ensure the visualization emphasizes the key insights you want to highlight.&lt;/p&gt;

&lt;p&gt;Another common issue is creating misleading visualizations. To prevent this, ensure the axes are appropriately scaled accurately to represent the data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnih76axhju2wrjnvso6q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnih76axhju2wrjnvso6q.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Selecting the right Seaborn plot is a critical step in enhancing data understanding and effectively communicating insights. The appropriate visualization can uncover patterns, relationships, and trends that might remain hidden. By aligning the plot type with your data structure and analysis goals—whether exploring distributions, relationships, or comparisons—you ensure clarity and precision in your storytelling.&lt;/p&gt;

&lt;p&gt;Data visualization is as much an art as it is a science. Don’t hesitate to experiment with different Seaborn plots to uncover new perspectives or refine your insights. With practice and creativity, you’ll be able to leverage the full potential of Seaborn to transform raw data into compelling visual narratives.&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>beginners</category>
      <category>seaborn</category>
      <category>python</category>
    </item>
    <item>
      <title>Why Seeing Data Beats Reading It: The Case for Data Visualization</title>
      <dc:creator>Arum Puri </dc:creator>
      <pubDate>Thu, 28 Nov 2024 03:56:11 +0000</pubDate>
      <link>https://dev.to/arum_puripratamawati_ef5/why-seeing-data-beats-reading-it-the-case-for-data-visualization-c1g</link>
      <guid>https://dev.to/arum_puripratamawati_ef5/why-seeing-data-beats-reading-it-the-case-for-data-visualization-c1g</guid>
      <description>&lt;p&gt;In today's data-driven world, raw data floods every sector. From complicated business metrics to simple data in everyday life. How many cars pass the road each day, how many students pass their math exams, or even how many eggs do you consume daily? The answers to all those questions are data.&lt;/p&gt;

&lt;p&gt;Raw data are often full of numbering rows and columns or spreadsheets. They are overwhelming and hard to interpret. To unlock actionable insights, we need to transform this data into something easier to understand—this is where data visualization plays its role.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Visualization Works: The Science Behind It
&lt;/h3&gt;

&lt;p&gt;Data visualization is the process of graphically representing information and data. It can be a chart, a graph, or a map. Through this visual media, we can see data from several perspectives, such as using a line plot to see a trend and a histogram to see a distribution.&lt;/p&gt;

&lt;p&gt;The effectiveness of data visualization lies in the way the human brain processes visual stimuli. Gestalt Principles of Visual Perception explain how the human brain processes visual information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Gestalt principles&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Proximity:&lt;/strong&gt; Objects that are physically close together belong to part of a group.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Similarity:&lt;/strong&gt; Objects with similar color, shape, size, or orientation are perceived as related or belonging to the same group.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Continuity:&lt;/strong&gt; Smooth paths are naturally followed, making line graphs intuitive for tracking trends.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Connection:&lt;/strong&gt; Objects that are physically connected are part of a group.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgmhqwfgipqx46qdn7te3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgmhqwfgipqx46qdn7te3.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enclosure:&lt;/strong&gt; Objects that are physically enclosed together as belonging to part of a group.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Closure:&lt;/strong&gt; Our brains fill in missing information to create complete shapes, aiding the understanding of fragmented visuals like dashed trend lines.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4wvmk2kwmemcm410lpiu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4wvmk2kwmemcm410lpiu.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Top Benefits of Data Visualization
&lt;/h3&gt;

&lt;p&gt;Research stated that 90% of the information transmitted to the brain is visual. Using data visualization as a communication tool is an effective way to leverage the brain’s natural ability to process information visually.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Visual Patterns Are Easier to Recognize&lt;/strong&gt;&lt;br&gt;
The human brain is wired to identify patterns, trends, and relationships. When data is presented as charts, graphs, or diagrams, patterns like upward trends, clusters, or anomalies are immediately apparent. For example, look at the picture below. For example, consider the 2024 USA election trend from July to August. The visual representation shows a tight race between the Democratic and Republican candidates, with their support levels running neck and neck. This visualization conveys the uncertainty of the outcome, emphasizing that both Trump and Kamala Harris remain strong contenders for victory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3k32yokpcws4y0c3uqp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3k32yokpcws4y0c3uqp.png" alt="Image description" width="630" height="358"&gt;&lt;/a&gt;&lt;br&gt;
source:&lt;a href="https://static.independent.co.uk/2024/08/21/17/Screenshot-2024-08-21-at-17.48.24.jpg?quality=75&amp;amp;width=640&amp;amp;auto=webp" rel="noopener noreferrer"&gt;The Independent&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Anomaly Detection Faster to Spot&lt;/strong&gt;&lt;br&gt;
Visualization helps users spot outliers quickly. For instance, tools like scatter plots and heatmaps make it easier to see correlations or anomalies hidden in the raw data. Identifying such patterns is crucial for decision-making in finance, marketing, and healthcare​.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbs1b9cayo4c36byq7gqf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbs1b9cayo4c36byq7gqf.gif" alt="Image description" width="496" height="370"&gt;&lt;/a&gt;&lt;br&gt;
source:&lt;a href="https://sites.chem.utoronto.ca" rel="noopener noreferrer"&gt;https://sites.chem.utoronto.ca&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Simplifies Complexity&lt;/strong&gt;&lt;br&gt;
Reading raw data means adding cognitive load to the brain. By aggregating or summarizing data, they reduce cognitive load and help viewers focus on the most important aspects. The visualization below illustrates how Americans identified their political affiliations from 2001 to 2024. Analyzing 23 years of data in its raw format would be almost impossible for most people to interpret effectively. However, with data visualization, complex patterns become immediately clear. For instance, the chart highlights a consistent trend: women tend to lean more toward liberal ideologies, while men gravitate toward conservative views. This is a prime example of how data visualization condenses decades of information into a digestible story.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9yagnmu5c9gwgaj108z7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9yagnmu5c9gwgaj108z7.png" alt="Image description" width="678" height="550"&gt;&lt;/a&gt;&lt;br&gt;
source:&lt;a href="https://static01.nyt.com/images/2024/10/31/learning/YoungVotersGraphLN/YoungVotersGraphLN-jumbo.png?quality=75&amp;amp;auto=webp" rel="noopener noreferrer"&gt;The New York Times&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Improved Retention&lt;/strong&gt;&lt;br&gt;
People are more likely to remember insights from visuals. The retention of data presented in charts or infographics can be significantly higher than the textual data. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Time Efficiency&lt;/strong&gt;&lt;br&gt;
A study conducted by McKinsey revealed that companies adopting data visualization 28% tend to make timely decisions. In fields where time is money, data visualization can help businesses grow by improving their efficiency. &lt;/p&gt;

&lt;p&gt;Another example of the power of data visualization can be seen in the 2024 U.S. election. A graph illustrating shows issues deemed important for swing voters, Trump loyalists, and Harris loyalists. Once political advisors see the graph they will create their next plan to sway swing voters and suggest strategic plans to their employer to win the election&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs3k6nedbr89pdmf93ug3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs3k6nedbr89pdmf93ug3.png" alt="Image description" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Accessibility to Non-Technical Audiences&lt;/strong&gt;&lt;br&gt;
Experts in different fields often collaborate on projects within the same organization. However, technical jargon can create barriers to effective communication and lead to misunderstandings. Data visualization bridges this gap by presenting complex information in a simple format. This ensures that non-technical stakeholders can grasp key insights, fostering more inclusive and effective decision-making.&lt;/p&gt;

&lt;h2&gt;
  
  
  Avoid These Common Data Visualization Mistakes
&lt;/h2&gt;

&lt;p&gt;While data visualization is a powerful tool, it has its pitfalls. One of the most often happening is manipulating axes or cherry-picking data which can lead to misinterpretation. A bar chart showing a difference between two values can appear exaggerated if the y-axis doesn’t start at zero. For instance, if one bar represents 40 and another 50, scaling the y-axis from 36 to 50 will make the difference look significant, misleading viewers into overestimating the disparity. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa0o3dytbr6mg2li9tkc3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa0o3dytbr6mg2li9tkc3.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another pitfall is cramming too much data into a single chart because it overwhelms viewers and choosing the wrong graph type. &lt;/p&gt;

&lt;h3&gt;
  
  
  Tools and Techniques for Data Visualization.
&lt;/h3&gt;

&lt;p&gt;Nowadays, tools for data visualization are widely available. Tools like Tableau, Power BI, and Python libraries such as Seaborn and Dash offer customization depending on the need. For developers, libraries like Seaborn and Dash are an excellent choice. Especially when dealing with data analysis in machine learning or artificial intelligence, Seaborn offers several types of graphs to analyze data with the help of other powerful libraries like Pandas and NumPy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Embrace Data Visualization for Better Insights
&lt;/h3&gt;

&lt;p&gt;Data visualization turns numbers into narratives. Simplifying complex datasets into accessible visuals bridges gaps between technical and non-technical audiences, enhances retention, and speeds up insights. Start leveraging data visualization today to unlock the full potential of your data-driven initiatives.&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>seaborn</category>
      <category>beginners</category>
      <category>python</category>
    </item>
    <item>
      <title>Python NumPy Tutorial for Beginners: Learn Array Creation, Indexing, and More</title>
      <dc:creator>Arum Puri </dc:creator>
      <pubDate>Thu, 07 Nov 2024 04:22:31 +0000</pubDate>
      <link>https://dev.to/arum_puripratamawati_ef5/python-numpy-tutorial-for-beginners-learn-array-creation-indexing-and-more-ajg</link>
      <guid>https://dev.to/arum_puripratamawati_ef5/python-numpy-tutorial-for-beginners-learn-array-creation-indexing-and-more-ajg</guid>
      <description>&lt;p&gt;NumPy, short for 'Numerical Python,' is one of the core libraries for numerical computing in Python. Known for its powerful array operations, NumPy makes data manipulation and mathematical calculations in Python efficient and fast, without the need for slow, traditional loops. Not only that NumPy also has many features including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, and many more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installing NumPy&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install numpy

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A. Basic NumPy Operation
&lt;/h2&gt;

&lt;p&gt;NumPy provides a variety of built-in functions that simplify math and data manipulation. Before diving into specific functions, it’s crucial to understand how NumPy arrays work and how to manipulate data within them. This knowledge will serve as a foundation for using NumPy’s more advanced features effectively.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Create a NumPy ndarray Object
&lt;/h3&gt;

&lt;p&gt;To access the full range of functions in NumPy, you need to create a NumPy array, or ndarray. Arrays can be created in several ways, depending on the type and structure of data you need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np

# Creating a 1D ndarray object
arr = np.array([1, 2, 3, 4, 5])
print("1D array:", arr)

# Creating a 2D ndarray object
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print("2D array:\n", arr_2d)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1D array: [1 2 3 4 5]
2D array:
 [[1 2 3]
 [4 5 6]]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0bk6etnszc61nl0hkndn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0bk6etnszc61nl0hkndn.png" alt="Image description" width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- np.ones(): Create an array of ones.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create a 1D array of ones with 5 elements
arr_ones_1d = np.ones(5)
print("1D array of ones:", arr_ones_1d)

# Create a 2D array of ones with shape (3, 4)
arr_ones_2d = np.ones((3, 4))
print("2D array of ones:\n", arr_ones_2d)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1D array of ones: [1. 1. 1. 1. 1.]
2D array of ones:
 [[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4eoau41ffia8onnyof81.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4eoau41ffia8onnyof81.png" alt="Image description" width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- np.zeros(): Create an array of zeros.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create a 1D array of zeros with 5 elements
arr_zeros_1d = np.zeros(5)
print("1D array of zeros:", arr_zeros_1d)

# Create a 2D array of zeros with shape (2, 3)
arr_zeros_2d = np.zeros((2, 3))
print("2D array of zeros:\n", arr_zeros_2d)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1D array of zeros: [0. 0. 0. 0. 0.]
2D array of zeros:
 [[0. 0. 0.]
 [0. 0. 0.]]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhf6t7mhrj204952h56iu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhf6t7mhrj204952h56iu.png" alt="Image description" width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- np.eye(): Create an identity matrix.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create a 3x3 identity matrix
identity_matrix = np.eye(3)
print("3x3 identity matrix:\n", identity_matrix)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3x3 identity matrix:
 [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhmh5pkbeg9gb27qiciou.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhmh5pkbeg9gb27qiciou.png" alt="Image description" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- np.full(): Create an array filled with a specified value.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create a 2x3 array filled with the value 7
arr_full = np.full((2, 3), 7)
print("Array filled with 7:\n", arr_full)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array filled with 7:
 [[7 7 7]
 [7 7 7]]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqc3txko27jfvuos8r6f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqc3txko27jfvuos8r6f.png" alt="Image description" width="800" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Array Slicing
&lt;/h3&gt;

&lt;p&gt;Array Slicing in NumPy is relatively similar to Python for 1 dimension. In a two-dimensional array, the elements at each index are no longer scalars but rather one-dimensional arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Slicing a 1D array
slice_1d = arr[1:4]
print("Slice of 1D array (index 1 to 3):", slice_1d)

# Slicing a 2D array
slice_2d = arr_2d[:, 1:3]
print("Slice of 2D array (all rows, col 1 to 2):\n", slice_2d)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Slice of 1D array (index 1 to 3): [2 3 4]
Slice of 2D array (all rows, col 1 to 2):
 [[2 3]
 [5 6]]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy57gldlc12ytlvovbs9n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy57gldlc12ytlvovbs9n.png" alt="Image description" width="800" height="471"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Array Shape
&lt;/h3&gt;

&lt;p&gt;The shape of an array tells us its dimensions. This is especially helpful in data analysis and machine learning when working with matrices or higher-dimensional data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("Shape of 1D array:", arr.shape)
print("Shape of 2D array:", arr_2d.shape)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Shape of 1D array: (5,)
Shape of 2D array: (2, 3)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Array Reshape
&lt;/h3&gt;

&lt;p&gt;Reshaping allows you to change the dimensions of an array. This is useful when reorganizing data or preparing it for machine learning models.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Reshaping a 1D array into a 2D array (3 rows, 2 columns)
reshaped_arr = arr.reshape((3, 2))
print("Reshaped array (3x2):\n", reshaped_arr)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Reshaped array (3x2):
 [[1 2]
 [3 4]
 [5 0]]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwar4omsq5zh3oq0ed4xm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwar4omsq5zh3oq0ed4xm.png" alt="Image description" width="800" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Array Iterating
&lt;/h3&gt;

&lt;p&gt;You can iterate over a NumPy array just like any other Python iterable, but NumPy provides built-in functions to make this efficient.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("Iterating over 1D array:")
for x in arr:
    print(x)

print("Iterating over 2D array:")
for row in arr_2d:
    print(row)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Iterating over 1D array:
1
2
3
4
5
Iterating over 2D array:
[1 2 3]
[4 5 6]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Array Join
&lt;/h3&gt;

&lt;p&gt;If you want to combine two arrays into one, use the concatenate function in NumPy. This is helpful when merging datasets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

joined_arr = np.concatenate((arr1, arr2))
print("Joined array:", joined_arr)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Joined array: [1 2 3 4 5 6]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzqro3ogz7aa7cl3sx49c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzqro3ogz7aa7cl3sx49c.png" alt="Image description" width="800" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Array Split
&lt;/h3&gt;

&lt;p&gt;The split function allows you to divide an array into multiple parts, which can be helpful when working with sections of data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;split_arr = np.array_split(joined_arr, 3)
print("Split array into 3 parts:", split_arr)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Split array into 3 parts: [array([1, 2]), array([3, 4]), array([5, 6])]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fczus1rklfnjab2i97ths.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fczus1rklfnjab2i97ths.png" alt="Image description" width="800" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Array Search
&lt;/h3&gt;

&lt;p&gt;You can use np.where to search for values within an array and return the indices where a condition is met.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search_result = np.where(joined_arr == 4)
print("Index where element is 4:", search_result[0])

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index where element is 4: [3]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ficf5fby0npli0izdxz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ficf5fby0npli0izdxz.png" alt="Image description" width="800" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Array Sort
&lt;/h3&gt;

&lt;p&gt;Sorting data is often essential in data analysis to identify trends or patterns. Use np.sort to sort arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unsorted_arr = np.array([3, 1, 5, 2, 4])
sorted_arr = np.sort(unsorted_arr)
print("Sorted array:", sorted_arr)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sorted array: [1 2 3 4 5]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frr8nhkkwif8b37d88781.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frr8nhkkwif8b37d88781.png" alt="Image description" width="800" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  10. Array Filter
&lt;/h3&gt;

&lt;p&gt;Filtering in NumPy can be done using boolean indexing. This allows you to extract elements based on certain conditions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;filter_condition = joined_arr &amp;gt; 3
filtered_arr = joined_arr[filter_condition]
print("Filtered array (elements &amp;gt; 3):", filtered_arr)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Filtered array (elements &amp;gt; 3): [4 5 6]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedn93571c890gndl5pgh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedn93571c890gndl5pgh.png" alt="Image description" width="800" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  B. Universal functions (ufuncs)
&lt;/h2&gt;

&lt;p&gt;Universal functions (ufuncs) in NumPy are functions that support operations like addition, subtraction, trigonometric functions, and more. Below are examples demonstrating how to use some common universal functions in NumPy:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Arithmetic Operations
&lt;/h3&gt;

&lt;p&gt;Arithmetic is a mathematics branch that studies numerical operations like addition, subtraction, multiplication, and division.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np

# Create two arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

# Addition
add_result = np.add(arr1, arr2)
print("Addition:", add_result)

# Subtraction
sub_result = np.subtract(arr1, arr2)
print("Subtraction:", sub_result)

# Multiplication
mul_result = np.multiply(arr1, arr2)
print("Multiplication:", mul_result)

# Division
div_result = np.divide(arr1, arr2)
print("Division:", div_result)

# Modulus
mod_result = np.mod(arr2, arr1)
print("Modulus:", mod_result)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Addition: [5 7 9]
Subtraction: [-3 -3 -3]
Multiplication: [ 4 10 18]
Division: [0.25 0.4  0.5 ]
Modulus: [0 1 0]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Illustration:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2eee3n6p4tu7s1jshxh9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2eee3n6p4tu7s1jshxh9.png" alt="Image description" width="800" height="1006"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Trigonometric Functions
&lt;/h3&gt;

&lt;p&gt;Trigonometry is the branch of mathematics concerned with specific functions of angles and their application to calculations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create an array of angles in radians
angles = np.array([0, np.pi/2, np.pi, 3*np.pi/2])

# Sine of angles
sin_result = np.sin(angles)
print("Sine:", sin_result)

# Cosine of angles
cos_result = np.cos(angles)
print("Cosine:", cos_result)

# Tangent of angles
tan_result = np.tan(angles)
print("Tangent:", tan_result)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sine: [ 0.  1.  0. -1.]
Cosine: [ 1.000000e+00  6.123234e-17 -1.000000e+00 -1.836970e-16]
Tangent: [ 0.0000000e+00  1.6331239e+16 -1.2246468e-16  5.4437465e+15]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Vector Operations
&lt;/h3&gt;

&lt;p&gt;NumPy also provides functions to deal with vector operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create two vectors
vec1 = np.array([1, 2, 3])
vec2 = np.array([4, 5, 6])

# Dot product
dot_product = np.dot(vec1, vec2)
print("Dot product:", dot_product)

# Cross product
cross_product = np.cross(vec1, vec2)
print("Cross product:", cross_product)

# Magnitude of a vector
magnitude = np.linalg.norm(vec1)
print("Magnitude of vec1:", magnitude)

# Angle between vectors (in radians)
cos_theta = np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))
angle = np.arccos(cos_theta)
print("Angle between vec1 and vec2 (radians):", angle)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dot product: 32
Cross product: [-3  6 -3]
Magnitude of vec1: 3.7416573867739413
Angle between vec1 and vec2 (radians): 0.2257261285527342

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Most Used Functions
&lt;/h3&gt;

&lt;p&gt;Some commonly used functions in NumPy include sum, mean, and max, which are often used in data analysis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Array of random numbers
arr = np.array([3, 1, 5, 4, 2])

# Minimum value
min_value = np.min(arr)
print("Minimum value:", min_value)

# Maximum value
max_value = np.max(arr)
print("Maximum value:", max_value)

# Sum of all elements
sum_value = np.sum(arr)
print("Sum of all elements:", sum_value)

# Mean of the array
mean_value = np.mean(arr)
print("Mean:", mean_value)

# Standard deviation
std_dev = np.std(arr)
print("Standard deviation:", std_dev)

# Sorting an array
sorted_arr = np.sort(arr)
print("Sorted array:", sorted_arr)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Minimum value: 1
Maximum value: 5
Sum of all elements: 15
Mean: 3.0
Standard deviation: 1.4142135623730951
Sorted array: [1 2 3 4 5]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;NumPy is an essential tool for any data analyst or Python programmer looking to work with large datasets efficiently. By understanding and mastering these basic and advanced NumPy functions, you’ll be equipped to handle a variety of data processing tasks. Experiment with these functions in your own projects to see the true power of NumPy in action!&lt;/p&gt;

</description>
      <category>numpy</category>
      <category>datascience</category>
      <category>phyton</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is Fine-Tuning in Machine Learning? A Beginner’s Guide</title>
      <dc:creator>Arum Puri </dc:creator>
      <pubDate>Fri, 25 Oct 2024 13:28:24 +0000</pubDate>
      <link>https://dev.to/arum_puripratamawati_ef5/what-is-fine-tuning-in-machine-learning-a-beginners-guide-n7h</link>
      <guid>https://dev.to/arum_puripratamawati_ef5/what-is-fine-tuning-in-machine-learning-a-beginners-guide-n7h</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;Introduction to Fine-Tuning&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Fine-tuning is adapting a pre-trained machine learning model to a new, specific task by retraining it on a smaller, task-related dataset. Building a machine learning model from scratch can be expensive. It requires vast amounts of data, computational power, and time. However, fine-tuning offers an efficient solution, making it possible to build a new model with limited resources. Imagine fine-tuning as tailoring a dress. The pre-trained model is like a ready-to-wear dress that fits most people well. Unfortunately, sometimes certain people need adjustments to specific areas, such as the waist or sleeves. So the dress must be fine-tuned to fit perfectly.&lt;/p&gt;

&lt;p&gt;Fine-tuning has become especially effective for domain-specific tasks. The pre-trained model already learns patterns from large datasets. One pre-trained model can be trained into several domain-specific models. For example, a Convolutional Neural Network (CNN) model can be fine-tuned to classify medical images, enabling AI systems to detect brain tumors or breast cancer earlier than the human eye. The same model can also fine-tuned to recognize individuals based on their facial images.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How does Fine-Tuning work?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The general process generally including freezing certain layers  includes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqh5k54284j0rgyk2raj2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqh5k54284j0rgyk2raj2.png" alt="Image description" width="800" height="619"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first step of fine-tuning begins by loading the pre-trained model. This pre-trained model would be the foundation of the fine-tuned model after being trained on new datasets.  Here's a step-by-step breakdown of how the process works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Loading the pre-trained model:&lt;/strong&gt; The pre-trained model is already trained with a general-purpose dataset. They learn patterns and features from the vast amount of data and are ready to fine-tune by domain or task-specified dataset to be a new model.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Freezing certain layers:&lt;/strong&gt; A pre-trained model has layers with their own weight, the result of previous training.  Freezing these layers means their parameters remain unchanged during the fine-tuning process, preserving the general knowledge the model has learned. For example,  The lower layers of the CNN model capture generic features like edges or shapes. When training brain tumor classification images, this layer definitely should be frozen.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Illustrated layers:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvkwd9y1gz5rdcgm4zfwf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvkwd9y1gz5rdcgm4zfwf.png" alt="Image description" width="564" height="514"&gt;&lt;/a&gt;&lt;br&gt;
By en:User:Cburnett - This W3C-unspecified vector image was created with Inkscape ., CC BY-SA 3.0, &lt;a href="https://commons.wikimedia.org/w/index.php?curid=1496812" rel="noopener noreferrer"&gt;https://commons.wikimedia.org/w/index.php?curid=1496812&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Updating higher layers:&lt;/strong&gt; The higher layers capture more task-specific patterns. This layer will be retrained on the new dataset.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the real world layers are more complicated:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwgt5bk2n5yhyldvxlc32.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwgt5bk2n5yhyldvxlc32.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
By Akritasa - Own work, CC BY-SA 4.0, &lt;a href="https://commons.wikimedia.org/w/index.php?curid=41652806" rel="noopener noreferrer"&gt;https://commons.wikimedia.org/w/index.php?curid=41652806&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Avoid overfitting:&lt;/strong&gt;  By freezing the lower layers and retraining only the higher layers, the model avoids overfitting to the small task-specific dataset. This allows the model to retain its ability to generalize to new data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Practical Fine-tuning&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Fine-tuning excels in task-specific models such as text summarization, code generation, classification, etc., While at domain-specific tasks, fine-tuning produces a new model that helps tremendously in their specific domain these are some examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Med-PaLM 2 is a version of the PaLM 2 model fine-tuned to answer medical questions with high accuracy
. &lt;/li&gt;
&lt;li&gt;PharmaGPT, fine-tuned from the LLaMA model, specializes in tasks related to biopharmaceuticals and chemical industries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Challenges and Best Practices in Fine-Tuning&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;While fine-tuning offers a practical way to use pre-trained models for specific tasks, it comes with its own challenges. Fine-tuning often works with small datasets so it is prone to overfitting. To overcome this ML practitioners should experiment with layers. Selecting layers to fine-tune is a tough challenge, there is no definite answer to which layer to freeze or update. Experimenting with different layers gives us an idea of which layers to freeze or update and find the right balance between generalization and specificity. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Fine-tuning is a powerful technique that enables us to create highly specialized models efficiently. Using the knowledge learned from pre-trained models and tailoring them to specific tasks, we can achieve impressive results in various domains. As machine learning evolves, fine-tuning will remain a key method for leveraging pre-trained models to solve domain-specific problems efficiently.&lt;/p&gt;

</description>
      <category>transferlearning</category>
      <category>machinelearning</category>
      <category>ai</category>
      <category>finetuning</category>
    </item>
    <item>
      <title>What is RAG in AI? How It Combines Retrieval with Generation for Accurate Results</title>
      <dc:creator>Arum Puri </dc:creator>
      <pubDate>Thu, 24 Oct 2024 06:12:28 +0000</pubDate>
      <link>https://dev.to/arum_puripratamawati_ef5/what-is-rag-in-ai-how-it-combines-retrieval-with-generation-for-accurate-results-4ne9</link>
      <guid>https://dev.to/arum_puripratamawati_ef5/what-is-rag-in-ai-how-it-combines-retrieval-with-generation-for-accurate-results-4ne9</guid>
      <description>&lt;p&gt;Imagine a world where AI-generated legal arguments are so convincing that even seasoned lawyers are fooled. This isn't science fiction; it's a reality that lawyer Steven Schwartz experienced firsthand when he unknowingly submitted six fake cases generated by ChatGPT.&lt;/p&gt;

&lt;p&gt;To be fair, ChatGPT was a new technology at that time. Even the judge was uncertain how to handle the situation, as nothing like this had happened before. In the end, Mr. Schwartz was fined $5,000 and had to issue letters of apology to the real judges whose names were falsely cited in the GPT-generated cases.&lt;br&gt;
Schwartz’s story is a stark reminder of the potential risks of using AI without fully understanding its limitations. While ChatGPT is undeniably useful—helping many professionals with day-to-day tasks—it’s not without flaws. Careless use, as we saw with Schwartz, can lead to serious consequences.&lt;/p&gt;

&lt;p&gt;The reason behind ChatGPT giving Schwartz fake cases lies in a well-known limitation of large language models (LLMs) like ChatGPT called hallucinations. This occurs when the AI generates answers that seem convincing but actually fabricated. Another flaw of LLMs like ChatGPT isn't always up to date. For example, if you asked ChatGPT 4 about recent events, such as one of the biggest pop singers' death, Liam Payne on 16 October 2024, it wouldn't know, because ChatGPT training data only goes up to December 2023.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is RAG?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;One promising solution to the problem of AI hallucinations is RAG. RAG or retrieval augmented generation is a technique to improve LLM performance. RAG uses external data and combines it with LLM for better results. This process starts by transforming external data into high-dimensional vectors, storing it in a vector database, and then retrieving the most relevant information when needed. RAG would depend mainly on two things: the retriever and the generator.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0z2ssbspzjevgizu079q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0z2ssbspzjevgizu079q.png" alt="Image description" width="652" height="576"&gt;&lt;/a&gt;&lt;br&gt;
By Turtlecrown - Own work, CC BY-SA 4.0, &lt;a href="https://commons.wikimedia.org/w/index.php?curid=150390279" rel="noopener noreferrer"&gt;https://commons.wikimedia.org/w/index.php?curid=150390279&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Retriever Component&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The retriever is responsible for fetching the most relevant information from external data. The result of this retrieval would directly affect the generator and several techniques available to make sure the most relevant data was chosen. Here are some examples of the techniques:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Self-Query Retriever&lt;/strong&gt;&lt;br&gt;
The self-query Retriever is a method where the system reformulates the original user query to better match the documents in the database. For example, the query “impact of AI on job markets,”  sounds vague on the word "impact". The phrase “impact” has many meanings, so it might automatically generate additional queries to retrieve more relevant documents. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- BM25Retriever&lt;/strong&gt;&lt;br&gt;
BM25 is a retrieval technique that uses term frequency and inverse document frequency (TF-IDF) principles. It gives score to documents based on how often query terms appear in them. Unfortunately, this retriever does not always capture the semantic meaning of queries, meaning this technique is the best only for Keyword-based search, search engines, e-commerce platforms&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- VectorStore Retriever&lt;/strong&gt;&lt;br&gt;
A VectorStore Retriever uses vector-based retrieval methods. The queries and documents are changed into high-dimensional vectors in a semantic space. The retriever fetches documents based on the closeness (cosine similarity or Euclidean distance) between the query vector and the document vectors. This approach is effective when understanding the context and meaning behind words is crucial. For example, In a recommendation system, where a user queries describing a book, the system will suggest similar books by measuring the vector similarity with a database of book embeddings and query embeddings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- EnsembleRetriever&lt;/strong&gt;&lt;br&gt;
An EnsembleRetriever combines the results from multiple retrieval methods to improve the overall quality of the retrieved documents. It uses several retrieval techniques (such as BM25, dense retrieval, and others) and merges their results. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- MultiQuery Retriever&lt;/strong&gt;&lt;br&gt;
The Multi-Query Retriever will break the query into several distinct subqueries based on its structure. This retriever breaks down the query into separate topics related to the query itself.&lt;/p&gt;

&lt;p&gt;Choosing which retriever to use depends on what kind of task this system will do in the future. Some techniques work better with structured data, while others excel with unstructured text. The list of retrievers above isn't the only retrievers available, there are still a lot of choices of retrieval out there. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Generator Component&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The generator component comes into play once the retriever selects the relevant documents. By the time this article was written, GPT-4 and Gemini Pro were still favorites of many developers. The generator combines its response with the retrieved content and mixes it into a new coherent and contextually appropriate answer.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Advantages &amp;amp; Challenges of RAG&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Using RAG minimizes the risk of incidents like Steven Schwartz’s courtroom disaster happening. By retrieving real-time and relevant information, RAG significantly improves the accuracy of the generated content, especially in fields where current knowledge is critical. The ability to use up-to-date information can open up many possibilities in various fields such as medical and scientific research.&lt;/p&gt;

&lt;p&gt;Using RAG to build chatbots is also common these days. The ability to retrieve domain-specific information from external knowledge sources makes responses more accurate and context-aware. This ability significantly improves the user experience, making interactions with chatbots more natural and satisfying.&lt;/p&gt;

&lt;p&gt;With so much abundance advantage RAG is not without challenges.&lt;br&gt;
Ensuring that the retrieved documents are highly relevant to the user query while still generating fluent and contextually correct responses is a tough challenge.&lt;/p&gt;

&lt;p&gt;The quality of the retrieved documents also matters. If the retriever pulls irrelevant or noisy data, the generator’s output will suffer. Over time the dataset also grows, and then the computational load for retrieval also gets bigger. Ensuring the system is efficient while processing large-scale information is an ongoing challenge in RAG research.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Retrieval Augmented Generation (RAG) represents a significant advancement in the field of artificial intelligence. By combining the power of large language models (LLMs) with external data, RAG addresses the limitations of traditional AI systems and offers a more reliable and informative approach. As researchers continue to refine RAG techniques and explore new applications, we can anticipate a future where AI plays an even more significant role in our lives, providing valuable assistance and insights across a wide range of domains.&lt;/p&gt;

</description>
      <category>rag</category>
      <category>genai</category>
      <category>llm</category>
      <category>ai</category>
    </item>
    <item>
      <title>Mastering Pandas in Python: A Beginner's Guide to Data Analysis</title>
      <dc:creator>Arum Puri </dc:creator>
      <pubDate>Tue, 17 Sep 2024 14:48:48 +0000</pubDate>
      <link>https://dev.to/arum_puripratamawati_ef5/mastering-pandas-in-python-a-beginners-guide-to-data-analysis-210c</link>
      <guid>https://dev.to/arum_puripratamawati_ef5/mastering-pandas-in-python-a-beginners-guide-to-data-analysis-210c</guid>
      <description>&lt;p&gt;In today’s data-driven world, the ability to efficiently clean and analyze large datasets is a key skill. This is where Pandas, one of Python’s most powerful libraries, comes into play. Whether you're handling time series data, numerical data, or categorical data, Pandas provides you with tools that make data manipulation easy and intuitive. Let's jump into Pandas and see how it can transform your approach to data analysis.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing pandas
&lt;/h3&gt;

&lt;p&gt;To start using Pandas, you’ll need to install it. Like any other Python library, Pandas can be installed via pip by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install pandas
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pandas Data Structures
&lt;/h3&gt;

&lt;p&gt;Pandas have series and dataframe for data structure. They provide a solid foundation for a wide variety of data tasks.  &lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. Series&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;From Panda's documentation, a Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pandas as pd

# Creating a Series
s = pd.Series(data, index=index)

# Creating a Series from a list
data = pd.Series([10, 20, 30, 40])

# Creating a Series from a dictionary
data_dict = pd.Series({'a': 10, 'b': 20, 'c': 30})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;2. DataFrame&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;DataFrame is a 2-dimensional labeled data structure with columns of potentially different value types (numeric, string, Boolean, etc.). You can think of it like a spreadsheet SQL table or a dict of Series objects&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pandas as pd

data = {
    'Name': ['Harry Potter', 'Hermione Granger', 'Ron Weasley', 'Draco Malfoy', 'Luna Lovegood'],
    'House': ['Gryffindor', 'Gryffindor', 'Gryffindor', 'Slytherin', 'Ravenclaw'],
    'Patronus': ['Stag', 'Otter', 'Jack Russell Terrier', 'None', 'Hare'],
    'Favorite Subject': ['Defense Against the Dark Arts', 'Arithmancy', 'Divination', 'Potions', 'Charms'],
    'Quidditch Position': ['Seeker', 'None', 'Keeper', 'None', 'None'],
    'OWL Scores': [7, 11, 7, 8, 9]
}

df = pd.DataFrame(data)
print(df)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5555lh2b3bru8dka48q3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5555lh2b3bru8dka48q3.png" alt="Image description" width="730" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Data Manipulation with Pandas
&lt;/h3&gt;

&lt;p&gt;Once you have your data in a DataFrame, Pandas provides powerful methods to explore, clean, and transform it. Let’s start with some of the most commonly used methods for exploring data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Exploring Data&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;head()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The head() method returns the headers and a specified number of rows, starting from the top. The default number of elements to display is five, but you may pass a custom number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; df.head(3)
              Name       House             Patronus                  Favorite Subject Quidditch Position  OWL Scores
0     Harry Potter  Gryffindor                  Stag   Defense Against the Dark Arts            Seeker           7
1  Hermione Granger  Gryffindor                Otter                        Arithmancy               None          11
2      Ron Weasley  Gryffindor  Jack Russell Terrier                      Divination             Keeper           7

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;tail()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tail() method returns the headers and a specified number of rows, starting from the bottom.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; df.tail(2)
              Name       House  Patronus Favorite Subject Quidditch Position  OWL Scores
3     Draco Malfoy   Slytherin      None           Potions               None           8
4    Luna Lovegood  Ravenclaw      Hare             Charms               None           9

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;info()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The DataFrames object has a method called info(), that gives you more information about the data set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; df.info()
&amp;lt;class 'pandas.core.frame.DataFrame'&amp;gt;
RangeIndex: 5 entries, 0 to 4
Data columns (total 6 columns):
 #   Column             Non-Null Count  Dtype 
---  ------             --------------  ----- 
 0   Name               5 non-null      object
 1   House              5 non-null      object
 2   Patronus           5 non-null      object
 3   Favorite Subject   5 non-null      object
 4   Quidditch Position 5 non-null      object
 5   OWL Scores         5 non-null      int64 
dtypes: int64(1), object(5)
memory usage: 368.0 bytes

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;describe()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The describe() methods give us the overall statistics of the dataset. It gives us values of min, max, mean, and standard deviation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; df.describe()
       OWL Scores
count    5.000000
mean     8.400000
std      1.673320
min      7.000000
25%      7.000000
50%      8.000000
75%      9.000000
max     11.000000

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;2.Filtering&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In data analysis, filtering helps you narrow down the data you're interested in. Pandas have several ways to filter data. The most simple and straightforward is direct Boolean indexing, especially filtering based on specific conditions (e.g., filtering based on column values). Let’s look at a few examples. In the first example, we’re selecting rows where the house value is Gryffindor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pandas as pd

data = {
    'Name': ['Harry Potter', 'Hermione Granger', 'Ron Weasley', 'Draco Malfoy', 'Luna Lovegood'],
    'House': ['Gryffindor', 'Gryffindor', 'Gryffindor', 'Slytherin', 'Ravenclaw'],
    'Patronus': ['Stag', 'Otter', 'Jack Russell Terrier', 'None', 'Hare'],
    'Favorite Subject': ['Defense Against the Dark Arts', 'Arithmancy', 'Divination', 'Potions', 'Charms'],
    'Quidditch Position': ['Seeker', 'None', 'Keeper', 'None', 'None'],
    'OWL Scores': [7, 11, 7, 8, 9]
}

df = pd.DataFrame(data)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Filter rows where the House is Gryffindor
gryffindor_students = df[df['House'] == 'Gryffindor']
print(gryffindor_students)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;               Name       House             Patronus                  Favorite Subject Quidditch Position  OWL Scores
0     Harry Potter  Gryffindor                  Stag   Defense Against the Dark Arts            Seeker           7.00
1  Hermione Granger  Gryffindor                Otter                        Arithmancy               None          11.00
2      Ron Weasley  Gryffindor  Jack Russell Terrier                      Divination             Keeper           7.00

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the second example, we’re filtering data where the OWL score (think of it as a magical equivalent to the SAT in the Harry Potter world) is greater than 8:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Filter students with OWL Scores greater than 8
high_scorers = df[df['OWL Scores'] &amp;gt; 8]
print(high_scorers)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;               Name       House Patronus Favorite Subject Quidditch Position  OWL Scores
1  Hermione Granger  Gryffindor    Otter       Arithmancy               None         11.00
4    Luna Lovegood  Ravenclaw     Hare           Charms               None         8.25

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another way to filter data is by using the &lt;strong&gt;.loc&lt;/strong&gt; method. This method allows you to filter using conditions and labels for both rows and columns. If the specified labels don’t exist, it will raise a KeyError:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use .loc[] to filter students who scored more than 8 OWLs
high_owl_scores_loc = df.loc[df['OWL Scores'] &amp;gt; 8]
print(high_owl_scores_loc)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              Name       House Patronus Favorite Subject Quidditch Position  OWL Scores
1  Hermione Granger  Gryffindor    Otter        Arithmancy               None         11
4    Luna Lovegood  Ravenclaw     Hare           Charms               None          9

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, this may look like direct Boolean indexing. Still, there’s a key difference: &lt;strong&gt;.loc&lt;/strong&gt; provides finer control, letting you select both rows and columns simultaneously, while Boolean indexing primarily filters rows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use .loc[] to filter and select specific columns
gryffindor_students = df.loc[df['House'] == 'Gryffindor', ['Name', 'OWL Scores']]
print(gryffindor_students)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            Name  OWL Scores
0   Harry Potter           7
1  Hermione Granger       11
2   Ron Weasley            7

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we have the &lt;strong&gt;.iloc&lt;/strong&gt; method. This is used for position-based filtering, meaning you select rows and columns by their index positions rather than their labels:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;third_character = df.iloc[2]
print(third_character)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name                 Ron Weasley
House                  Gryffindor
Patronus     Jack Russell Terrier
Favorite Subject          Divination
Quidditch Position            Keeper
OWL Scores                        7
Name: 2, dtype: object

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Select the 1st and last rows (indexes 0 and 4) for columns "House" and "OWL Scores"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;first_last_info = df.iloc[[0, 4], [1, 5]]
print(first_last_info)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        House  OWL Scores
0  Gryffindor           7
4  Ravenclaw            9

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;3. Sorting&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Sorting data with pandas is straightforward and can be done using the sort_values() method. For example, you can sort a list of students by their OWL scores in ascending order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Sort by 'OWL Scores' in ascending order (default)
sorted_by_owl = df.sort_values(by='OWL Scores')
print(sorted_by_owl)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              Name       House              Patronus                  Favorite Subject Quidditch Position  OWL Scores
0     Harry Potter  Gryffindor                   Stag  Defense Against the Dark Arts            Seeker            7
2      Ron Weasley  Gryffindor   Jack Russell Terrier                    Divination            Keeper            7
3      Draco Malfoy  Slytherin                  None                         Potions               None           8
4    Luna Lovegood  Ravenclaw                   Hare                           Charms               None          9
1  Hermione Granger  Gryffindor                   Otter                    Arithmancy               None         11

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To sort in descending order, set the ascending parameter to False:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Sort by 'OWL Scores' in descending order
sorted_by_owl_desc = df.sort_values(by='OWL Scores', ascending=False)
print(sorted_by_owl_desc)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              Name       House              Patronus                  Favorite Subject Quidditch Position  OWL Scores
1  Hermione Granger  Gryffindor                   Otter                    Arithmancy               None         11
4    Luna Lovegood  Ravenclaw                   Hare                           Charms               None          9
3      Draco Malfoy  Slytherin                  None                         Potions               None           8
0     Harry Potter  Gryffindor                   Stag  Defense Against the Dark Arts            Seeker            7
2      Ron Weasley  Gryffindor   Jack Russell Terrier                    Divination            Keeper            7

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One of the powerful features of sort_values() is that it allows you to sort by multiple columns. In the example below, students are sorted first by their OWL scores and then by their house:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Sort by 'OWL Scores' first in descending order, then by 'House' in ascending order
sorted_by_owl_first = df.sort_values(by=['OWL Scores', 'House'], ascending=[False, True])
print(sorted_by_owl_first)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              Name       House              Patronus                  Favorite Subject Quidditch Position  OWL Scores
1  Hermione Granger  Gryffindor                   Otter                    Arithmancy               None         11
4    Luna Lovegood  Ravenclaw                   Hare                           Charms               None          9
3      Draco Malfoy  Slytherin                  None                         Potions               None           8
0     Harry Potter  Gryffindor                   Stag  Defense Against the Dark Arts            Seeker            7
2      Ron Weasley  Gryffindor   Jack Russell Terrier                    Divination            Keeper            7

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the OWL score is the primary criterion for sorting, meaning pandas will prioritize it. If two students have the same OWL score, the house value is used as the secondary criterion for sorting&lt;/p&gt;

&lt;p&gt;Exploring, filtering, and sorting data is an essential first step before jumping into tasks like data cleaning or wrangling in the data analysis process. Pandas offers a range of built-in methods that help organize and accelerate these operations. Additionally, Pandas integrates seamlessly with other libraries, such as NumPy or SciPy for numerical computations, Matplotlib for data visualization, and analytical tools like Statsmodels and Scikit-learn. By learning Pandas, you can significantly boost your efficiency in handling and analyzing data, making it a valuable skill for any data professional. Happy coding!&lt;/p&gt;

</description>
      <category>pandas</category>
      <category>datascience</category>
      <category>machinelearning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Cook Up Your Code: JavaScript Design Patterns</title>
      <dc:creator>Arum Puri </dc:creator>
      <pubDate>Sat, 24 Aug 2024 10:55:10 +0000</pubDate>
      <link>https://dev.to/arum_puripratamawati_ef5/cook-up-your-code-javascript-design-patterns-32ln</link>
      <guid>https://dev.to/arum_puripratamawati_ef5/cook-up-your-code-javascript-design-patterns-32ln</guid>
      <description>&lt;p&gt;Picture this: you're standing in your kitchen ready to cook up a tasty meal. You've got all the ingredients laid out, but you're missing a recipe to follow. You start to experiment, but soon you feel overwhelmed. You add too much salt to one dish, burn another. Without a clear plan, cooking becomes a mess of guesswork.&lt;/p&gt;

&lt;p&gt;Creating software can feel just like this. You have all the tools and know-how, but adding new features can become a frustrating puzzle without a well-organized approach. Do you understand what your code needs to do, but are you working out the best way to make everything work together? That's where things get complicated. One tiny error, and you find yourself tumbling down a hole filled with bugs and tangled code.&lt;/p&gt;

&lt;p&gt;Enter design patterns— the time-tested recipes passed down by coders over the years. These reusable fixes help you handle the tricky parts of making software without breaking a sweat. We'll get into what exactly design patterns are, how they can make your coding life easier, and why they are the key to building robust, easy-to-maintain apps. To make things more interesting, we will be using cooking terminology throughout our explanation—because, let's be honest, who doesn't love a good cooking show?&lt;/p&gt;

&lt;p&gt;So, what is a design pattern? How are they going to help us build better apps?&lt;/p&gt;

&lt;p&gt;A design pattern is a reusable solution template that one can apply to recurring problems and themes in software design. That will be a good cookbook of tried and tested solutions from experienced developers working on common problems of software design. The guidelines say that with design patterns, we can achieve maintainable and reusable code in our apps.&lt;/p&gt;

&lt;p&gt;Design patterns are, in essence, classified according to the problem they solve into three broad categories: creational design patterns, structural design patterns, and behavioral design patterns.&lt;/p&gt;

&lt;p&gt;Design patterns are divided into three categories based on the problem they solve. They are creational design patterns, structural design patterns, and behavioral design patterns.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Creational Design Patterns: The Ingredients and Preparation&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Creational design patterns provide mechanisms for creating objects. In the context of a cooking show, these patterns are like gathering and preparing ingredients before cooking. Some patterns that fall under this category are Constructor, Factory, Abstract, Prototype, Singleton, and Builder. To gain a better understanding, look at the three examples below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Singleton&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine that there is some family's secret sauce that can only be made in some special pot, passed through generations. Of course, the sauce cannot taste the same if the pot is different. This is pretty much what Singleton does: a design pattern where a class is restricted to having a single instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SecretSauce {
  constructor() {
    if (SecretSauce.instance) {
      return SecretSauce.instance;
    }
    SecretSauce.instance = this;
    this.flavor = 'Umami';
  }

  getFlavor() {
    return this.flavor;
  }
}

const sauce1 = new SecretSauce();
const sauce2 = new SecretSauce();

console.log(sauce1.getFlavor() === sauce2.getFlavor()); // true

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Factory Method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Factory Method provides a generic interface for creating objects, allowing us to specify the kind of object we want. In our cooking show, the Recipe Book is the factory. Depending on the type of dish you want to make, it will give you the recipe—object—that you need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Product Classes
class Pizza {
    constructor(size, toppings) {
      this.size = size;
      this.toppings = toppings;
    }

    prepare() {
      console.log(`Preparing a ${this.size} pizza with ${this.toppings.join(', ')} toppings.`);
    }
  }

  class Pasta {
    constructor(sauce, noodles) {
      this.sauce = sauce;
      this.noodles = noodles;
    }

    prepare() {
      console.log(`Preparing pasta with ${this.noodles} noodles and ${this.sauce} sauce.`);
    }
  }

  // Creator Class
  class RecipeBook {
    createDish(type, options) {
      let dish;

      if (type === 'Pizza') {
        dish = new Pizza(options.size, options.toppings);
      } else if (type === 'Pasta') {
        dish = new Pasta(options.sauce, options.noodles);
      }

      return dish;
    }
  }

  // Usage
  const recipeBook = new RecipeBook();

  const pizzaOptions = {
    size: 'large',
    toppings: ['cheese', 'pepperoni', 'olives']
  };

  const pastaOptions = {
    sauce: 'alfredo',
    noodles: 'fettuccine'
  };

  const pizza = recipeBook.createDish('Pizza', pizzaOptions);
  const pasta = recipeBook.createDish('Pasta', pastaOptions);

  pizza.prepare(); // Preparing a large pizza with cheese, pepperoni, olives toppings.
  pasta.prepare(); // Preparing pasta with fettuccine noodles and alfredo sauce.


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Factory Method comes in handy in scenarios of complex objects' creation—for example, generating different instances depending on the environment or managing many similar objects.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;3. Abstract Factory *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It encapsulates the implementation details from the general usage of the Objects. The best way to explain this is if you consider a meal kit delivery service: whether you cook Italian, Chinese, or Mexican, this service will deliver everything with ingredients and recipes, only tailored to the cuisine at hand, so everything fits perfectly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Abstract Factory Interfaces
class ItalianKitchen {
    createPizza(options) {
      return new Pizza(options.size, options.toppings);
    }

    createPasta(options) {
      return new Pasta(options.sauce, options.noodles);
    }
  }

  class MexicanKitchen {
    createTaco(options) {
      return new Taco(options.shellType, options.fillings);
    }

    createBurrito(options) {
      return new Burrito(options.size, options.fillings);
    }
  }

  // Concrete Product Classes
  class Pizza {
    constructor(size, toppings) {
      this.size = size;
      this.toppings = toppings;
    }

    prepare() {
      console.log(`Preparing a ${this.size} pizza with ${this.toppings.join(', ')} toppings.`);
    }
  }

  class Pasta {
    constructor(sauce, noodles) {
      this.sauce = sauce;
      this.noodles = noodles;
    }

    prepare() {
      console.log(`Preparing pasta with ${this.noodles} noodles and ${this.sauce} sauce.`);
    }
  }

  class Taco {
    constructor(shellType, fillings) {
      this.shellType = shellType;
      this.fillings = fillings;
    }

    prepare() {
      console.log(`Preparing a taco with a ${this.shellType} shell and ${this.fillings.join(', ')} fillings.`);
    }
  }

  class Burrito {
    constructor(size, fillings) {
      this.size = size;
      this.fillings = fillings;
    }

    prepare() {
      console.log(`Preparing a ${this.size} burrito with ${this.fillings.join(', ')} fillings.`);
    }
  }

  // Client Code
  const italianKitchen = new ItalianKitchen();
  const mexicanKitchen = new MexicanKitchen();

  const italianPizza = italianKitchen.createPizza({
    size: 'medium',
    toppings: ['mozzarella', 'tomato', 'basil']
  });

  const mexicanTaco = mexicanKitchen.createTaco({
    shellType: 'hard',
    fillings: ['beef', 'lettuce', 'cheese']
  });

  italianPizza.prepare(); // Preparing a medium pizza with mozzarella, tomato, basil toppings.
  mexicanTaco.prepare(); // Preparing a taco with a hard shell and beef, lettuce, cheese fillings.


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Structural Design Patterns: The Cooking Techniques and Tools&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Structural design patterns focus on object composition, identifying straightforward ways to establish relationships between different objects. They help ensure that when one part of a system changes, the overall structure remains stable. In cooking, these patterns represent the techniques and tools we use to combine ingredients into a harmonious and delicious dish.&lt;/p&gt;

&lt;p&gt;Patterns that fall under this category include Decorator, Facade, Flyweight, Adapter, and Proxy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The Facade Pattern&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Facade pattern offers a convenient, high-level interface to a more complex body of code, effectively hiding the underlying complexity. Imagine a sous chef simplifying complex tasks for the head chef. The sous-chef gathers ingredients, preps them, and organizes everything so the head chef can focus on the final touches of the dish&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Complex Subsystem
class IngredientPrep {
    chop(ingredient) {
      console.log(`Chopping ${ingredient}.`);
    }

    measure(amount, ingredient) {
      console.log(`Measuring ${amount} of ${ingredient}.`);
    }
  }

  class CookingProcess {
    boil(waterAmount) {
      console.log(`Boiling ${waterAmount} of water.`);
    }

    bake(temp, duration) {
      console.log(`Baking at ${temp} degrees for ${duration} minutes.`);
    }
  }

  class Plating {
    arrangeDish(dish) {
      console.log(`Arranging the ${dish} on the plate.`);
    }

    garnish(garnish) {
      console.log(`Adding ${garnish} as garnish.`);
    }
  }

  // Facade Class
  class SousChef {
    constructor() {
      this.ingredientPrep = new IngredientPrep();
      this.cookingProcess = new CookingProcess();
      this.plating = new Plating();
    }

    prepareDish(dishName) {
      console.log(`Starting to prepare ${dishName}...`);
      this.ingredientPrep.chop('vegetables');
      this.ingredientPrep.measure('2 cups', 'flour');
      this.cookingProcess.boil('1 liter');
      this.cookingProcess.bake(180, 30);
      this.plating.arrangeDish(dishName);
      this.plating.garnish('parsley');
      console.log(`${dishName} is ready!`);
    }
  }

  // Client Code
  const sousChef = new SousChef();
  sousChef.prepareDish('Lasagna');
  // Output:
  // Starting to prepare Lasagna...
  // Chopping vegetables.
  // Measuring 2 cups of flour.
  // Boiling 1 liter of water.
  // Baking at 180 degrees for 30 minutes.
  // Arranging the Lasagna on the plate.
  // Adding parsley as garnish.
  // Lasagna is ready!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Decorator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Decorator pattern is used to modify existing systems by adding features to objects without significantly altering the underlying code. If our applications require many distinct types of objects, this pattern is ideal. For instance, when making coffee, we start with a basic cup and then dynamically add ingredients like milk, sugar, or whipped cream. The Decorator pattern lets us add the base coffee without changing the core recipe.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Base Component
class Coffee {
    constructor() {
      this.description = 'Basic Coffee';
    }

    getDescription() {
      return this.description;
    }

    cost() {
      return 2; // Base cost for a simple coffee
    }
  }

  // Decorator Class
  class CoffeeDecorator {
    constructor(coffee) {
      this.coffee = coffee;
    }

    getDescription() {
      return this.coffee.getDescription();
    }

    cost() {
      return this.coffee.cost();
    }
  }

  // Concrete Decorators
  class Milk extends CoffeeDecorator {
    constructor(coffee) {
      super(coffee);
    }

    getDescription() {
      return `${this.coffee.getDescription()}, Milk`;
    }

    cost() {
      return this.coffee.cost() + 0.5;
    }
  }

  class Sugar extends CoffeeDecorator {
    constructor(coffee) {
      super(coffee);
    }

    getDescription() {
      return `${this.coffee.getDescription()}, Sugar`;
    }

    cost() {
      return this.coffee.cost() + 0.2;
    }
  }

  class WhippedCream extends CoffeeDecorator {
    constructor(coffee) {
      super(coffee);
    }

    getDescription() {
      return `${this.coffee.getDescription()}, Whipped Cream`;
    }

    cost() {
      return this.coffee.cost() + 0.7;
    }
  }

  // Client Code
  let myCoffee = new Coffee();
  console.log(`${myCoffee.getDescription()} costs $${myCoffee.cost()}`); // Basic Coffee costs $2

  myCoffee = new Milk(myCoffee);
  console.log(`${myCoffee.getDescription()} costs $${myCoffee.cost()}`); // Basic Coffee, Milk costs $2.5

  myCoffee = new Sugar(myCoffee);
  console.log(`${myCoffee.getDescription()} costs $${myCoffee.cost()}`); // Basic Coffee, Milk, Sugar costs $2.7

  myCoffee = new WhippedCream(myCoffee);
  console.log(`${myCoffee.getDescription()} costs $${myCoffee.cost()}`); // Basic Coffee, Milk, Sugar, Whipped Cream costs $3.4

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Flyweight&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Flyweight pattern is a classical structural solution for optimizing code that is repetitive, slow, and inefficiently shares data. It aims to minimize memory in use in an application by sharing as much data as possible with related objects. Think of common ingredients like salt, pepper, and olive oil that are used in many dishes. Instead of having separate instances of these ingredients for each dish, they are shared across dishes to save resources. For example, you put salt on fried chicken and beef stew from the same jar.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Flyweight Class
class Ingredient {
    constructor(name) {
      this.name = name;
    }

    use() {
      console.log(`Using ${this.name}.`);
    }
  }

  // Flyweight Factory
  class IngredientFactory {
    constructor() {
      this.ingredients = {};
    }

    getIngredient(name) {
      if (!this.ingredients[name]) {
        this.ingredients[name] = new Ingredient(name);
      }
      return this.ingredients[name];
    }

    getTotalIngredientsMade() {
      return Object.keys(this.ingredients).length;
    }
  }

  // Client Code
  const ingredientFactory = new IngredientFactory();

  const salt1 = ingredientFactory.getIngredient('Salt');
  const salt2 = ingredientFactory.getIngredient('Salt');
  const pepper = ingredientFactory.getIngredient('Pepper');

  salt1.use(); // Using Salt.
  salt2.use(); // Using Salt.
  pepper.use(); // Using Pepper.

  console.log(ingredientFactory.getTotalIngredientsMade()); // 2, Salt and Pepper were created only once
  console.log(salt1 === salt2); // true, Salt is reused


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Behavioral Design Patterns: The Cooking Process and Interaction&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Behavioral patterns focus on improving or streamlining the communication between disparate objects in a system. They identify common communication patterns among objects and provide solutions that distribute the communication responsibility among different objects, thereby increasing communication flexibility. In a cooking show, behavioral design patterns are the way we cook the dish, the process of cooking, and how various parts of the kitchen interact with each other to create the final dish. Some of the behavioral patterns are Iterator, Mediator, Observer, and Visitor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Observer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Observer pattern is used to notify components of state changes. When a subject needs to inform observers about a change, it broadcasts a notification. If an observer no longer wishes to receive updates, they can be removed from the list of observers. For example, once the head chef finishes preparing a dish, all the assistant chefs need to be notified to begin their tasks, such as plating or garnishing. The Observer pattern allows multiple chefs (observers) to be notified when the head chef (subject) completes a dish.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Subject Class
class HeadChef {
  constructor() {
    this.chefs = [];
    this.dishReady = false;
  }

  addObserver(chef) {
    this.chefs.push(chef);
  }

  removeObserver(chef) {
    this.chefs = this.chefs.filter(c =&amp;gt; c !== chef);
  }

  notifyObservers() {
    if (this.dishReady) {
      this.chefs.forEach(chef =&amp;gt; chef.update(this.dishName));
    }
  }

  prepareDish(dishName) {
    this.dishName = dishName;
    console.log(`HeadChef: Preparing ${dishName}...`);
    this.dishReady = true;
    this.notifyObservers();
  }
}

// Observer Class
class Chef {
  constructor(name) {
    this.name = name;
  }

  update(dishName) {
    console.log(`${this.name}: Received notification - ${dishName} is ready!`);
  }
}

// Client Code
const headChef = new HeadChef();

const chef1 = new Chef('Chef A');
const chef2 = new Chef('Chef B');

headChef.addObserver(chef1);
headChef.addObserver(chef2);

headChef.prepareDish('Beef Wellington');
// Output:
// HeadChef: Preparing Beef Wellington...
// Chef A: Received notification - Beef Wellington is ready!
// Chef B: Received notification - Beef Wellington is ready!

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Mediator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Mediator pattern allows one object to be in charge of the communication between several other objects when an event occurs. While it does sound similar to the Observer pattern, the key difference is that the Mediator handles communication between objects rather than just broadcasting changes. For example, let's think of our kitchen with its grill, bakery, and garnish station sections. A kitchen coordinator (mediator) handles the communication so that all the preparations are done on time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Mediator Class
class KitchenCoordinator {
  notify(sender, event) {
    if (event === 'dishPrepared') {
      console.log(`Coordinator: Notifying all stations that ${sender.dishName} is ready.`);
    } else if (event === 'orderReceived') {
      console.log(`Coordinator: Received order for ${sender.dishName}, notifying preparation stations.`);
    }
  }
}

// Colleague Classes
class GrillStation {
  constructor(coordinator) {
    this.coordinator = coordinator;
  }

  prepareDish(dishName) {
    this.dishName = dishName;
    console.log(`GrillStation: Grilling ${dishName}.`);
    this.coordinator.notify(this, 'dishPrepared');
  }
}

class BakeryStation {
  constructor(coordinator) {
    this.coordinator = coordinator;
  }

  bakeDish(dishName) {
    this.dishName = dishName;
    console.log(`BakeryStation: Baking ${dishName}.`);
    this.coordinator.notify(this, 'dishPrepared');
  }
}

// Client Code
const coordinator = new KitchenCoordinator();
const grillStation = new GrillStation(coordinator);
const bakeryStation = new BakeryStation(coordinator);

grillStation.prepareDish('Steak');
// Output:
// GrillStation: Grilling Steak.
// Coordinator: Notifying all stations that Steak is ready.

bakeryStation.bakeDish('Bread');
// Output:
// BakeryStation: Baking Bread.
// Coordinator: Notifying all stations that Bread is ready.


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Command design pattern is an Object Behavioral Pattern that encapsulates the invocation of methods, requests, or operations into a single object and allows both parameterization and pass method calls that can be executed at our discretion. For example, look at how the head chef gives the command below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Command Interface
class Command {
  execute() {}
}

// Concrete Commands
class GrillCommand extends Command {
  constructor(grillStation, dishName) {
    super();
    this.grillStation = grillStation;
    this.dishName = dishName;
  }

  execute() {
    this.grillStation.grill(this.dishName);
  }
}

class BakeCommand extends Command {
  constructor(bakeryStation, dishName) {
    super();
    this.bakeryStation = bakeryStation;
    this.dishName = dishName;
  }

  execute() {
    this.bakeryStation.bake(this.dishName);
  }
}

// Receiver Classes
class GrillStation {
  grill(dishName) {
    console.log(`GrillStation: Grilling ${dishName}.`);
  }
}

class BakeryStation {
  bake(dishName) {
    console.log(`BakeryStation: Baking ${dishName}.`);
  }
}

// Invoker Class
class HeadChef {
  setCommand(command) {
    this.command = command;
  }

  executeCommand() {
    this.command.execute();
  }
}

// Client Code
const grillStation = new GrillStation();
const bakeryStation = new BakeryStation();

const grillCommand = new GrillCommand(grillStation, 'Steak');
const bakeCommand = new BakeCommand(bakeryStation, 'Bread');

const headChef = new HeadChef();

headChef.setCommand(grillCommand);
headChef.executeCommand(); // GrillStation: Grilling Steak.

headChef.setCommand(bakeCommand);
headChef.executeCommand(); // BakeryStation: Baking Bread.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Behavioral patterns can feel similar, so let's highlight their differences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Observer: When a head chef prepares a dish, several other chefs are informed about it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mediator: A coordinator works in the kitchen, facilitating communication between various stations in the kitchen.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Command: The head chef issues commands to grill or bake dishes, encapsulating these actions as objects.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Design patterns give a clear way to fix common issues in software development much like a tidy kitchen and smart cooking methods lead to a good meal. When you get these patterns and put them to use, you make your coding easier and help your apps work better and grow more. It doesn't matter if you're new to coding or have done it for a long time - think of design patterns as trusted recipes passed down by many coders over the years. Try them out, play around with them, and soon you'll find that making strong apps becomes as natural as following a recipe you love. Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
