0% found this document useful (0 votes)
2 views28 pages

Lecture 4

Lecture 4 covers the fundamentals of NumPy, a powerful library in Python for numerical computing that supports large multi-dimensional arrays and matrices. It discusses array creation, operations, and features such as broadcasting, indexing, and file handling. The lecture highlights the advantages of NumPy arrays over Python lists, including speed, memory efficiency, and support for advanced mathematical functions.

Uploaded by

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

Lecture 4

Lecture 4 covers the fundamentals of NumPy, a powerful library in Python for numerical computing that supports large multi-dimensional arrays and matrices. It discusses array creation, operations, and features such as broadcasting, indexing, and file handling. The lecture highlights the advantages of NumPy arrays over Python lists, including speed, memory efficiency, and support for advanced mathematical functions.

Uploaded by

Harry Borth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Lecture 4

Smart Data and Discovery


Data Understanding and Various Operations
Outline
• NumPy Intro
• Features
• Numpy array and python list
• Array creations
• Operations in numpy
• Arrays with initial place holders
NumPy(Numerical Python)
• used in almost every field of science and engineering
• powerful library in Python for numerical computing
• provides support for large multi-dimensional arrays and matrices
• brings the computational power of languages like C and Fortran to Python
• clear and elegant
• lies at the core of a rich ecosystem of data science libraries.
• forms the basis of powerful machine learning libraries like scikit-learn and SciPy
• NumPy’s accelerated processing of large arrays allows researchers to visualize datasets
far larger than native Python could handle.
NumPy(Numerical Python)
• Provides ndarray , fast and space-efficient multidimensional array providing vectorized
arithmetic operations, matrix operations.
• Provides linear algebra, random number generation.
• Standard mathematical functions for fast operations on entire arrays of data without
having to write loops.

NumPy ndarray
• Main object is homogeneous multidimensional array.
• Creating numpy array as vector.
• Creating numy array as matrix.
NumPy array and python list
• Numpy offers wide range of fast and efficient ways of creating arrays and manipulating
numerical data inside them
• List can contain different data types within a single list, all of the elements in a NumPy
array should be homogeneous
• NumPy arrays are faster and more compact than Python lists
• consumes less memory to store data and is convenient to use
Array Creation
• Import numpy

• Using python structures like lists and tuples


Array Creation contd…
NumPy (examples)
NumPy (examples)
Arrays with initial place holders
• Offers several functions to create array with initial placeholder content.
• Minimizes the necessity of growing array which is an expensive operation.
• Useful when the elements are unknown but size is known.
Multidimensional Arrays
Basic Operations
Basic Operations (Multi-Array)
Aggregate Functions
Some more functions
Working with files
Working With Files(Contd.)
Converting to numpy array
Sales Data Analysis
Statistical operations Numpy
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
Statistical operations Numpy
NumPy Broadcasting
• Broadcasting allows NumPy to perform element-wise operations on arrays of different shapes.
• It automatically expands smaller arrays to match the shape of larger ones.
Broadcasting Rules
• 1. If arrays have different dimensions, prepend 1s to shape.
• 2. Dimensions must match or one of them should be 1.
• 3. Arrays with size 1 in a dimension get stretched.
Indexing & Slicing
• NumPy allows array indexing similar to Python lists.
• Supports slicing with [start:stop:step] syntax.

Fancy Indexing

• Use arrays of indices to access multiple elements at once.

• Example: arr[[0, 2, 4]] returns elements at positions 0, 2, and


4.
NumPy Loadtxt Function
• loadtxt() reads text files into NumPy arrays.
• Supports CSV and space/tab-separated files.
Boolean Masking
• Use boolean conditions to filter data.
• Example: arr[arr > 5] returns all elements greater than 5.

Reshaping Arrays

• Reshape an array to a different shape without changing data.


• Example: arr.reshape(3, 4) converts a 1D array into 3x4 matrix.

Stacking and Splitting


• Stacking: Combine multiple arrays (hstack, vstack, dstack).
• Splitting: Divide an array into smaller arrays (split, hsplit, vsplit).
Any Questions

You might also like