Creating arrays Flashcards
(35 cards)
What is NumPy?
NumPy (Numerical Python) is a library for numerical computations in Python, providing support for arrays, matrices, and mathematical functions.
Why use NumPy over Python lists?
NumPy is faster, more memory-efficient, and provides advanced functionality like vectorized operations and mathematical functions.
How to install NumPy?
Use pip install numpy
.
How to import NumPy?
import numpy as np
.
What is the purpose of the np
alias?
It is a convention to make the code more concise when using NumPy functions.
How to create a 1D array from a Python list?
arr = np.array([1, 2, 3, 4])
.
How to create a 2D array from a Python list?
matrix = np.array([[1, 2], [3, 4]])
.
How to create an array filled with zeros?
zeros_arr = np.zeros((3, 3))
.
How to create an array filled with ones?
ones_arr = np.ones((2, 4))
.
How to create an uninitialized array?
empty_arr = np.empty((2, 2))
.
What does arr.shape
return?
The dimensions of the array (e.g., (3, 3)
for a 3x3 array).
What does arr.ndim
return?
The number of dimensions (e.g., 2
for a 2D array).
What does arr.dtype
return?
The data type of the array elements (e.g., int32
, float64
).
What does arr.size
return?
The total number of elements in the array (e.g., 9
for a 3x3 array).
What are common data types in NumPy?
int32
, float64
, bool
, complex
.
How to specify the data type when creating an array?
arr = np.array([1, 2, 3], dtype=np.float64)
.
How to create an array with values from 0 to 10 with a step of 2?
arr = np.arange(0, 10, 2)
→ [0, 2, 4, 6, 8]
.
How to create an array with 5 evenly spaced values between 0 and 1?
arr = np.linspace(0, 1, 5)
→ [0.0, 0.25, 0.5, 0.75, 1.0]
.
How to create an array of random numbers between 0 and 1?
rand_arr = np.random.rand(3, 3)
.
How to create an identity matrix?
identity_matrix = np.eye(3)
or identity_matrix = np.identity(3)
.
How to create a diagonal matrix?
diag_matrix = np.diag([1, 2, 3])
→ [[1, 0, 0], [0, 2, 0], [0, 0, 3]]
.
What is the difference between np.eye()
and np.identity()
?
np.eye()
can create non-square matrices, while np.identity()
always creates square matrices.
What is vectorization in NumPy?
Performing operations on entire arrays without explicit loops, making code faster and more concise.
How to check the shape of an array?
Use arr.shape
.