CSC 203 Final: Arrays

0.0(0)
studied byStudied by 1 person
0.0(0)
full-widthCall with Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/27

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

28 Terms

1
New cards

How to create an array?

import numpy as np

array_name = np.array([#,#])

2
New cards

array_name.ndim

an array’s number of dimensions

3
New cards

array_name.shape

Always outputted as a tuple specifying an array’s dimensions (e.g., rows, columns)

4
New cards

array_name.size

An array’s total number of elements (product of the shape tuple’s values)

5
New cards
Example: a = np.array([1,2,3])

1-dimensional so a.ndim → 1 & a.shape → (3,) & a.size → 3

6
New cards
Example: b = np.array([[1,2,3],[4,5,6]])

2-dimensional so b.ndim → 2 & b.shape → (2,3) & b.size → 6

7
New cards
Example: d = np.array([[1],[2],[3],[4],[5]])

2-dimensional so d.ndim → 2 & d.shape → (5,1) & d.size → 5

8
New cards

3D array_name.shape format

(layers, rows, columns)

9
New cards

Another way to think about ndim

How many indices do you need to refer to one specific element of the array? That "how many" is your n or dimensionality of the array

<p><span style="background-color: transparent; font-family: &quot;Josefin Sans&quot;, sans-serif;"><span>How many indices do you need to refer to </span><em><span>one specific element</span></em><span> of the array? That "how many" is your n or </span><em><span>dimensionality</span></em><span> of the array</span></span></p>
10
New cards

In 1D arrays

A tuple with only one element in Python must have a trailing comma, e.g., (5,), to distinguish it from just the number 5.

Example:

  • floats = np.array([0.1, 0.2, 0.3, 0.4])

  • floats.shape

(4,)

11
New cards

1D shape example

a = np.array([1, 2, 3, 4, 5]) # a simple list of 5 elements.

print(a.shape)  # (5,)

print(a.ndim)   #1

12
New cards

2D column example

b = np.array([[1], [2], [3], [4], [5]]) # 5 rows, 1 column.

print(b.shape)  # (5, 1)

print(b.ndim)   #2

13
New cards

2D row example

c = np.array([[1, 2, 3, 4, 5]]) # 1 row, 5 columns.

print(c.shape)  # (1, 5)

print(c.ndim)   #2

14
New cards

np.arange(#)

Creates an array with a sequence of integers, similar to Python’s range()
15
New cards

np.arange() parameters

start = The beginning value of the sequence (default is 0).

stop = The sequence will go up to, but not include, the stop value.

step = The difference between consecutive elements (default is 1). To decrement, step is negative and start is a number greater than stop.

16
New cards

np.arange() output

array([#, #])

17
New cards

array_name.reshape(#,#)

Changes the shape of an array without changing its data. By reshaping, we can add or remove dimensions or change number of elements in each dimension.

18
New cards
reshape requirement
New shape must contain the exact same number of elements as the original array
19
New cards

Broadcasting

When one operand is a single value, called a scalar, NumPy performs the element-wise calculations as if the scalar were an array of the same shape as the other operand, but with the scalar value in all its elements.

Broadcasting also works with arrays of different shapes when compatible.

Original array is unchanged

20
New cards
Indexing 1D arrays
Same as Python lists; indexes start at 0
21
New cards

Indexing 2D arrays

Use array_name[row, column] to access an element

22
New cards

Selecting a row in 2D array

Use array_name[row]

23
New cards

Selecting sequential rows

Use slicing and indices: array_name[start:stop]

(stop not included)

24
New cards

Selecting non-sequential rows

Use a list of indices: array_name[[0,2,4]]

25
New cards
What slicing returns
Slices return views of the original array whenever possible
26
New cards

Example: c = np.array[

 [[1, 2, 3],

  [4, 5, 6]],   # Layer 1

 [[7, 8, 9],

  [10, 11, 12]] # Layer 2

]    

3-dimensional so c.ndim → 3 & c.shape → (2,2,3) & c.size → 12

27
New cards

No extra brackets

ndim is 1D → shape has one number, hence the trailing comma.

28
New cards

Extra brackets

like np([[1], [2], [3]]) make it 2D → shape is (3,1) or (3 rows, 1 column).

Explore top flashcards

October exam
Updated 465d ago
flashcards Flashcards (32)
10/6
Updated 218d ago
flashcards Flashcards (62)
PSCH 262 Final Exam
Updated 634d ago
flashcards Flashcards (110)
WWII
Updated 4d ago
flashcards Flashcards (35)
October exam
Updated 465d ago
flashcards Flashcards (32)
10/6
Updated 218d ago
flashcards Flashcards (62)
PSCH 262 Final Exam
Updated 634d ago
flashcards Flashcards (110)
WWII
Updated 4d ago
flashcards Flashcards (35)