0% found this document useful (0 votes)
5 views6 pages

NumPy_Study_Notes (2)

NumPy is a Python library designed for efficient numerical computing with arrays, outperforming standard Python lists in speed and memory usage. It provides essential functionalities for machine learning and data science, including element-wise operations and built-in mathematical functions. The document also covers the core attributes of NumPy arrays, methods for reshaping and summarizing data, and includes practice questions to reinforce understanding.

Uploaded by

hiafsal63
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)
5 views6 pages

NumPy_Study_Notes (2)

NumPy is a Python library designed for efficient numerical computing with arrays, outperforming standard Python lists in speed and memory usage. It provides essential functionalities for machine learning and data science, including element-wise operations and built-in mathematical functions. The document also covers the core attributes of NumPy arrays, methods for reshaping and summarizing data, and includes practice questions to reinforce understanding.

Uploaded by

hiafsal63
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

NumPy

Numerical Computing in Python for Machine Learning

NumPy (Numerical Python) is a Python library used for working with arrays. It is much faster than a normal
Python list because it is written in C internally and stores data in a fixed, continuous block of memory.
NumPy forms the base of almost every data science and machine learning library such as Pandas,
Scikit-learn, and TensorFlow.

Why NumPy instead of a Python list?


●​ Faster execution for large numerical data.
●​ Uses less memory compared to lists.
●​ Supports element-wise mathematical operations directly.
●​ Provides many ready-made functions for maths and statistics.

Installing and Importing

The core object in NumPy is the ndarray (N-dimensional array). Arrays can be created directly from a list, or
generated using built-in functions.
Every NumPy array has attributes that describe its structure. These are used constantly while checking or
debugging data shapes.

Attribute Meaning Example

ndim Number of dimensions [Link]

shape Size along each dimension [Link]

size Total number of elements [Link]

dtype Data type of elements [Link]

Elements of an array can be accessed using an index, similar to Python lists. For 2D arrays, indexing is
done as [row, column].
NumPy allows mathematical operations to be applied directly on arrays without using loops. Each operation
works element by element on arrays of the same shape.

Reshaping changes the way data is arranged without changing the actual values.

NumPy provides ready-made functions to quickly summarise data — very useful during Exploratory Data
Analysis (EDA).
The random module inside NumPy is used to generate random numbers — useful for creating sample/test data.
Practice Questions

Solve the following to strengthen your understanding of NumPy basics.

1.​What is NumPy and why is it faster than a normal Python list?

2.​What is the difference between a Python list and a NumPy array?

3.​What does the shape attribute of an array tell you?

4.​What is the difference between [Link]() and [Link]()?

5.​Explain the difference between reshape() and flatten().

Write the output for each of the following code snippets.

Q1.

Q2.

Q3.

Q4.

1.​Create a 1D NumPy array of numbers from 1 to 20 and print only the even numbers.

2.​Create a 3x3 identity matrix using NumPy.

3.​ Create an array of 10 random integers between 50 and 100, then print the maximum, minimum,
and average value.

4.​Create a 1D array of 12 elements and reshape it into a 3x4 matrix.

5.​Create two arrays of the same size and print their sum, difference, and product.

6.​Create an array [15, 3, 9, 27, 6] and print it sorted in ascending order.


7.​Create a 4x4 matrix of ones and change the second row to all zeros using indexing.

You might also like