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()