Usage of NumPy for Numerical Data in Detail
Usage of NumPy for Numerical Data in Detail
Mathematical
Extensive mathematical functions Limited built-in functions
Operations
2. NumPy Basics
Before diving into advanced numerical operations, let's start with the basics.
import numpy as np
# 1D array
# 3D array
print(arr1)
print(arr2)
print(arr3)
b = np.array([1, 2, 3, 4])
print(a - b) # [ 9 18 27 36]
print(a * b) # [ 10 40 90 160]
print(a / b) # [10. 10. 10. 10.]
NumPy is widely used in Data Science, Machine Learning, and Scientific Computing.
import numpy as np
# Compute statistics
7. Broadcasting in NumPy
scalar = 10
8.1 Vectorization
NumPy performs operations without explicit loops, making it much faster than Python
lists.
import sys
list_data = list(range(1000))
numpy_data = np.array(list_data)
9. Applications of NumPy
• Data Science & Machine Learning – Preprocessing data before feeding into
models.
• Computer Vision & AI – Working with image and video data (e.g., OpenCV).
Conclusion
1. Data Structures
2. Common Operations
a. Importing Data
Pandas can read various file formats, such as CSV, Excel, SQL, and JSON.
import pandas as pd
df = pd.read_csv("data.csv")
df_excel = pd.read_excel("data.xlsx")
Once the data is loaded into a DataFrame, you can perform a series of operations to
explore it.
df.head()
# Checking basic info about the DataFrame
df.info()
df.describe()
c. Accessing Data
• column_data = df["column_name"]
• Row Access: Access rows by index using .iloc[] for integer-based indexing or .loc[]
for label-based indexing.
d. Filtering Data
e. Sorting Data
df.isna().sum()
# Dropping rows with any missing data
df_clean = df.dropna()
df_filled = df.fillna(value=0)
g. Grouping Data
The groupby() function allows you to group data based on one or more columns and
then apply aggregation functions like mean, sum, count, etc.
grouped = df.groupby("department")["salary"].mean()
h. Merging Data
Pandas supports merging datasets based on common columns (similar to SQL JOINs).
i. Applying Functions
You can apply functions across DataFrame rows or columns using apply(), map(), and
applymap().
df = df.applymap(str)
j. Pivot Tables
Pandas makes it easy to create pivot tables, which summarize data in a tabular form.
pivot = df.pivot_table(values="salary", index="department", aggfunc="mean")
3. Visualization Integration
df.groupby("department")["salary"].mean().plot(kind="bar")
plt.show()
4. Advanced Operations
• Time Series Analysis: Using pd.to_datetime() to work with date-time data and
perform time-based indexing, resampling, and rolling window operations.
• MultiIndexing: For handling hierarchical indexing and working with more complex
datasets.
5. Saving Data
You can save the manipulated data back to various file formats:
df.to_csv("output.csv", index=False)
df.to_excel("output.xlsx", index=False)
import pandas as pd
# Sample data
data = {
# Creating a DataFrame
df = pd.DataFrame(data)
print(df.info())
print(df.describe())
print(filtered_df)
grouped_df = df.groupby("Department")["Salary"].mean()
print(grouped_df)
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Age Salary
3 David 40 80000 HR
Department
Finance 90000
HR 65000
Tech 75000
Conclusion
Pandas is an incredibly powerful tool for data analysis and manipulation. From reading
and cleaning data to performing advanced operations like grouping and pivoting, it
covers almost all aspects of data processing that are common in data science.
A statistical plot is a type of graph that is used to visualize and understand the
distribution, relationships, and patterns within data. These plots are particularly useful
for analyzing statistical data because they help to make sense of the data by presenting
it visually, which can reveal important insights, trends, and anomalies.
4. Box Plot:
o Purpose: Displays the spread and central tendency of the data, and also
highlights outliers (data points that are far away from the rest).
o Example: A box plot could show the distribution of income across different
cities, with the "box" showing where most incomes lie and "whiskers"
showing the range.
6. Scatter Plot:
8. Line Plot:
o Example: A line plot can show how the temperature changes over a week.
9. sns.lineplot(x='date', y='temperature', data=df) # Shows temperature change
over time
10. Heatmap:
o Example: A heatmap can show how different features, like age, income,
and education level, are correlated in a dataset.
• Understanding Data: Helps to identify trends, patterns, and outliers in the data.
Summary:
A statistical plot is a visual tool that represents data in a way that helps you understand
its key features, such as distribution, relationships, and trends. Examples include
histograms, box plots, scatter plots, and heatmaps, which are often used to explore and
analyze datasets in fields like business, science, and engineering. By using statistical
plots, you can easily gain insights that might be harder to spot in raw data alone.
Seaborn is a Python library that helps you create beautiful and informative statistical
plots. It's built on top of another library called Matplotlib but makes it easier to
generate complex plots without writing too much code.
Here’s a simple overview of Seaborn and its use for statistical plots:
2. Built for Statistics: Seaborn is designed for statistical data. It allows you to make
plots that show the relationships between different variables, distributions of
data, and trends.
3. Attractive Plots: Seaborn comes with pre-designed themes that make your plots
look clean and professional, with just a few lines of code.
1. Distribution Plots:
3. Box Plots:
o Box plots help you understand the spread of data and highlight outliers
(unusual values).
o Example: A box plot to compare bills across different days of the week.
5. Scatter Plots:
o Example: A scatter plot to show the relationship between the bill amount
and the tip amount.
7. Line Plots:
9. Pair Plots:
o Example: A pair plot can show how different features like total bill, tip, and
size are related.
11. Heatmaps:
o Example: A heatmap can show the correlation between features like total
bill, tip, and size.
• Integration with Pandas: Seaborn works well with Pandas DataFrames (the
format in which most data is stored). This means you can directly use your
DataFrame without having to modify the data too much.
• Less Code, Better Plots: You can make complex plots with just a few lines of code.
Seaborn handles many details automatically, like colors, labels, and axes.
Let’s say you have a dataset with information about restaurant bills. You can use Seaborn
to easily visualize this data.
df = sns.load_dataset("tips")
This will generate a box plot that compares how total bills vary across different days of
the week, automatically taking care of things like labels and color.
Conclusion
Seaborn is an excellent tool for anyone who wants to create statistical plots easily.
Whether you’re analyzing the distribution of your data, finding relationships between
variables, or comparing categories, Seaborn makes the job simple, fast, and visually
appealing. It's a great choice for beginners and professionals alike who want to visualize
data in a meaningful way without much hassle.
Matplotlib is a powerful and widely used Python library for creating static, animated,
and interactive visualizations. It is highly customizable and flexible, making it a go-to tool
for generating a wide range of plots, such as line plots, bar charts, histograms, scatter
plots, pie charts, and more.
Matplotlib is built on top of NumPy and integrates well with other scientific computing
libraries like Pandas, which makes it ideal for data visualization in data science and
analysis.
Installing Matplotlib
Before using Matplotlib, you'll need to install it. You can install it using pip:
2. Add axes: The area where the plot elements (lines, bars, etc.) will be drawn.
3. Plot the data: Using various functions to add lines, bars, or other plot types to the
axes.
1. Line Plot
A line plot is one of the most common types of plots. It is used to visualize continuous
data points connected by a line.
# Example data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
2. Bar Plot
A bar plot is used to display and compare the quantity of different categories.
# Example data
values = [3, 7, 2, 5]
plt.bar(categories, values)
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()
3. Histogram
import numpy as np
data = np.random.randn(1000)
# Create a histogram
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.title("Histogram")
plt.show()
4. Scatter Plot
# Example data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.scatter(x, y)
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.show()
5. Pie Chart
# Example data
# Add a title
plt.show()
Customizing Plots
Matplotlib provides a range of options to customize your plots, including adding grid
lines, changing line colors, adding legends, and more.
plt.plot(x, y)
plt.show()
plt.show()
3. Adding Legends
plt.show()
4. Subplots
plt.plot(x, y)
plt.title("Plot 1")
plt.bar(categories, values)
plt.title("Plot 2")
plt.show()
Saving a Plot
You can save a plot to a file (e.g., PNG, PDF) using savefig().
plt.plot(x, y)
plt.title("Saved Plot")
Here’s an example that demonstrates multiple plot types and customizations in one
script:
import numpy as np
# Example data
y = np.sin(x)
fig, ax = plt.subplots()
# Adding grid
ax.grid(True)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
# Displaying a legend
ax.legend()
plt.show()
Conclusion
Matplotlib is a highly versatile and essential tool for creating static plots in Python. It is
widely used in data analysis, machine learning, and scientific research for visualizing
data. Whether you're plotting simple line plots, bar charts, or more complex
visualizations like subplots and multiple axis plots, Matplotlib offers extensive
functionality for creating publication-quality visualizations.
No worries! I'll walk you through creating a GUI (Graphical User Interface) step by step,
starting from the very basics.
We're going to use Tkinter, which is the simplest way to build a GUI in Python. Tkinter is
already built into Python, so you don’t need to install anything extra.
What is a GUI?
A GUI (Graphical User Interface) allows users to interact with the software through
graphical icons, buttons, and other visual elements, rather than text-based commands.
Goal
3. When the button is clicked, the label will change its text.
Step-by-Step Explanation:
1. Import Tkinter
To start building the GUI, we first need to import the Tkinter library.
import tkinter as tk
Next, we need to create a window where all our widgets (like buttons, labels) will
appear.
root = tk.Tk()
• root is the main window where all the graphical elements will be placed.
button.pack()
• Button() creates a button, and we set the text on the button to "Click Me".
Next, let's add a label (a place where we can display text). Initially, it will show a simple
message.
label.pack()
• Label() creates a label widget, and we set the initial text to "Hello, World!".
Now, we want to handle an event. The event is when the user clicks the button. When
the button is clicked, we will change the text on the label.
To do this, we will define a function that gets triggered when the button is clicked. This
function will update the label's text.
def on_button_click():
Now, we need to connect the button to the event handler (the function
on_button_click). This is done using the command parameter of the Button widget.
button.config(command=on_button_click)
Finally, we start the event loop. This loop keeps the window open and listens for events,
like button clicks.
• root.mainloop() starts the Tkinter event loop and makes the window interactive.
Complete Code
import tkinter as tk
def on_button_click():
root = tk.Tk()
label.pack()
button.pack()
root.mainloop()
How It Works:
1. Start the Program: When you run the program, a window appears.
Next Steps:
• Using other Tkinter widgets like Entry (for text input), Checkbutton (for
checkboxes), and more.
This is the simplest way to get started with creating a GUI in Python!
Creating a simple GUI (Graphical User Interface) that handles an event can be done
easily using Tkinter, which is a standard Python library for building graphical user
interfaces. In this example, we will create a simple GUI with a button that, when clicked,
will display a message.
Step-by-Step Example:
4. Event Handling: When the button is clicked, an event is triggered (i.e., the button
click).
5. Display a Message: After the button is clicked, a message will be displayed in a
label.
Example Code:
import tkinter as tk
def on_button_click():
root = tk.Tk()
label.pack(pady=20)
button.pack()
root.mainloop()
6. root.mainloop(): This starts the GUI application and keeps the window open,
listening for events (like button clicks).
• Initially, the window shows a label with the text "Click the button to see a
message".
• When you click the "Click Me" button, the on_button_click function is triggered,
and the label's text changes to "Button was clicked!".
This is a basic and easy example of a GUI application that handles an event (a button
click). You can extend this by adding more widgets like entry fields, checkboxes, and
more.
SQLite Manager is a tool that allows you to interact with SQLite databases. It provides
an easy-to-use graphical interface for performing common database operations like
creating tables, inserting data, and running SQL queries. It's particularly useful for
developers who want to quickly manage an SQLite database without having to write a
lot of code.
In this guide, I'll show you how to use SQLite Manager (in a browser-based tool like DB
Browser for SQLite) to work with an SQLite database, including basic steps like creating a
database, adding tables, and inserting data.
Prerequisites:
2. SQLite Manager (DB Browser for SQLite): You can download and install DB
Browser for SQLite from here, or you can use browser-based tools like SQLite
Online.
• Go to the DB Browser for SQLite website and download the tool for your
operating system.
1. Launch the program and click on the "New Database" option in the toolbar.
2. Choose a location to save the new database file (e.g., my_database.db) and give
it a name.
o After selecting a location and filename, you’ll see an option to create the
database.
o Click "Save".
2. Click on the "Database Structure" tab. This tab shows all the tables and objects in
your database.
o Columns: Add columns for your table. For example, add columns like id,
name, age.
Example table:
o name (TEXT)
o age (INTEGER)
Example schema:
1. To insert data into the table, click on the "Browse Data" tab.
2. Select the table you want to insert data into from the dropdown menu (e.g.,
students).
4. A new row will appear, and you can start entering data:
▪ id = 1
▪ age = 20
5. After entering the data, click the "Apply Changes" button to save the new record.
2. In the SQL text editor, type the query you want to execute. For example, to
retrieve all data from the students table, you can write:
4. Click "Execute SQL" to run the query and view the results in the lower section.
Example queries:
o Delete a record:
1. Once you're done working with the database, make sure to save your changes.
2. Click "File" > "Save Database" to save the changes made to the database.
• Create a Database: File → New Database → Choose location and name → Save.
• Create a Table: Database Structure tab → Create Table → Define columns → OK.
• Execute SQL Queries: Execute SQL tab → Write SQL query → Execute SQL.
Here are a few examples of SQL queries you can run in DB Browser for SQLite:
1. Create a table:
4. name TEXT,
5. age INTEGER
6. );
7. Insert data:
Conclusion:
SQLite Manager (or DB Browser for SQLite) is a great tool for managing SQLite
databases with a user-friendly interface. You can:
Here’s a simple guide to working with a database using Python's sqlite3 library.
Python comes with the sqlite3 library built-in, so there's no need to install anything
extra.
2. Creating a Database Connection
To work with a database, first, you need to create a connection to the database file. If
the database file doesn’t exist, SQLite will create it for you.
import sqlite3
# Create a connection to the SQLite database (if the database does not exist, it will be
created)
connection = sqlite3.connect("my_database.db")
cursor = connection.cursor()
3. Creating a Table
Once we have a connection to the database, we can create tables in the database. A
table is where the data will be stored.
# Create a table
name TEXT,
age INTEGER)''')
connection.commit()
• The CREATE TABLE IF NOT EXISTS statement ensures that the table is only created
if it doesn't already exist.
• We are creating a table students with columns: id, name, and age.
To insert data into the table, you can use the INSERT INTO SQL statement.
connection.commit()
• This statement adds a new student with the name John Doe and age 20 to the
students table.
Once we have data in the table, we can query it using the SELECT statement.
students = cursor.fetchall()
print(student)
• cursor.execute("SELECT * FROM students") runs the SQL query to select all rows
from the students table.
6. Updating Data
To update data, we use the UPDATE SQL statement. Here's how to update a student's
age:
connection.commit()
• This will update the record where the name is John Doe and set their age to 21.
7. Deleting Data
connection.commit()
• This will delete the student with the name John Doe from the students table.
After finishing operations with the database, it’s important to close the connection.
connection.close()
Complete Example: Creating, Inserting, Querying, Updating, and Deleting Data
Here’s a complete example that demonstrates the full flow of working with a database in
Python:
import sqlite3
# Connect to the database (it will create the database file if it doesn't exist)
connection = sqlite3.connect("my_database.db")
cursor = connection.cursor()
# Create a table
name TEXT,
age INTEGER)''')
connection.commit()
students = cursor.fetchall()
# Print all students
print("All Students:")
print(student)
connection.commit()
students = cursor.fetchall()
print("\nAfter Update:")
print(student)
connection.commit()
students = cursor.fetchall()
print("\nAfter Deletion:")
print(student)
connection.close()
Output:
All Students:
After Update:
After Deletion:
• Closing the Connection: After all operations, we closed the database connection.
Key Concepts:
• Cursor: Used to interact with the database and execute SQL queries.
• CRUD Operations:
Conclusion
Using Python and SQLite3, you can easily perform all basic operations like creating
tables, inserting, updating, deleting, and querying data. This is a great way to store and
manipulate small to medium-sized datasets in a lightweight database without needing to
install a full database server.
Interactive dynamic visualization refers to the use of
visual elements, like graphs or charts, that allow
users to engage with the data and see it change or
update in real time. It's like making data come to life,
so instead of just looking at a static image, you can
interact with it—zoom in, filter, or change aspects of
the data to explore different views or patterns.
In simple terms:
• Interactive: You can click, drag, or hover to
change the data you're seeing.
• Dynamic: The data can update or move over time
(like showing changes over days, months, or
years).
• Visualization: It’s a graphical representation of
data, like charts, maps, or animations.
Examples include:
• A map that lets you click to see weather changes
over the next few hours.
• A chart that updates when you select different
options (like filtering by year or category).
It’s useful because it makes complex data easier to
understand and more engaging for the user.
1. Matplotlib क्या है ?
स प
िं ल और फ्लेक्क् बल – Basic से िेकर advanced visualization बना
सकते हैं।
Object-oriented API – अधधक customization के लिए।
Pyplot Interface – आसान plotting के लिए matplotlib.pyplot का
उपयोग ककया िाता है ।
अन्य लाइब्रेरी के ाथ compatibility – NumPy, Pandas, और SciPy के
साथ आसानी से काम करता है ।
# डेटा
x = [1, 2, 3, 4, 5]
y = [10, 15, 7, 12, 5]
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Simple Line Plot")
plt.legend()
# ग्राफ ददखाएं
plt.show()
Explanation:
# डेटा
plt.xlabel("Categories")
plt.ylabel("Values")
plt.title("Bar Chart Example")
plt.show()
# डेटा
x = np.random.rand(50)
y = np.random.rand(50)
plt.scatter(x, y, color='red')
plt.xlabel("X values")
plt.ylabel("Y values")
plt.title("Scatter Plot Example")
plt.show()
data = np.random.randn(1000)
# Histogram बनाएं
plt.hist(data, bins=30, color='blue', edgecolor='black')
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.title("Histogram Example")
plt.show()
x = [1, 2, 3, 4, 5]
y = [10, 15, 7, 12, 5]
plt.show()
Customization Details:
अधधक manual
Customization सुंदर default styles
customization
Seaborn का एक उदाहरण:
8. ननष्कषट (Conclusion)