4 - NumPy Basics- Arrays and Vectorized Computation

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/21

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

22 Terms

1
New cards

NumPy

NumPy (Numerical Python) is a foundational package for numerical computing in Python, providing efficient multidimensional arrays and tools for working with them.

2
New cards

ndarray

An ndarray is a multidimensional array object in NumPy that enables fast array-oriented arithmetic operations and flexible broadcasting capabilities.

3
New cards

Creating a NumPy array from a list

Use np.array(list). For example:

data = [1, 2, 3]

arr = np.array(data)

4
New cards

Shape attribute of an array

The shape attribute is a tuple indicating the size of each dimension of the array. For example, a 2x3 array has shape (2, 3).

5
New cards

Creating an array of zeros

Use np.zeros(shape). For example:

np.zeros((3, 4)) # Creates a 3x4 array of zeros.

6
New cards

Purpose of the dtype attribute

The dtype attribute describes the data type of the elements in the array, such as float64 or int32.

7
New cards

Element-wise arithmetic operations

Use standard operators (+, -, *, /). For example:

arr * 2 # Multiplies each element by 2.

8
New cards

Boolean indexing in NumPy

Boolean indexing allows you to select elements from an array using a boolean condition. For example:

arr[arr > 0] # Selects all positive elements.

9
New cards

Transposing a NumPy array

Use the T attribute or the transpose() method. For example:

arr.T # Transposes the array.

10
New cards

np.where function

np.where(condition, x, y) returns elements from x where condition is True and from y otherwise. For example:

np.where(arr > 0, 2, -2) # Replaces positives with 2 and negatives with -2.

11
New cards

Computing the mean of an array

Use the mean() method or np.mean(). For example:

arr.mean() # Computes the mean of all elements.

12
New cards

Purpose of the cumsum method

cumsum() computes the cumulative sum of elements along an axis. For example:

arr.cumsum() # Returns an array of cumulative sums.

13
New cards

Sorting a NumPy array

Use the sort() method. For example:

arr.sort() # Sorts the array in-place.

14
New cards

np.unique function

np.unique returns the sorted unique values in an array. For example:

np.unique([1, 2, 2, 3]) # Returns [1, 2, 3].

15
New cards

Saving and loading a NumPy array

Use np.save('filename.npy', arr) to save and np.load('filename.npy') to load.

16
New cards

Difference between np.dot and * operator

np.dot performs matrix multiplication, while * performs element-wise multiplication.

17
New cards

Generating random numbers in NumPy

Use functions from np.random, such as np.random.randn() for standard normal samples or np.random.randint() for integers.

18
New cards

Purpose of np.random.seed

np.random.seed sets the random number generator's seed for reproducibility. For example:

np.random.seed(1234) # Ensures the same random numbers are generated each time.

19
New cards

Computing the inverse of a matrix

Use np.linalg.inv(matrix). For example:

inv_mat = np.linalg.inv(mat)

20
New cards

argmax method

argmax returns the indices of the maximum values along an axis. For example:

arr.argmax() # Returns the index of the maximum value.

21
New cards

Computing the QR decomposition

Use np.linalg.qr(matrix). For example:

q, r = np.linalg.qr(mat)

22
New cards

Broadcasting in NumPy

Broadcasting allows NumPy to perform operations on arrays of different shapes by expanding dimensions as needed.