Introduction to Numpy Arrays

In this class, We discuss Introduction to Numpy Arrays.

For Complete YouTube Video: Click Here

The reader should have prior knowledge of class and object concepts. Click here.

In python, We do not have the concept of arrays.

In other programming languages, we have arrays.

In most situations, arrays are essential. Because easy to access.

So python provided a module called ‘numpy‘. This module numpy was developed to use numerics.

In this module, they created the concept of an array.

In our python playlist, we discuss arrays. The remaining numeric options are used in data science, etc.

The remaining concepts of numpy are discussed according to the requirement in our data science and machine learning courses.

Arrays

An array is a grid of homogeneous elements, and it contains information about how to access elements.

Homogeneous means elements of the same type.

In the python list, we can take elements of a different type.

In arrays, we can take elements of the same type.

One Dimensional Array

Take an example and understand how to create one-dimensional arrays

We use the module ‘numpy’ and alias the module as np.

In the module ‘numpy’, we use the function ‘array’ to create an array.

The below example shows how to create an array.

# numpy arrays
# create array using array function and it returns array class object
import numpy as np
x=np.array([1,2,3])#one dimmension
print(x)

Output:
[1 2 3]

The function array takes a list as input and converts the list into an array.

In array, all elements are given continuous memory allocation.

In our following classes, We discuss how memory is allocated to arrays.

Two Dimensional Arrays

We use a list within the list to create two-dimensional arrays.

Given below example shows how to create a two-dimensional array.

y=np.array([[1,2,3],[4,5,6]])#two dimmensional
print(y)

Output:
[[1 2 3]
 [4 5 6]]

The two-dimensional array is of matrix form. It has rows and columns.

Three Dimensional Array

The three-dimensional array will have a sequence of two-dimensional arrays.

The below example shows the three-dimensional array.

z=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])#three dimmension
print(z)

Output:
[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]

In our example, we have two two-dimensional arrays of shape 2X3.

The shape 2X3 means two rows and three columns.

shape Attribute

The shape attribute will give the shape of the array.

The below example shows the shape of the array z.

print(z.shape)

Output:
(2, 2, 3)

It is a three-dimensional array with shape (2,2,3).

dtype Parameter

We use the parameter ‘dtype’ to mention the type of elements in the array.

Below example taking elements in the type float.

l=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]],dtype=float)#three dimmension
print(l)

Output:
[[[ 1.  2.  3.]
  [ 4.  5.  6.]]

 [[ 7.  8.  9.]
  [10. 11. 12.]]]

When we display the elements, they are displayed as float values.

Suppose the dtype is not mentioned. Python takes the minimum type required to save the elements is considered.

Below the example, we have all the elements integers except one element.

#if not mentioned any type
m=np.array([[1,2,3],[4,5,"hello"]])#minimum type required to hold the elements
print(m[0][0])#accessing element

Output:
1

One element is a string. So the type considered is a string.

Access Elements in Array

To access the elements in the array, we mention the rows and columns in the array.

The above example shows how to access a two-dimensional array.

By default, index values start from zero.

m[0][0] means we are accessing zeroth row zeroth column element.

We are taking two elements and adding the two elements.

print(m[0][0]+m[0][1])#string concatenation happen here default taken as string

Output:
12

Because elements are taken in string type. String concatenation happens when we add the elements.

Accessing Three-dimensional array

The below example shows how to access a three-dimensional array.

#three dimension array element accessing
l=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]],dtype=float)#three dimension
print(l[0][0][1])

Output:
2.0

The third dimension is indexed from zero.

The above example says the zeroth matrix takes a zeroth row and the first column element.

zeros Function

We use the function ‘zeros’ to create an array filled with all zeros.

The function zero will take the shape of the array.

Below example, the shape is given (2,3).

m=np.zeros((2,3))
print(m)

Output:
[[0. 0. 0.]
 [0. 0. 0.]]

It creates a two-dimensional array with two rows and three columns. The values are taken zeros.

ones function

Similar to zeros function. The ones function is used to create an array filled with all one value.

The below example shows the use of ones function.

n=np.ones((2,3))
print(n)

Output:
[[1. 1. 1.]
 [1. 1. 1.]]

empty function

Similar to the zeros function, the empty function is used to create an array with space.

Garbage values are displayed.

The below example shows how to use the empty function.

e=np.empty((2,4))
print(e)

Output:
[[-3.80543340e-039  3.39519327e-313  1.96082929e+243  1.04733407e-312]
 [-3.80532130e-039  3.39519327e-313  3.64460920e-086  3.64894209e-313]]