0% found this document useful (0 votes)
3 views7 pages

Python Programming in AI

This document covers the use of Python's NumPy and pandas libraries for efficient numerical operations and data management. It includes an overview of data analysis, detailed operations with NumPy arrays and pandas DataFrames, and a real-world case study on retail sales analysis. The session emphasizes the importance of these tools in processing large datasets and extracting actionable insights.

Uploaded by

prasun.ongc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Python Programming in AI

This document covers the use of Python's NumPy and pandas libraries for efficient numerical operations and data management. It includes an overview of data analysis, detailed operations with NumPy arrays and pandas DataFrames, and a real-world case study on retail sales analysis. The session emphasizes the importance of these tools in processing large datasets and extracting actionable insights.

Uploaded by

prasun.ongc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Efficient Numerical Operations and Data

Management

1. Introduction & Context


This session focuses on using Python’s core libraries—NumPy and pandas—to
perform efficient numerical operations and manage large datasets. By leveraging
these tools, you will learn to manipulate data arrays, handle missing values,
perform aggregations, and extract actionable insights from real-world data.

2. Useful Links
Link to Python files and resources used in the lecture:

Link 1

Link 2

Link 3

Link 4
3. Main Content

3.1 Overview of Data Analysis with Python


Data Analysis: Processing, cleaning, transforming, and modeling data to extract
insights.

Key Roles: Data Analysts, Data Engineers, Data Scientists, and Data Visualization
Specialists.

Python’s Importance: Its simplicity and powerful libraries make it the preferred
choice for data analysis.

3.2 NumPy and pandas Libraries

3.2.1 NumPy

Definition: NumPy (Numerical Python) is a fundamental library that supports


large, multi-dimensional arrays (ndarrays) and high-level mathematical functions.

Advantages:

Fast, vectorized operations that eliminate slow Python loops.


Memory efficiency and ability to handle large datasets.

Example:

import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)

3.2.2 pandas

Definition: pandas is a library for data manipulation and analysis that provides
powerful data structures such as Series (1D) and DataFrame (2D).

Advantages:

Ability to handle heterogeneous data.


Intuitive data cleaning, filtering, grouping, and aggregation.
Example:

import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = [Link](data)
print(df)

3.3 Detailed NumPy Operations

3.3.1 Python Lists vs. NumPy Arrays

Lists: Can hold mixed data types; slower for numerical tasks.

NumPy Arrays: Homogeneous; optimized for performance with vectorized


operations.

Feature Python Lists NumPy Arrays

Data Type Mixed types Uniform type

Performance Slower Fast (vectorized operations)

Multi-dimensional Support Limited Full support (ndarray)

3.3.2 Creating and Inspecting Arrays

Creation:

arr = [Link]([1, 2, 3, 4, 5])


multi_arr = [Link]([[1, 2, 3], [4, 5, 6]])

Checking Type and Shape:

print(type(arr)) # <class '[Link]'>


print(multi_arr.shape) # (2, 3)

3.3.3 Array Operations


Operations include scalar addition, aggregation, indexing, slicing, logical indexing,
reshaping, and transposing.
# Scalar Addition
arr + 5

# Aggregation
[Link](arr)
[Link](arr)

# Indexing & Slicing


element = arr[1]
slice_arr = arr[1:4]
step_slice = arr[1:5:2]

# Logical Indexing
even_elements = arr[arr % 2 == 0]

# Reshaping & Transposing


reshaped = multi_arr.reshape(3, 2)
transposed = multi_arr.T

3.3.4 Special Functions

Examples include [Link](), [Link](), [Link](), [Link]().

3.4 Detailed pandas Data Management

3.4.1 DataFrames and Series

DataFrame: A 2D table with labeled rows and columns.

Series: A 1D array with labels; each DataFrame column is a Series.

3.4.2 Importing and Indexing Data


Loading Data:

df = pd.read_csv('[Link]')

Accessing Columns:

ages = df['Age']

Indexing with loc and iloc:


first_row = [Link][0]
element = [Link][0, 1]

3.4.3 Handling Missing Data

Identification: [Link]().sum()

Imputation: [Link](0) or replacing with column mean:

df['Age'] = df['Age'].fillna(df['Age'].mean())

3.4.4 Grouping and Aggregation


GroupBy Example:

grouped = [Link]('department')
mean_salary = grouped['salary'].mean()

3.5 Real-World Case Study: Retail Sales Analysis


Scenario: Analyze a retail sales dataset to improve inventory management and
boost revenue.

Steps:

Load Data: df = pd.read_csv('retail_sales_data.csv')

Explore Data: Use [Link]() and [Link]()


Key Metrics:
Total Revenue: total_revenue = df['total_sales'].sum()

Price Extremes:
highest_price = df['price_per_unit'].max()
lowest_price = df['price_per_unit'].min()

Average Units Sold: average_units_sold = df['units_sold'].mean()


Store Analysis:
store_revenue = [Link]('store_id')['total_sales'].sum()
best_store = store_revenue.idxmax()
revenue_std = df['total_sales'].std()

Outcomes: Identification of top-performing stores, insights into revenue


distribution and pricing trends, actionable data for strategic decision-making.

4. Additional Reading Resources


For further in-depth study of numerical operations and data management with
Python, consider exploring these resources:

Official NumPy Documentation: [Link]

Official Pandas Documentation: [Link]

KDnuggets – NumPy with Pandas for More Efficient Data Analysis:


KDnuggets
5. Self-Test Questions
What is the primary difference between Python lists and NumPy arrays, and
why are NumPy arrays preferred for numerical operations?
How do you create a NumPy array from a list and check its shape? Provide a
code example.
Explain how to handle missing data in a pandas DataFrame, including one
method for imputation.
Describe the process of grouping data in pandas and how it can be used to
compute aggregated statistics.
What is the purpose of vectorized operations in NumPy, and how do they
improve performance compared to traditional Python loops?

6. Conclusion & Summary


This session demonstrated how NumPy and pandas enable efficient numerical
operations and robust data management in Python. Key topics included creating
and manipulating NumPy arrays, performing vectorized operations, handling
missing data in pandas, and grouping data for aggregation. A real-world retail
sales analysis case study illustrated the practical application of these techniques.

Mastering these tools will allow you to efficiently process large datasets, derive
actionable insights, and prepare a solid foundation for more advanced data
science projects.

You might also like