Unit 5 Python Notes Hm
Unit 5 Python Notes Hm
Detailed notes for Unit 5: Advanced Topics and Applications for the subject Computational
Thinking with Python or similar Data Science-oriented Python courses. The unit is broken
into three sections (A, B, C) and includes conceptual explanations, Python code examples,
and key takeaways.
Key Features:
ndarray (n-dimensional array) object
Broadcasting functions
Vectorized operations (fast and efficient)
Mathematical and logical operations
Integration with C/C++ and Fortran code
Basic NumPy Operations:
import numpy as np
# Creating arrays
a = np.array([1, 2, 3])
b = np.array([[1, 2], [3, 4]])
# Array properties
print(a.shape) # Output: (3,)
print(b.ndim) # Output: 2
# Basic operations
print(a + 10) # Output: [11 12 13]
print(b * 2) # Output: [[2 4], [6 8]]
# Mathematical functions
print(np.mean(a)) # Output: 2.0
print(np.sqrt(b)) # Square root of each element
# Creating a Series
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
# Creating a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]}
df = pd.DataFrame(data)
print(s)
print(df)
B. Data Manipulation and Analysis with Pandas
1. Reading and Writing Data:
# Reading data from CSV
df = pd.read_csv('data.csv')
# Modify column
df['Age'] = df['Age'] + 1
6. Handling Missing Data:
# Check missing data
print(df.isnull().sum())
plt.plot(x, y)
plt.title('Line Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()
3. Bar Chart:
categories = ['A', 'B', 'C']
values = [5, 7, 3]
plt.bar(categories, values)
plt.title('Bar Chart')
plt.show()
4. Histogram:
data = [1, 2, 2, 3, 3, 3, 4, 4, 5]
plt.hist(data, bins=5)
plt.title('Histogram')
plt.show()
5. Pie Chart:
labels = ['Python', 'Java', 'C++']
sizes = [40, 30, 30]
plt.scatter(x, y)
plt.title('Scatter Plot')
plt.show()
Matplotlib is a Python library used for creating static, animated, and interactive
visualizations. It is a versatile tool that enables users to generate a wide array of plots,
charts, and figures, making it a fundamental asset in data analysis, scientific research, and
various other fields.
Matplotlib's capabilities include:
Diverse Plotting Options:
It supports various plot types, such as line plots, scatter plots, bar charts, histograms, pie
charts, and 3D plots.
Customization:
Matplotlib allows for extensive customization of plots, including adjustments to colors,
markers, labels, titles, and axes.
Integration:
It can be integrated with other Python libraries like NumPy and Pandas, enhancing its
functionality and making it suitable for complex data manipulation and visualization tasks.
Output Formats:
Matplotlib can produce high-quality figures in various formats, including PNG, JPG, PDF, and
SVG.
Mathematical Expressions:
It supports mathematical expressions in text using a built-in TeX expression parser.
Python
import matplotlib.pyplot as plt
import numpy as np
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Sine Wave')
plt.grid(True)
plt.show()
Matplotlib: Visualization with Python
Matplotlib is a comprehensive library for creating static, animated, and interactive
visualizations in Python. Matplotlib makes easy things easy and hard things possible.
Create publication quality plots.
Make interactive figures that can zoom, pan, update.
Customize visual style and layout.
Export to many file formats.
Embed in JupyterLab and Graphical User Interfaces.
Use a rich array of third-party packages built on Matplotlib.
Sure! Below are detailed theoretical notes on Matplotlib: Visualization with Python, first in
English, then in Hindi.
🔷 Advanced Customization
Subplots using plt.subplot(), plt.subplots()
Legends with plt.legend()
Gridlines with plt.grid(True)
Logarithmic scales using plt.xscale('log')
Adding annotations with plt.annotate()
🔷 Embedding in Applications
JupyterLab: %matplotlib inline or %matplotlib notebook
GUI Integration: With Tkinter, PyQt5, wxPython
🔷 Conclusion
Matplotlib provides a powerful and flexible framework for creating a wide variety of plots in
Python. Its deep integration with Python's data ecosystem makes it a go-to library for data
scientists and analysts.
📙 मैटप्लॉटलिब: पाइथन के साथ डेटा विज़ुअलाइज़ेशन – विस्तृत थ्योरी
नोट्स (Hindi)
🔷 मैटप्लॉटलिब का परिचय
Matplotlib पाइथन की एक प्रमुख डेटा विज़ुअलाइज़ेशन लाइब्रेरी है। इसका
उपयोग स्थैतिक (static), एनिमेटेड (animated), और इंटरैक्टिव (interactive)
ग्राफ़ बनाने के लिए किया जाता है। इसका मुख्य मॉड्यूल pyplot है, जो
MATLAB जैसी सिंटैक्स प्रदान करता है।
# प्लॉट बनाएं
plt.plot(x, y)
# प्लॉट दिखाएं
plt.show()
🔷 निष्कर्ष
Matplotlib एक शक्तिशाली और लचीली डेटा विज़ुअलाइज़ेशन लाइब्रेरी है जो
वैज्ञानिक विश्लेषण और प्रस्तुतिकरण के लिए अत्यंत उपयोगी है। इसकी
विशेषताएं इसे डेटा वैज्ञानिकों और विश्लेषकों के लिए एक अनिवार्य
उपकरण बनाती हैं।
✅ Key Takeaways:
NumPy is essential for numerical and matrix computations.
Pandas simplifies data loading, cleaning, transformation, and analysis.
Matplotlib helps create insightful visualizations like bar charts, histograms, and
scatter plots.
These libraries form the foundation of Data Science and are often used with scikit-
learn, seaborn, and TensorFlow in advanced applications.
Introduction to Libraries in Python: NumPy and Pandas
✅ What is NumPy?
NumPy is a Python library used for numerical computations.
It introduces the powerful n-dimensional array object called ndarray.
Provides tools to perform mathematical, statistical, and algebraic operations
efficiently.
NumPy Introduction
What is NumPy?
NumPy is a Python library used for working with arrays.
Arrays are very frequently used in data science, where speed and resources
are very important.
This is the main reason why NumPy is faster than lists. Also it is optimized to
work with latest CPU architectures.
Exercise?
What does NumPy stand for?
Number Picker
Numerical Platform
Numerical Python
Submit Answer »
Installation of NumPy
If you have Python and PIP already installed on a system, then installation of NumPy is very
easy.
Install it using this command:
C:\Users\Your Name>pip install numpy
If this command fails, then use a python distribution that already has NumPy installed like,
Anaconda, Spyder etc.
Import NumPy
Once NumPy is installed, import it in your applications by adding the import keyword:
import numpy
Now NumPy is imported and ready to use.
ExampleGet your own Python Server
import numpy
print(arr)
Try it Yourself »
NumPy as np
NumPy is usually imported under the np alias.
alias: In Python alias are an alternate name for referring to the same thing.
Create an alias with the as keyword while importing:
import numpy as np
Now the NumPy package can be referred to as np instead of numpy.
Example
import numpy as np
print(arr)
Try it Yourself »
print(np.__version__)
✅ What is Pandas?
Pandas is a powerful Python library for data manipulation and analysis. Pandas is a
Python library used for data manipulation and analysis. It provides data structures
like DataFrames for efficiently working with tabular data, enabling operations such
as loading, cleaning, transforming, and merging datasets. Pandas is widely used in
data science for its versatility and performance, integrating well with other Python
libraries like NumPy and Matplotlib. Pandas is used to analyze data.
It provides two primary data structures:
o Series: 1-dimensional labeled array
o DataFrame: 2-dimensional table (like an Excel sheet or SQL table)
//code for pandas //
import pandas as pd
# Sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 28],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
# Display DataFrame
print(df)
Search field
Log inSign Up Get Certified For Teachers Spaces Plus
❮
❯
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C+
+ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA
TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN
AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST
Pandas Tutorial
Pandas HOMEPandas IntroPandas Getting StartedPandas SeriesPandas DataFramesPandas
Read CSVPandas Read JSONPandas Analyzing Data
Cleaning Data
Cleaning DataCleaning Empty CellsCleaning Wrong FormatCleaning Wrong DataRemoving
Duplicates
Correlations
Pandas Correlations
Plotting
Pandas Plotting
Quiz/Exercises
Pandas EditorPandas QuizPandas ExercisesPandas SyllabusPandas Study PlanPandas
Certificate
References
DataFrames Reference
Pandas Introduction
❮ PreviousNext ❯
What is Pandas?
Pandas is a Python library used for working with data sets.
It has functions for analyzing, cleaning, exploring, and manipulating data.
The name "Pandas" has a reference to both "Panel Data", and "Python Data Analysis" and
was created by Wes McKinney in 2008.
Exercise?
The name 'Pandas' is short for one of the following:
Panel Data
Python Dates
Python Algorithms
Submit Answer »
Pandas Tutorial
Pandas HOMEPandas IntroPandas Getting StartedPandas SeriesPandas DataFramesPandas
Read CSVPandas Read JSONPandas Analyzing Data
Cleaning Data
Cleaning DataCleaning Empty CellsCleaning Wrong FormatCleaning Wrong DataRemoving
Duplicates
Correlations
Pandas Correlations
Plotting
Pandas Plotting
Quiz/Exercises
Pandas EditorPandas QuizPandas ExercisesPandas SyllabusPandas Study PlanPandas
Certificate
References
DataFrames Reference
Pandas Introduction
❮ PreviousNext ❯
What is Pandas?
Pandas is a Python library used for working with data sets.
It has functions for analyzing, cleaning, exploring, and manipulating data.
The name "Pandas" has a reference to both "Panel Data", and "Python Data Analysis" and
was created by Wes McKinney in 2008.
Panel Data
Python Dates
Python Algorithms
Submit Answer »
To install NumPy, you can follow the method depending on your environment:
🔍 Verify Installation
After installation, test it in Python:
python
CopyEdit
import numpy as np
print(np.__version__)
If it prints a version number like 1.24.3, NumPy is installed correctly.
💡 Common Issues
If you get a Permission denied error, try:
css
CopyEdit
pip install numpy --user
If pip is not recognized, you might need to install/update pip:
css
CopyEdit
python -m ensurepip --upgrade
step-by-step guide to install NumPy in VS Code:
Great! Here's how to set up and use a virtual environment in VS Code (ideal for Python
projects like using NumPy):
🔧 Step-by-Step: Virtual Environment Setup in VS Code
✅ Step 1: Open VS Code and Terminal
1. Open your project folder in VS Code.
2. Open the terminal:
Press Ctrl + ` (or go to Terminal > New Terminal).
✅ Check Installation
You can confirm NumPy is installed by running this in any Python file:
python
CopyEdit
import numpy as np
print(np.__version__)
It should print something like:
CopyEdit
1.26.4
Step-by-Step: Create Virtual Environment and Install NumPy in PyCharm
✅ Step 1: Open or Create a Project
1. Open PyCharm.
2. Either:
o Click “New Project”, or
o Open an existing project.
💻 For Windows:
✅ Step 1: Download PyCharm
1. Go to the official JetBrains website:
🔗 https://www.jetbrains.com/pycharm/download
2. Choose:
o Community Edition – Free version (recommended for students and
beginners).
o Professional Edition – Paid version (offers web development, database
support).
3. Click Download under the version you want.
✅ Step 2: Install PyCharm
1. Open the downloaded .exe file.
2. Click Next until you reach Installation Location.
3. Choose installation options:
o ✔ Create Desktop Shortcut
o ✔ Add "Open Folder as Project"
o ✔ Add to PATH (optional)
4. Click Install.
5. Click Finish after installation.
✅ Step 3: First Launch
1. Open PyCharm (double-click desktop icon).
2. Choose UI theme (Light/Dark), configure settings.
3. You're ready to start a new Python project!
🍎 For macOS:
✅ Step 1: Download PyCharm
Go to: https://www.jetbrains.com/pycharm/download
Choose Community Edition (free) or Professional Edition.
Click Download.
✅ Step 2: Install
1. Open the downloaded .dmg file.
2. Drag and drop PyCharm into the Applications folder.
✅ Step 3: Launch
Go to Applications > PyCharm and open it.
Approve security prompt if shown.
Choose UI settings and configure interpreter.
🐧 For Linux:
✅ Step 1: Download
Go to: https://www.jetbrains.com/pycharm/download
Download the tar.gz file (Community or Pro).
✅ Step 2: Install
1. Extract the .tar.gz file:
bash
CopyEdit
tar -xzf pycharm-community-*.tar.gz
2. Navigate into the extracted folder:
bash
CopyEdit
cd pycharm-community-*/bin
3. Run:
bash
CopyEdit
./pycharm.sh
✅ Optional: Create Desktop Entry
From within PyCharm:
Tools > Create Desktop Entry
✅ After Installation
Once installed, you can:
Create a new project.
Set up a virtual environment.
Install packages like NumPy using the PyCharm interface or terminal.
📘 पायथन लाइब्रेरीज़: NumPy और Pandas पर परिचय
Matplotlib is a comprehensive library used for creating static, animated, and interactive
visualizations in Python. It is widely used for:
Line plots
Bar charts
Scatter plots
Histograms
Pie charts
Subplots and more
✅ Matplotlib works well with NumPy and Pandas.
🧰 2. Installation of Matplotlib
If not already installed, install using pip:
pip install matplotlib
📥 3. Importing Matplotlib
import matplotlib.pyplot as plt
pyplot is a module in Matplotlib that provides a MATLAB-like interface.
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]
plt.plot(x, y)
plt.title("Simple Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
📝 Explanation:
plot(x, y) draws the line
title() sets the chart title
xlabel() and ylabel() label the axes
show() displays the plot window
📊 6. Bar Chart
subjects = ['Math', 'Physics', 'Chemistry']
marks = [90, 85, 95]
📈 7. Scatter Plot
x = [5, 7, 8, 7, 2, 17, 2]
y = [99, 86, 87, 88, 100, 86, 103]
plt.scatter(x, y, color='red')
plt.title("Scatter Plot Example")
plt.xlabel("X values")
plt.ylabel("Y values")
plt.show()
📌 Use to visualize relationships/correlation between variables.
📉 8. Histogram
ages = [22, 55, 62, 45, 21, 22, 34, 42, 42, 34, 42, 22]
🥧 9. Pie Chart
activities = ['Sleep', 'Work', 'Exercise', 'Leisure']
hours = [8, 9, 2, 5]
plt.tight_layout()
plt.show()
📝 subplot(rows, cols, index) helps manage layout.
plt.plot(x, y)
plt.title("Sine Wave")
plt.show()
📝 np.linspace(start, stop, num_points) generates evenly spaced values.
🔰 1. Matplotlib का परिचय
Matplotlib एक लोकप्रिय Python लाइब्रेरी है जिसका उपयोग विभिन्न प्रकार
के ग्राफ, चार्ट, और विज़ुअलाइजेशन बनाने के लिए किया जाता है। इसके
द्वारा आप बना सकते हैं:
लाइन प्लॉट (Line Plot)
बार चार्ट (Bar Chart)
स्कैटर प्लॉट (Scatter Plot)
हिस्टोग्राम (Histogram)
पाई चार्ट (Pie Chart)
सबप्लॉट (Subplots) आदि
✅ यह NumPy और Pandas के साथ आसानी से काम करता है।
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]
plt.plot(x, y)
plt.title("साधारण लाइन प्लॉट")
plt.xlabel("X-अक्ष")
plt.ylabel("Y-अक्ष")
plt.show()
📌 समझाइए:
plot() से लाइन बनती है।
title() चार्ट का नाम सेट करता है।
xlabel() और ylabel() अक्ष के नाम सेट करते हैं।
show() से ग्राफ स्क्रीन पर दिखता है।
plt.scatter(x, y, color='red')
plt.title("स्कैटर प्लॉट उदाहरण")
plt.xlabel("X मान")
plt.ylabel("Y मान")
plt.show()
📌 उपयोग: दो मानों के बीच संबंध को दिखाना।
📉 8. हिस्टोग्राम (Histogram)
python
CopyEdit
ages = [22, 55, 62, 45, 21, 22, 34, 42, 42, 34, 42, 22]
plt.subplot(1, 2, 1)
plt.plot(x, y1)
plt.title("पहला प्लॉट")
plt.subplot(1, 2, 2)
plt.plot(x, y2, color='red')
plt.title("दूसरा प्लॉट")
plt.tight_layout()
plt.show()
📝 subplot(rows, columns, index) से आप लेआउट को नियंत्रित करते हैं।
plt.plot(x, y)
plt.title("साइन वेव")
plt.show()
📝 linspace() 0 से 10 के बीच 100 पॉइंट्स देता है।