Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🔹 1. What is NumPy?
NumPy (short for Numerical Python) is a
powerful open-source library for numerical computations in Python. It provides
a high-performance, N-dimensional array object called ndarray, and tools
for efficiently operating on arrays. It is widely used in data science, machine
learning, and scientific computing for tasks involving matrix algebra, data
manipulation, and mathematical operations.
🔹 2. Why NumPy over
Python Lists?
While Python lists are flexible, they are inefficient for
large-scale numerical computations. NumPy arrays:
🔹 3. Installing and
Importing NumPy
To get started, you need to install NumPy:
pip
install numpy
Then, import it in your Python code:
import
numpy as np
🔹 4. Creating Arrays in
NumPy
✅ 1D Array
a
= np.array([1, 2, 3])
✅ 2D Array
b
= np.array([[1, 2], [3, 4]])
✅ Using Built-in Functions
Function |
Description |
Example |
np.zeros() |
Creates an
array of all 0s |
np.zeros((2,
3)) |
np.ones() |
Creates an
array of all 1s |
np.ones((3,
2)) |
np.full() |
Fills with a
specified value |
np.full((2,
2), 7) |
np.eye() |
Identity
matrix |
np.eye(3) |
np.arange() |
Range of
values |
np.arange(0,
10, 2) |
np.linspace() |
Linearly
spaced values |
np.linspace(0,
1, 5) |
🔹 5. Array Attributes
arr
= np.array([[1, 2, 3], [4, 5, 6]])
Attribute |
Description |
Output |
arr.ndim |
Number of
dimensions |
2 |
arr.shape |
Tuple of
array dimensions |
(2, 3) |
arr.size |
Total number
of elements |
6 |
arr.dtype |
Data type of
elements |
int64 |
arr.itemsize |
Size of each
item (in bytes) |
8 (for int64) |
🔹 6. Indexing and Slicing
Arrays
✅ 1D Indexing
arr
= np.array([10, 20, 30, 40])
print(arr[2]) # Output: 30
✅ 2D Indexing
arr
= np.array([[1, 2, 3], [4, 5, 6]])
print(arr[1,
2]) # Output: 6
✅ Slicing
print(arr[0:2]) # First two rows
print(arr[:,
1]) # All rows, column 1
🔹 7. Array Reshaping
You can reshape arrays to different dimensions:
a
= np.arange(12)
b
= a.reshape((3, 4)) # 3 rows, 4 columns
You can also flatten arrays:
flat
= b.flatten()
🔹 8. Array Data Types
You can explicitly set the type of array elements:
arr
= np.array([1.5, 2.3], dtype=np.float32)
Data Type |
Description |
int32 / int64 |
Integer types |
float32 /
float64 |
Floating-point
numbers |
bool |
Boolean
values |
🔹 9. Copy vs View in
NumPy
a
= np.array([1, 2, 3])
b
= a.view()
c
= a.copy()
🔹 10. Basic Array
Operations
a
= np.array([1, 2, 3])
b
= np.array([4, 5, 6])
print(a
+ b) # [5 7 9]
print(a
* b) # [4 10 18]
print(a
** 2) # [1 4 9]
print(np.sqrt(a))
# [1. 1.41 1.73]
🔹 11. Broadcasting Basics
Broadcasting lets NumPy perform arithmetic between arrays of
different shapes.
a
= np.array([1, 2, 3])
b
= 2
print(a
+ b) # [3 4 5]
🔹 12. Summary Table: Key
Concepts
Feature |
Description |
np.array() |
Create a
NumPy array |
.ndim |
Number of dimensions |
.shape |
Shape of
array |
reshape() |
Change shape
without modifying data |
flatten() |
Convert to 1D |
Slicing |
Use : to
select rows/columns |
Broadcasting |
Operate
between arrays of different shapes |
NumPy is used for numerical computations, array operations, linear algebra, and data processing in Python.
NumPy arrays are faster, use less memory, and support vectorized operations, unlike Python lists which are slower and less flexible for numerical tasks
It’s the core data structure in NumPy — an N-dimensional array that allows element-wise operations and advanced indexing.
No, it needs to be installed separately using pip install numpy.
Broadcasting allows NumPy to perform operations on arrays of different shapes by automatically expanding them to be compatible.
Yes, it provides comprehensive support for matrix multiplication, eigenvalues, singular value decomposition, and more.
While NumPy is essential for preprocessing and fast array computations, deep learning libraries like TensorFlow or PyTorch build on top of it for more advanced tasks.
✅ Absolutely — Pandas is built on NumPy arrays, and Matplotlib supports NumPy for plotting.
✅ Yes — the numpy.random module offers distributions like normal, binomial, uniform, etc.
✅ Significantly. NumPy’s vectorized operations are typically 10x to 100x faster than traditional for-loops in Python.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)