Lesson 6 Numpy Flashcards

1
Q

Import numpy and check what version you have

A

import numpy as np
numpy.__version__

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

Create an array from this list my_list = [1,2,3]

A

np.array(my_list)

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

Create an array from the matrix my_matrix = [[1,2,3],[4,5,6],[7,8,9]]

A

np.array(my_matrix)

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

my_arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

Add the values 1,2 to the array above.

A

np.append(my_arr, [1,2])

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

my_arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

Delete index 1 from my array

A

np.delete(my_arr, 1)

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

Arrange the numbers in ascending order: another_arr = np.array([2, 1, 5, 3, 7, 4, 6, 8])

A

np.sort(another_arr)

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

Return evenly spaced values within a given interval. Do this for the values from 0-9

A

np.arange(0,10)

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

Return evenly spaced values within a given interval. Do this for the values from 0-10 every 2 values

A

np.arange(0,11,2)

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

Generate a matrix of zeros - one row, 3 columns

A

np.zeros(3)

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

Generate a matrix of zeros - 10 rows, 10 cols

A

np.zeros((10,10))

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

Generate a matrix of ones, 5 rows, 5 cols

A

np.ones((5,5))

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

Return evenly spaced numbers over a specified interval. Do this for the numbers 0-10 with 3 numbers.

A

np.linspace(0,10,3)

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

Return evenly spaced numbers over a specified interval. Do this for the numbers 0-20 with 50 numbers.

A

np.linspace(0,20,50)

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

Create an identity matrix for 8 rows.

A

np.eye(8)

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

Return 3 random numbers (0-1) in an array.

A

np.random.rand(3)

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

Return random numbers (0-1) in a matrix with 4 rows and 4 cols.

A

np.random.rand(4,4)

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

Return a random sample from the “standard normal” distribution. Do this for 3 numbers.

A

np.random.randn(3)

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

Return a random sample from the “standard normal” distribution. Do this for 3 numbers.

A

np.random.randn(3)

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

Return random integers from 1-99

A

np.random.randint(1,100)

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

Return random integers from 1-99 as an array for 10 numbers.

A

np.random.randint(1,100,10)

21
Q

Return an array containing the same data with a new shape. Do this for the array arr = np.arange(30). Reshape the array into 5 rows and 6 cols.

A

arr.reshape(5,6)

22
Q

Return an array containing the same data with a new shape. Do this for the array arr = np.arange(30). Reshape the array into 5 rows and 2 cols.

A

rand_arr.reshape(5,2)

23
Q

find the index for the max and min value in the array rand_arr = np.random.randint(0,100,10)

A

rand_arr.argmax()
rand_arr.argmin()

24
Q

Find the max and min value for the array rand_arr = np.random.randint(0,100,10)

A

rand_arr.max()
rand_arr.min()

25
Tell me how to find out the number of elements in a dimension
arr.shape
26
How do I find out the number of axes or dimensions in an array.
arr.ndim
27
How do I find out the total number of elements of the array.
arr.size
28
Tell me the datatype of the object in the array.
arr.dtype
29
Specify the datatype to be an integer when making the following matrix. arr_2 = np.ones(2) and then check the datatype.
arr_2 = np.ones(2, dtype=int) arr_2.dtype
30
x = np.array([1, 2, 3, 4, 5, 6]) add a new axis
x2 = x[np.newaxis] x2.shape
31
For the array x = np.array([1, 2, 3, 4, 5, 6]), convert the 1D array to a col vector by inserting an axis along the first dimension. Then show the number of elements in each row and col.
col_vector = x[np.newaxis, :] col_vector.shape
32
For the array x = np.array([1, 2, 3, 4, 5, 6]), convert the 1D array to a row vector by inserting an axis along the first dimension. Then show the number of elements in each row and col.
row_vector = x[:, np.newaxis] row_vector.shape
33
Select the 8th index in this array new_arr = np.arange(0,110,10)
new_arr[8]
34
Select the numbers 10,20,40,40 in this array by splicing new_arr = np.arange(0,110,10)
new_arr[1:5]
35
new_arr = np.arange(0,110,10) convert everything from 0-40 to 100.
new_arr[0:5]=100
36
Multiply everything in this array by 2 new_arr = np.arange(0,110,10)
new_arr * 2
37
Change everything in the array new_arr = np.arange(0,110,10) to 88
arr_slice[:]=88 #Show Slice again arr_slice
38
Create a copy of the array.
new_arr_copy = new_arr.copy()
39
a = np.array([[1,1], [2,2]]) b = np.array([[3,3], [4,4]]) Combine the two arrays vertically
np.vstack((a, b))
40
Combine the two arrays horizontally a = np.array([[1,1], [2,2]]) b = np.array([[3,3], [4,4]])
np.hstack((a, b))
41
c = np.arange(1, 25) Make a new array and split it 3 ways
np.hsplit(c,3)
42
np.hsplit(c,3) split your array after the second column here three ways.
np.hsplit(c,(2,3))
43
Add, subtract and multiply these two arrays arr_1 = np.array([2,4]) arr_2 = np.ones(2)
arr_1 + arr_2 arr_1 - arr_2 arr_1 * arr_2
44
Add all of the elements in the array arr_3 = np.array([1, 2, 3, 4, 5, 6, 7, 8])
arr_3.sum()
45
Add the rows in a 2d array arr_4 = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
arr_4.sum(axis=0)
46
Add the columns in a 2d array. arr_4 = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
arr_4.sum(axis=1)
47
Find the unique elements in an array easily mixed_array = np.array([5,5,11,11,2,3,4,8,14,14,15,5])
mixed_array = np.array([5,5,11,11,2,3,4,8,14,14,15,5]) unique_vals = np.unique(mixed_array)
48
Find the index positions of unique values in an array mixed_array = np.array([5,5,11,11,2,3,4,8,14,14,15,5])
unique_vals_idx = np.unique(mixed_array, return_index=True)
49
Count the number of unique values in an array. mixed_array = np.array([5,5,11,11,2,3,4,8,14,14,15,5])
unique_vals_idx = np.unique(mixed_array, return_counts=True)