Python NumPy For Your Grandma - 2.3 Creating NumPy Arrays
In this secion, we’ll look at different ways to create a NumPy array.
As we saw earlier, you can make an array by typing np dot array and then passing in a list of values.
import numpy as np np.array(['a', 'b', 'c']) ## array(['a', 'b', 'c'], dtype='<U1')
You can also make a 2-dimensional array from a list of lists like this.
np.array([ ['a', 'b'], ['c', 'd'], ['e', 'f'] ]) ## array([['a', 'b'], ## ['c', 'd'], ## ['e', 'f']], dtype='<U1')
You can also make a three dimensional array from a list of lists of lists.
np.array([ [ ['a', 'b'], ['c', 'd'], ['e', 'f'] ], [ ['g', 'h'], ['i', 'j'], ['k', 'l'] ] ]) ## array([[['a', 'b'], ## ['c', 'd'], ## ['e', 'f']], ## ## [['g', 'h'], ## ['i', 'j'], ## ['k', 'l']]], dtype='<U1')
… and so on.
Now let’s say you want to make a three by five array filled with zeros. This’ll be a good time so check out the NumPy documentation. It turns out, there’s a function called
zeros() that does exactly what we want. The only argument that doesn’t have a default value is
shape, so we’ll need to pass in the
shape as an int or tuple of ints.
If we call
np.zeros(shape = 3) we’ll get back a 1d array with three zeros.
np.zeros(shape = 3) ## array([0., 0., 0.])
But, our goal was to get a three by five array so I’m gonna set the shape to a tuple like this.
np.zeros(shape = (3,5)) ## array([[0., 0., 0., 0., 0.], ## [0., 0., 0., 0., 0.], ## [0., 0., 0., 0., 0.]])
Now, what about the generic case? Say, instead of zeros you want a three by five array filled with the word ‘cat’. For this, we can use a function called
full(). We just need to pass in a
fill_value. I’ll do that here.
np.full(shape = (3,5), fill_value = 'cat') ## array([['cat', 'cat', 'cat', 'cat', 'cat'], ## ['cat', 'cat', 'cat', 'cat', 'cat'], ## ['cat', 'cat', 'cat', 'cat', 'cat']], dtype='<U3')
Another very common array to build is a sequence array like
[1,2,3,4]. The function for this is
np.arange() where you pass in
step values. For example,
np.arange(start = 1, stop = 5, step = 1) ## array([1, 2, 3, 4])
(Note that start is inclusive while stop is exclusive.)
Alternatively, if you just do
np.arange() and pass in an integer like
10, you’ll get back a sequence of ten integers starting from zero.
np.arange(10) ## array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
By the way, the name “arange” stands for “array range” because it’s like the array version of a python range.
Another useful function is
np.random.randint() which let’s you build an array of random integers selected with replacement. Here we’ll make a two by three array of random ints between one (inclusive) and seven (exclusive). You can think of this as two iterations of rolling three dice.
np.random.randint(low = 1, high = 7, size = (2, 3)) ## array([[2, 3, 1], ## [5, 5, 2]])
Obviously there’s a bunch of other methods to build arrays, and we’ll see a lot of them later the course. But hopefully this was enough to get your feet wet and introduce you to the NumPy documentation.
- Basic Array Stuff
2.1 NumPy Array Motivation
2.2 NumPy Array Basics
2.3 Creating NumPy Arrays
2.4 Indexing 1-D Arrays
2.5 Indexing Multidimensional Arrays
2.6 Basic Math On Arrays
2.7 Challenge: High School Reunion
2.8 Challenge: Gold Miner
2.9 Challenge: Chic-fil-A
- Intermediate Array Stuff
3.4 Boolean Indexing
3.8 Challenge: Love Distance
3.9 Challenge: Professor Prick
3.10 Challenge: Psycho Parent
- Common Operations
4.2 Math Functions
4.8 Challenge: Movie Ratings
4.9 Challenge: Big Fish
4.10 Challenge: Taco Truck
- Advanced Array Stuff
5.1 Advanced Array Indexing
5.2 View vs Copy
5.3 Challenge: Population Verification
5.4 Challenge: Prime Locations
5.5 Challenge: The Game of Doors
5.6 Challenge: Peanut Butter
- Final Boss
6.3 Challenge: One-Hot-Encoding
6.4 Challenge: Cumulative Rainfall
6.5 Challenge: Table Tennis
6.6 Challenge: Where’s Waldo
6.7 Challenge: Outer Product