numpy Flashcards

1
Q

How do you use numpy as a shortcut?

A
  • import numpy as np
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Given an array my_list([1,2,3]), create an array ‘arr’ that is composed of my_list.

A
  1. my_list = [1,2,3]
  2. arr = np.arry(my_list)
  3. arr
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Practice Creating ndarrays:

  1. How do you create a 1-dimensional numpy array of a list of three values?
  2. How do you create a 2-dimensional numpy array of two, three value lists?
A
  1. x = np.array([1,2,3])
  2. m = np.array([1,2,3], [4,5,6])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Create an array of even numbers from 0 to 10

A

np.arange(0,11,2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you print the number of rows and columns in an array?

A
  • m = np.array([1,2,3],[4,5,6])
  • print(m.shape)
    • returns (2,3
    • ‘2’ is the number of rows, ‘3’ is the number of columns
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Create an array composed of values between 0 and 14

A

np.arange(15)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you create a 1-dimensional array of even values between 0 and 30?

How would you reshape this array into a 3 x 5 array?

A
  • n = np.arange(0, 30, 2)
  • the np.arange syntax is (x, y, z), where:
    • x = min range value (inclusive)
    • y = max range value (exclusive)
    • z = step value
  • To reshape this array:
    • n = n.reshape(3, 5)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you create an array of 9 evenly spaced values from range 0 to 4?

How do you change the size of the array to a 3 x 3 array?

A
  • o = np.linspace(0, 4, 9)
  • o.resize(3,3)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you create a 3 x 2 array of 1’s?

A
  • np.ones((3,2))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you create a 2 by 3 array of 0’s?

A
  • np.zeros((2, 3))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you create a 3 x 3 array with 1’s in the diaganol?

A
  • np.eye(3)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Given an array y = np.array([4,5,6]), how would you create a 3 x 3 array with the ‘y-array’ through the diagnol?

A
  • np.diag(y)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Given an array p = np.ones([2,3]), how would you create a vertical stack of the array ‘p’ and an additional array of each integer within the p-array multiplied by 2?

A
  • np.vstack([p, 2*p])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Given an array p = np.ones([2,3]), how would you create a horizontal stack of the array ‘p’ and an additional array of each integer within the p-array multiplied by 2?

A
  • np.hstack([p, 2*p])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Given an array a = np.array([-4, -2, 1, 3, 5]),

  1. how would you add all of the values of the array together?
  2. how would you find the max value of the array?
  3. how would you find the min value of the array?
  4. how would you find the mean of the array?
  5. how would you find the standard deviation of the array?
  6. how would you find the max index value of the array?
  7. how would you find the min index value of the array?
A
  1. a.sum()
  2. a.max()
  3. a.min()
  4. a.mean()
  5. a.std()
  6. a.argmax()
  7. a. argmin()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Given the array s = np.arange(13)**2, how would you find?

  1. the first value of the array
  2. the fifth value of the array
  3. the range of the first three values of the array
A
  1. s[0]
  2. s[4]
  3. s[0:3]
17
Q
  1. For an array ‘z’, how would you transpose the rows and columns?
  2. For an array ‘z’, how would you see what type of data the array has?
  3. For an array ‘z’, how would yo cast the array to the type ‘float’?
A
  1. z.T
  2. z.dtype
  3. z.astype(‘f’)
18
Q
  • What is an important distinction about Python’s built in lists when it comes to slicing arrays?
A
  • Array slices are views on the original array.
  • This means that the data is not copied, and any modifications to the view will be reflected in the source array.
19
Q

For a 6 x 6, 2-dimensional array ‘r’, how would you get:

  1. the value three rows in and three columns downn?
  2. he values in the third row from the fourth column over?
  3. the first two rows, including all columns except the last column?
  4. every other value of the last row?
  5. set every value to ‘0’?
A
  1. r[2,2]
  2. r[3, 3:6]
  3. r[:2, :-1]
  4. r[-1, ::2]
  5. r[:] = 0
20
Q

For a given array ‘r’, how can you copy the array?

A
  • r_copy = r.copy()
21
Q

How do you create a 4 x 3 array of random integers between the range of 0 and 9?

A
  • test = np.random.randint(0, 10, (4, 3))
22
Q

Python is an example of an ________ language?

A

interpreted

23
Q

What is the result of the following code: [‘a’, ‘b’, ‘c’] + [1, 2, 3]?

A

[‘a’, ‘b’, ‘c’, 1, 2, 3]