 Python NumPy for Machine Learning

Python is the most preferred language for machine learning and is loved by thousands of machine learning developers and millions of enthusiasts mostly, because of its vast library size. In today’s blog, we will specifically talk about the NumPy python library.

NumPy basically does complex mathematics to generate sample data for machine learning and various other purposes. From making multi-dimensional arrays to multi-dimensional matrices, numpy is python’s mathematics master. Jim Hugunin is the creator of NumPy which was later upgraded by other developers. To get the best knowledge of the working of NumPy, we shall see its implementation through code.

Numerical Python (NumPy) code Implementation:-

Let us take an example where we are going to create an array with the help of the NumPy library, steps involved are:-

• Importing library
• Creating a list
• Using the NumPy library to create an array of that list

Numpy Array

 import  numpy  as  np length = [2,6,8] x = np.array(length) x

Output : array([2, 6, 8])

Above was the example of the one-dimensional array, now we shall see one example of a multi-dimensional array.

 a = np.array([[1,2,3], [4,5,6], [7,8,9]]) print(a) print(a.shape)

Output : [[1 2 3]

[4 5 6]

[7 8 9]]

(3, 3)

In the above example, we have created an array of size 3*3 and printed it. (3,3) shows the size of the array.

Now we shall see an example of making an identity matrix using NumPy

Identity Matrix using Numpy

 np.eye(4) # Identity Matrix

Output: array([[1., 0., 0., 0.],

[0., 1., 0., 0.],

[0., 0., 1., 0.],

[0., 0., 0., 1.]])

np.eye() is used to create an identity matrix and the number inside the brackets shows the size of the Identity matrix.

Another example to create an identity matrix of zeroes is shown below:

 np.zeros((2,3))

Output: array([[0., 0., 0.],

[0., 0., 0.]])

np.zeros() is used to make the identity matrix of zeros of any size.

Just like np.zeros(), we have another example of making an identity matrix of one’s of any size that is np.ones().

 q = np.ones((3,2)) q

Output: array([[1., 1.],

[1., 1.],

[1., 1.]])

In order to create the Diagonal matrix, we have another function.

Diagonal array using Numpy

 np.diag(np.array([[1,2,3],[4,5,6],[7,8,9]])) # Diagonal array

Output: array([1, 5, 9])

And,

 np.diag((10,99,44))

Output: array([[10,  0,  0],

[ 0, 99,  0],

[ 0,  0, 44]])

At last, We can create reshape and resize any numbers within some range.

Let us see what np.arrange() function can do.

 n = np.arange(0,30,2) n

Output: array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28])

Reshaping the above example:-

Numpy Reshape

 n.shape

Output: (15,)

 n = n.reshape(3,5) # the product should match the number of total elements n

Output: array([[ 0,  2,  4,  6,  8],

[10, 12, 14, 16, 18],

[20, 22, 24, 26, 28]])

There are various other operation such as Vectorization and Transpose that can be implemented using NumPy

Numpy Vectorization

 # Multiplication with a scalar print(np.array([1,2,3]) * 3, "-- array * 3") print("VS") print(np.array([1,2,3] * 3), "-- list * 3") print("VS") print(np.repeat([1,2,3], 3), "-- repeat method of numpy array")

Output: [3 6 9] -- array * 3

VS

[1 2 3 1 2 3 1 2 3] -- list * 3

VS

[1 1 1 2 2 2 3 3 3] -- repeat method of numpy array

Numpy Transpose

 y = np.array([2,4,5])   z = np.array([y, y**2]) z

Output: array([[ 2,  4,  5],

[ 4, 16, 25]])

 # Transpose print(z.T) print(z)

Output: [[ 2  4]

[ 4 16]

[ 5 25]]

[[ 2  4  5]

[ 4 16 25]]

Above was the list of functions that we could perform with the help of NumPy. This much knowledge with NumPy could help in data exploration and make it easy to play with machine Learning Algorithms. tanesh

Founder Of Aipoint, A very creative machine learning researcher that loves playing with the data.