December 29, 2019python courses
This video covers various ways to create a NumPy array from scratch.
import numpy as np # from list np.array(['a', 'b', 'c']) # from list of lists np.array([ ['a', 'b'], ['c', 'd'], ['e', 'f'] ]) # 3x5 array of 0s with np.zeros() np.zeros(shape = (3, 5)) # initialize the array with any value np.full(shape = (3, 5), fill_value = 'cat') # sequence of integers from 1 to N np.arange(start = 1, stop = 5, step = 1) # note that start is inclusive while stop is exclusive # random integers between 1 and 6 np.random.randint(low = 1, high = 7, size = (2, 3))
There are many ways to create a numpy array from scratch. Here are some of the most common.
- As we saw earlier, you can make an array from a list
- You can make a 2-dimensional array from a list of lists
- You can use np.zeros() to make an array of 0s
- More generally, you can use np.full() to initialize an array of any shape, filled with a specific value
- You can use np.arange() to make an array as a sequence of integers from 1 to N. Note that the start parameter is inclusive but the stop parameter is exclusive.
- You can use np.random.randint() to make an array of random integers in some range
These are some of the most common ways to make an array, but there are lots of other methods. We’ll see more of them as we go through the course.