# Python NumPy For Your Grandma - 2.3 Creating NumPy Arrays

Contents

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 `shape` and `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 `start`, `stop`, and `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.