numpy Flashcards
How do you use numpy as a shortcut?
- import numpy as np
Given an array my_list([1,2,3]), create an array ‘arr’ that is composed of my_list.
- my_list = [1,2,3]
- arr = np.arry(my_list)
- arr
Practice Creating ndarrays:
- How do you create a 1-dimensional numpy array of a list of three values?
- How do you create a 2-dimensional numpy array of two, three value lists?
- x = np.array([1,2,3])
- m = np.array([1,2,3], [4,5,6])
Create an array of even numbers from 0 to 10
np.arange(0,11,2)
How do you print the number of rows and columns in an array?
- 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
Create an array composed of values between 0 and 14
np.arange(15)
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?
- 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 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?
- o = np.linspace(0, 4, 9)
- o.resize(3,3)
How do you create a 3 x 2 array of 1’s?
- np.ones((3,2))
How do you create a 2 by 3 array of 0’s?
- np.zeros((2, 3))
How do you create a 3 x 3 array with 1’s in the diaganol?
- np.eye(3)
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?
- np.diag(y)

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?
- np.vstack([p, 2*p])
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?
- np.hstack([p, 2*p])
Given an array a = np.array([-4, -2, 1, 3, 5]),
- how would you add all of the values of the array together?
- how would you find the max value of the array?
- how would you find the min value of the array?
- how would you find the mean of the array?
- how would you find the standard deviation of the array?
- how would you find the max index value of the array?
- how would you find the min index value of the array?
- a.sum()
- a.max()
- a.min()
- a.mean()
- a.std()
- a.argmax()
- a. argmin()
Given the array s = np.arange(13)**2, how would you find?
- the first value of the array
- the fifth value of the array
- the range of the first three values of the array
- s[0]
- s[4]
- s[0:3]
- For an array ‘z’, how would you transpose the rows and columns?
- For an array ‘z’, how would you see what type of data the array has?
- For an array ‘z’, how would yo cast the array to the type ‘float’?
- z.T
- z.dtype
- z.astype(‘f’)
- What is an important distinction about Python’s built in lists when it comes to slicing arrays?
- 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.
For a 6 x 6, 2-dimensional array ‘r’, how would you get:
- the value three rows in and three columns downn?
- he values in the third row from the fourth column over?
- the first two rows, including all columns except the last column?
- every other value of the last row?
- set every value to ‘0’?
- r[2,2]
- r[3, 3:6]
- r[:2, :-1]
- r[-1, ::2]
- r[:] = 0
For a given array ‘r’, how can you copy the array?
- r_copy = r.copy()
How do you create a 4 x 3 array of random integers between the range of 0 and 9?
- test = np.random.randint(0, 10, (4, 3))
Python is an example of an ________ language?
interpreted
What is the result of the following code: [‘a’, ‘b’, ‘c’] + [1, 2, 3]?
[‘a’, ‘b’, ‘c’, 1, 2, 3]