Skip to content

================ by Jawad Haider

00 - Numpy Arrays


Image
Copyright Qalmaqihir
For more information, visit us at www.github.com/qalmaqihir/



NumPy

NumPy is a powerful linear algebra library for Python. What makes it so important is that almost all of the libraries in the PyData ecosystem (pandas, scipy, scikit-learn, etc.) rely on NumPy as one of their main building blocks. Plus we will use it to generate data for our analysis examples later on!

NumPy is also incredibly fast, as it has bindings to C libraries. For more info on why you would want to use arrays instead of lists, check out this great StackOverflow post.

We will only learn the basics of NumPy. To get started we need to install it!

Installation Instructions

NumPy is already included in your environment! You are good to go if you are using the course environment!


For those not using the provided environment:

It is highly recommended you install Python using the Anaconda distribution to make sure all underlying dependencies (such as Linear Algebra libraries) all sync up with the use of a conda install. If you have Anaconda, install NumPy by going to your terminal or command prompt and typing:

conda install numpy

If you do not have Anaconda and can not install it, please refer to Numpy’s official documentation on various installation instructions.


Using NumPy

Once you’ve installed NumPy you can import it as a library:

import numpy as np

NumPy has many built-in functions and capabilities. We won’t cover them all but instead we will focus on some of the most important aspects of NumPy: vectors, arrays, matrices and number generation. Let’s start by discussing arrays.

NumPy Arrays

NumPy arrays are the main way we will use NumPy throughout the course. NumPy arrays essentially come in two flavors: vectors and matrices. Vectors are strictly 1-dimensional (1D) arrays and matrices are 2D (but you should note a matrix can still have only one row or one column).

Let’s begin our introduction by exploring how to create NumPy arrays.

Creating NumPy Arrays

1. From a Python List

We can create an array by directly converting a list or list of lists:

my_list = [1,2,3]
my_list
[1, 2, 3]
np.array(my_list)
array([1, 2, 3])
my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
my_matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
np.array(my_matrix)
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

2. Built-in Methods

There are lots of built-in ways to generate arrays.

arange

Return evenly spaced values within a given interval.[Numpy ndarray arange]

np.arange(0,10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0,11,2)
array([ 0,  2,  4,  6,  8, 10])

zeros and ones

Generate arrays of zeros or ones.[Numpy ndarray zeros]

np.zeros(3)

np.zeros((5,5))
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
np.ones(3)
array([1., 1., 1.])
np.ones((3,3))
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

linspace

Return evenly spaced numbers over a specified interval.[Numpy ndarray linspace]

np.linspace(0,10,3)
array([ 0.,  5., 10.])
np.linspace(0,5,20)
array([0.        , 0.26315789, 0.52631579, 0.78947368, 1.05263158,
       1.31578947, 1.57894737, 1.84210526, 2.10526316, 2.36842105,
       2.63157895, 2.89473684, 3.15789474, 3.42105263, 3.68421053,
       3.94736842, 4.21052632, 4.47368421, 4.73684211, 5.        ])

Note that .linspace() includes the stop value. To obtain an array of common fractions, increase the number of items:

np.linspace(0,5,21)
array([0.  , 0.25, 0.5 , 0.75, 1.  , 1.25, 1.5 , 1.75, 2.  , 2.25, 2.5 ,
       2.75, 3.  , 3.25, 3.5 , 3.75, 4.  , 4.25, 4.5 , 4.75, 5.  ])

eye

Creates an identity matrix [Numpy eye]

np.eye(4)
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])

Random

Numpy also has lots of ways to create random number arrays. Here we will go through some of the most used methods from random module

1. rand

Creates an array of the given shape and populates it with random samples from a uniform distribution over [0, 1). [Random rand]

np.random.rand(2)
array([0.69647666, 0.14395438])
np.random.rand(5,5)
array([[0.9112553 , 0.75849901, 0.43392287, 0.4134459 , 0.10902179],
       [0.66881652, 0.21265267, 0.21783956, 0.08716564, 0.46147918],
       [0.16064897, 0.38241433, 0.50076915, 0.58926492, 0.69837196],
       [0.88502465, 0.2996012 , 0.49291933, 0.75316852, 0.29998398],
       [0.42345042, 0.57034504, 0.94797283, 0.70571464, 0.35788149]])

2. randn

Returns a sample (or samples) from the “standard normal” distribution [σ = 1]. Unlike rand which is uniform, values closer to zero are more likely to appear. [Radnom randn]

np.random.randn(2)
array([-0.55673554, -0.08515858])
np.random.randn(5,5)
array([[ 0.83041645, -1.22369138,  0.258011  ,  0.90984287, -0.48702078],
       [-0.88539528, -0.54034218, -0.39928196,  0.85910869, -0.36305332],
       [ 0.132046  , -1.28709664,  0.49352402,  0.80293611,  0.2601146 ],
       [ 0.74912365,  0.16013944,  0.39345536, -0.52355146,  1.0536796 ],
       [ 0.00293273, -0.14715505, -1.22460234, -0.65347358, -0.31514422]])

3. randint

Returns random integers from low (inclusive) to high (exclusive). [Random randint]

np.random.randint(1,100)
42
np.random.randint(1,100,10)
array([33, 26, 51, 78, 89, 15, 42, 68, 14, 62])

4. seed

Can be used to set the random state, so that the same “random” results can be reproduced. [Numpy ndarray random]

np.random.seed(42)
np.random.rand(4)
array([0.37454012, 0.95071431, 0.73199394, 0.59865848])
np.random.seed(42)
np.random.rand(4)
array([0.37454012, 0.95071431, 0.73199394, 0.59865848])

Array Attributes and Methods

Let’s discuss some useful attributes and methods for an array:
In particular, the reshape attribute and max,min,argmax, argmin, shape & the dytpe methods
Let’s first create two numpy arrays to experiment with :)

arr = np.arange(25)
ranarr = np.random.randint(0,50,10)
arr
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24])
ranarr
array([38, 18, 22, 10, 10, 23, 35, 39, 23,  2])

1. Reshape

Returns an array containing the same data with a new shape.[Numpy ndarray reshape]

arr.reshape(5,5)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

2. max, min, argmax, argmin

These are useful methods for finding max or min values. Or to find their index locations using argmin or argmax

ranarr
array([38, 18, 22, 10, 10, 23, 35, 39, 23,  2])
ranarr.max()
39
ranarr.argmax()
7
ranarr.min()
2
ranarr.argmin()
9

3. Shape

Shape is an attribute that arrays have (not a method):[Numpy ndarray Shape]

# Vector
arr.shape
(25,)
# Notice the two sets of brackets
arr.reshape(1,25)
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
        16, 17, 18, 19, 20, 21, 22, 23, 24]])
arr.reshape(1,25).shape
(1, 25)
arr.reshape(25,1)
array([[ 0],
       [ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
       [ 6],
       [ 7],
       [ 8],
       [ 9],
       [10],
       [11],
       [12],
       [13],
       [14],
       [15],
       [16],
       [17],
       [18],
       [19],
       [20],
       [21],
       [22],
       [23],
       [24]])
arr.reshape(25,1).shape
(25, 1)

4. dtype

You can also grab the data type of the object in the array:[Numpy ndarray dtype]

arr.dtype
dtype('int64')
arr2 = np.array([1.2, 3.4, 5.6])
arr2.dtype
dtype('float64')

Great Job! Thats the end of this part.

Don't forget to give a star on github and follow for more curated Computer Science, Machine Learning materials